Class: BetterController::Service
- Inherits:
-
Object
- Object
- BetterController::Service
- Defined in:
- lib/better_controller/service.rb
Overview
Base service class for resource operations
Instance Method Summary collapse
-
#all(ancestry_params = {}) ⇒ ActiveRecord::Relation
Get all resources.
-
#create(ancestry_params = {}, attributes) ⇒ Object
Create a new resource.
-
#destroy(ancestry_params = {}, id) ⇒ Object
Destroy a resource.
-
#find(ancestry_params = {}, id) ⇒ Object
Find a specific resource.
-
#update(ancestry_params = {}, id, attributes) ⇒ Object
Update an existing resource.
Instance Method Details
#all(ancestry_params = {}) ⇒ ActiveRecord::Relation
Get all resources
9 10 11 |
# File 'lib/better_controller/service.rb', line 9 def all(ancestry_params = {}) resource_scope(ancestry_params).all end |
#create(ancestry_params = {}, attributes) ⇒ Object
Create a new resource
25 26 27 28 29 30 31 |
# File 'lib/better_controller/service.rb', line 25 def create(ancestry_params = {}, attributes) resource = resource_class.new(prepare_attributes(attributes, ancestry_params)) resource.save if resource.respond_to?(:save) resource end |
#destroy(ancestry_params = {}, id) ⇒ Object
Destroy a resource
50 51 52 53 54 55 56 |
# File 'lib/better_controller/service.rb', line 50 def destroy(ancestry_params = {}, id) resource = find(ancestry_params, id) resource.destroy if resource.respond_to?(:destroy) resource end |
#find(ancestry_params = {}, id) ⇒ Object
Find a specific resource
17 18 19 |
# File 'lib/better_controller/service.rb', line 17 def find(ancestry_params = {}, id) resource_scope(ancestry_params).find(id) end |
#update(ancestry_params = {}, id, attributes) ⇒ Object
Update an existing resource
38 39 40 41 42 43 44 |
# File 'lib/better_controller/service.rb', line 38 def update(ancestry_params = {}, id, attributes) resource = find(ancestry_params, id) resource.update(prepare_attributes(attributes, ancestry_params)) if resource.respond_to?(:update) resource end |