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
@private
# File lib/upgrow/active_record_queries.rb, line 27 def self.included(base) base.extend(ClassMethods) end
Public Instance Methods
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
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
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
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