module Upgrow::ActiveRecordQueries

Mixin that implements Repository methods with an Active Record Base. When included in a Repository class, it sets the default base to be a class ending with `Record`.

Public Class Methods

included(base) click to toggle source

@private

# File lib/upgrow/active_record_queries.rb, line 27
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

all() click to toggle source

Fetches all Records and returns them as an Array of Models.

@return [Array<Model>] a collection of Models representing all persisted

Records.
# File lib/upgrow/active_record_queries.rb, line 35
def all
  to_model(base.all)
end
create(input) click to toggle source

Persists a new Record with the given input, and materializes the newly created Record as the returned Model instance.

@param input [Input] the Input with the attributes for the new Record.

@return [Model] the Model with the attributes of the newly created

Record.
# File lib/upgrow/active_record_queries.rb, line 46
def create(input)
  record = base.create!(input.attributes)
  to_model(record)
end
delete(id) click to toggle source

Deletes the Record that has the given ID.

@param id [Integer] the ID of the Record to be deleted.

# File lib/upgrow/active_record_queries.rb, line 77
def delete(id)
  base.destroy(id)
end
find(id) click to toggle source

Retrieves the Record with the given ID, representing its data as a Model.

@param id [Integer] the ID of the Record to be fetched.

@return [Model] the Model with the attributes of the Record with the given

ID.
# File lib/upgrow/active_record_queries.rb, line 57
def find(id)
  record = base.find(id)
  to_model(record)
end
update(id, input) click to toggle source

Updates the Record with the given ID with the given Input attributes.

@param id [Integer] the ID of the Record to be updated. @param input [Input] the Input with the attributes to be set in the

Record.

@return [Model] the Model instance with the updated data of the Record.

# File lib/upgrow/active_record_queries.rb, line 69
def update(id, input)
  record = base.update(id, input.attributes)
  to_model(record)
end