Module: Upgrow::ActiveRecordQueries
- Included in:
- Repository
- Defined in:
- lib/upgrow/active_record_queries.rb
Overview
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`.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#all ⇒ Array<Model>
Fetches all Records and returns them as an Array of Models.
-
#create(input) ⇒ Model
Persists a new Record with the given input, and materializes the newly created Record as the returned Model instance.
-
#delete(id) ⇒ Object
Deletes the Record that has the given ID.
-
#find(id) ⇒ Model
Retrieves the Record with the given ID, representing its data as a Model.
-
#update(id, input) ⇒ Model
Updates the Record with the given ID with the given Input attributes.
Class Method Details
.included(base) ⇒ Object
27 28 29 |
# File 'lib/upgrow/active_record_queries.rb', line 27 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#all ⇒ Array<Model>
Fetches all Records and returns them as an Array of Models.
35 36 37 |
# File 'lib/upgrow/active_record_queries.rb', line 35 def all to_model(base.all) end |
#create(input) ⇒ Model
Persists a new Record with the given input, and materializes the newly created Record as the returned Model instance.
46 47 48 49 |
# File 'lib/upgrow/active_record_queries.rb', line 46 def create(input) record = base.create!(input.attributes) to_model(record) end |
#delete(id) ⇒ Object
Deletes the Record that has the given ID.
77 78 79 |
# File 'lib/upgrow/active_record_queries.rb', line 77 def delete(id) base.destroy(id) end |
#find(id) ⇒ Model
Retrieves the Record with the given ID, representing its data as a Model.
57 58 59 60 |
# File 'lib/upgrow/active_record_queries.rb', line 57 def find(id) record = base.find(id) to_model(record) end |
#update(id, input) ⇒ Model
Updates the Record with the given ID with the given Input attributes.
69 70 71 72 |
# File 'lib/upgrow/active_record_queries.rb', line 69 def update(id, input) record = base.update(id, input.attributes) to_model(record) end |