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.
-
#create(custom_attributes = nil, &block) ⇒ Cyrax::Response
(also: #create!)
Creates a new resource and persists to DB Used for :create action in controller.
-
#destroy(&block) ⇒ Cyrax::Response
(also: #destroy!)
Destroys a resource from the DB Used for :destroy action in controller.
-
#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
- #set_resource_attributes(resource, attributes = {}) ⇒ Object
- #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.
94 95 96 |
# File 'lib/cyrax/extensions/has_service.rb', line 94 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
19 20 21 22 23 24 |
# File 'lib/cyrax/extensions/has_service.rb', line 19 def build(&block) resource = repository.build(nil) (:build, resource) block.call(resource) if block_given? respond_with resource 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
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cyrax/extensions/has_service.rb', line 30 def create(custom_attributes = nil, &block) resource = repository.build(nil) old_resource = resource.dup set_resource_attributes(resource, custom_attributes||resource_attributes) (:create, resource) transaction do if repository.save(resource) (:created) block.call(resource, old_resource) if block_given? end end respond_with(resource) end |
#destroy(&block) ⇒ Cyrax::Response Also known as: destroy!
Destroys a resource from the DB Used for :destroy action in controller
78 79 80 81 82 83 84 85 86 |
# File 'lib/cyrax/extensions/has_service.rb', line 78 def destroy(&block) resource = repository.find(resource_params_id) (:destroy, resource) transaction do repository.delete(resource) block.call(resource) if block_given? end respond_with(resource) end |
#read(&block) ⇒ Cyrax::Response Also known as: read!, edit, edit!
Reads a single item from the DB Used for :show action in controller
48 49 50 51 52 |
# File 'lib/cyrax/extensions/has_service.rb', line 48 def read(&block) resource = repository.find(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 = repository.find_all block.call(collection) if block_given? respond_with collection, name: collection_name, present: :collection end |
#resource_params_id ⇒ Object
108 109 110 |
# File 'lib/cyrax/extensions/has_service.rb', line 108 def resource_params_id params[:id] end |
#set_resource_attributes(resource, attributes = {}) ⇒ Object
112 113 114 |
# File 'lib/cyrax/extensions/has_service.rb', line 112 def set_resource_attributes(resource, attributes = {}) resource.attributes = attributes end |
#transaction(&block) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/cyrax/extensions/has_service.rb', line 98 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
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cyrax/extensions/has_service.rb', line 60 def update(custom_attributes = nil, &block) resource = repository.build(resource_params_id) old_resource = resource.dup set_resource_attributes(resource, custom_attributes||resource_attributes) (:update, resource) transaction do if repository.save(resource) (:updated) block.call(resource, old_resource) if block_given? end end respond_with(resource) end |