Module: Cyrax::Extensions::HasService
- Extended by:
- ActiveSupport::Concern
- Included in:
- Resource
- Defined in:
- lib/cyrax/extensions/has_service.rb
Instance Method Summary collapse
-
#authorize_resource!(action, resource) ⇒ Object
Authorize a resource Should be called on each service method and should be implemented on each resource.
-
#build(&block) ⇒ Cyrax::Response
(also: #build!)
Builds a new resource without saving to DB Runs Model.new (before saving) Used for :new action in controller.
-
#build_collection ⇒ type
Returns a collection of the resource we are calling.
-
#build_resource(id, attributes = {}) ⇒ object
Instantiates the resource.
-
#create(custom_attributes = nil, &block) ⇒ Cyrax::Response
(also: #create!)
Creates a new resource and persists to DB Used for :create action in controller.
-
#delete_resource(resource) ⇒ Object
Remove a resource Calls destroy method on resource.
-
#destroy(&block) ⇒ Cyrax::Response
(also: #destroy!)
Destroys a resource from the DB Used for :destroy action in controller.
-
#find_resource(id) ⇒ object
Finds and returns a single item from the DB.
-
#read(&block) ⇒ Cyrax::Response
(also: #read!, #edit, #edit!)
Reads a single item from the DB Used for :show action in controller.
-
#read_all(&block) ⇒ Cyrax::Response
(also: #read_all!)
Builds and returns a collection response for Rails.
- #resource_params_id ⇒ Object
-
#save_resource(resource) ⇒ Object
Saves a resource.
- #transaction(&block) ⇒ Object
-
#update(custom_attributes = nil, &block) ⇒ Cyrax::Response
(also: #update!)
Updates a single item and persists to DB Used for :update action in controller.
Instance Method Details
#authorize_resource!(action, resource) ⇒ Object
Authorize a resource Should be called on each service method and should be implemented on each resource. Implementation may raise an exception which may be handled by controller.
125 126 127 |
# File 'lib/cyrax/extensions/has_service.rb', line 125 def (action, resource) # raise AuthorizationError end |
#build(&block) ⇒ Cyrax::Response Also known as: build!
Builds a new resource without saving to DB Runs Model.new (before saving) Used for :new action in controller
20 21 22 23 24 25 |
# File 'lib/cyrax/extensions/has_service.rb', line 20 def build(&block) resource = build_resource(nil) (:build, resource) block.call(resource) if block_given? respond_with resource end |
#build_collection ⇒ type
Returns a collection of the resource we are calling.
If you want your resource to return something interesting, you should override the resource_scope method. Otherwise by default it will return the constantized model name and it will call .all on it during presentation.
135 136 137 |
# File 'lib/cyrax/extensions/has_service.rb', line 135 def build_collection resource_scope end |
#build_resource(id, attributes = {}) ⇒ object
Instantiates the resource
97 98 99 100 101 102 103 104 105 |
# File 'lib/cyrax/extensions/has_service.rb', line 97 def build_resource(id, attributes = {}) if id.present? resource = find_resource(id) resource.attributes = attributes resource else resource_scope.new(default_resource_attributes.merge(attributes)) end end |
#create(custom_attributes = nil, &block) ⇒ Cyrax::Response Also known as: create!
Creates a new resource and persists to DB Used for :create action in controller
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cyrax/extensions/has_service.rb', line 31 def create(custom_attributes = nil, &block) resource = build_resource(nil, custom_attributes||resource_attributes) (:create, resource) transaction do if save_resource(resource) (:created) block.call(resource) if block_given? end end respond_with(resource) end |
#delete_resource(resource) ⇒ Object
Remove a resource Calls destroy method on resource
116 117 118 |
# File 'lib/cyrax/extensions/has_service.rb', line 116 def delete_resource(resource) resource.destroy end |
#destroy(&block) ⇒ Cyrax::Response Also known as: destroy!
Destroys a resource from the DB Used for :destroy action in controller
75 76 77 78 79 80 81 82 83 |
# File 'lib/cyrax/extensions/has_service.rb', line 75 def destroy(&block) resource = find_resource(resource_params_id) (:destroy, resource) transaction do delete_resource(resource) block.call(resource) if block_given? end respond_with(resource) end |
#find_resource(id) ⇒ object
Finds and returns a single item from the DB
89 90 91 |
# File 'lib/cyrax/extensions/has_service.rb', line 89 def find_resource(id) resource_scope.find(id) end |
#read(&block) ⇒ Cyrax::Response Also known as: read!, edit, edit!
Reads a single item from the DB Used for :show action in controller
47 48 49 50 51 |
# File 'lib/cyrax/extensions/has_service.rb', line 47 def read(&block) resource = find_resource(resource_params_id) block.call(resource) if block_given? respond_with resource end |
#read_all(&block) ⇒ Cyrax::Response Also known as: read_all!
Builds and returns a collection response for Rails
7 8 9 10 11 12 |
# File 'lib/cyrax/extensions/has_service.rb', line 7 def read_all(&block) (:read_all, resource_class) collection = build_collection block.call(collection) if block_given? respond_with collection, name: collection_name, present: :collection end |
#resource_params_id ⇒ Object
149 150 151 |
# File 'lib/cyrax/extensions/has_service.rb', line 149 def resource_params_id params[:id] end |
#save_resource(resource) ⇒ Object
Saves a resource
109 110 111 |
# File 'lib/cyrax/extensions/has_service.rb', line 109 def save_resource(resource) resource.save end |
#transaction(&block) ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/cyrax/extensions/has_service.rb', line 139 def transaction(&block) if defined?(ActiveRecord::Base) ActiveRecord::Base.transaction do block.call end else block.call end end |
#update(custom_attributes = nil, &block) ⇒ Cyrax::Response Also known as: update!
Updates a single item and persists to DB Used for :update action in controller
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/cyrax/extensions/has_service.rb', line 59 def update(custom_attributes = nil, &block) resource = build_resource(resource_params_id, custom_attributes||resource_attributes) (:update, resource) transaction do if save_resource(resource) (:updated) block.call(resource) if block_given? end end respond_with(resource) end |