module Cyrax::Extensions::HasService

Constants

VALIDATION_ERROR_STATUS

Public Instance Methods

authorize_resource!(action, resource) click to toggle source

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. @param action [Symbol] The action to authorize @param resource [object] The resource to authorize

# File lib/cyrax/extensions/has_service.rb, line 130
def authorize_resource!(action, resource)
  # raise AuthorizationError
end
build(&block) click to toggle source

Builds a new resource without saving to DB Runs Model.new (before saving) Used for :new action in controller @return [Cyrax::Response] response

# File lib/cyrax/extensions/has_service.rb, line 21
def build(&block)
  resource = build_resource(nil)
  authorize_resource!(:build, resource)
  block.call(resource) if block_given?
  respond_with resource
end
Also aliased as: build!
build!(&block)
Alias for: build
build_collection() click to toggle source

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.

@return [type] The collection

# File lib/cyrax/extensions/has_service.rb, line 140
def build_collection
  resource_scope
end
build_resource(id, attributes = {}) click to toggle source

Instantiates the resource @param id [int] ID or nil if you want a new object @param attributes [hash] Attributes you want to add to the resource @return [object] The object

# File lib/cyrax/extensions/has_service.rb, line 102
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) click to toggle source

Creates a new resource and persists to DB Used for :create action in controller @return [Cyrax::Response] response

# File lib/cyrax/extensions/has_service.rb, line 32
def create(custom_attributes = nil, &block)
  resource = build_resource(nil, custom_attributes||resource_attributes)
  authorize_resource!(:create, resource)
  transaction do
    if save_resource(resource)
      set_message(:created)
      block.call(resource) if block_given?
    elsif Cyrax.automatically_set_invalid_status
      set_status VALIDATION_ERROR_STATUS
    end
  end
  respond_with(resource)
end
Also aliased as: create!
create!(custom_attributes = nil, &block)
Alias for: create
delete_resource(resource) click to toggle source

Remove a resource Calls destroy method on resource @param resource [object] The resource to destroy

# File lib/cyrax/extensions/has_service.rb, line 121
def delete_resource(resource)
  resource.destroy
end
destroy(&block) click to toggle source

Destroys a resource from the DB Used for :destroy action in controller @return [Cyrax::Response] response

# File lib/cyrax/extensions/has_service.rb, line 80
def destroy(&block)
  resource = find_resource(resource_params_id)
  authorize_resource!(:destroy, resource)
  transaction do
    delete_resource(resource)
    block.call(resource) if block_given?
  end
  respond_with(resource)
end
Also aliased as: destroy!
destroy!(&block)
Alias for: destroy
edit(&block)
Alias for: read
edit!(&block)
Alias for: read
find_resource(id) click to toggle source

Finds and returns a single item from the DB @param id [int] ID of item @return [object] The object

# File lib/cyrax/extensions/has_service.rb, line 94
def find_resource(id)
  resource_scope.find(id)
end
read(&block) click to toggle source

Reads a single item from the DB Used for :show action in controller @return [Cyrax::Response] response

# File lib/cyrax/extensions/has_service.rb, line 50
def read(&block)
  resource = find_resource(resource_params_id)
  block.call(resource) if block_given?
  respond_with resource
end
Also aliased as: read!, edit, edit!
read!(&block)
Alias for: read
read_all(&block) click to toggle source

Builds and returns a collection response for Rails @return [Cyrax::Response] response

# File lib/cyrax/extensions/has_service.rb, line 8
def read_all(&block)
  authorize_resource!(:read_all, resource_class)
  collection = build_collection
  block.call(collection) if block_given?
  respond_with collection, name: collection_name, present: :collection
end
Also aliased as: read_all!
read_all!(&block)

Overrides collection with read_all

Alias for: read_all
resource_params_id() click to toggle source
# File lib/cyrax/extensions/has_service.rb, line 154
def resource_params_id
  params[:id]
end
save_resource(resource) click to toggle source

Saves a resource @param resource [object] The resource to save

# File lib/cyrax/extensions/has_service.rb, line 114
def save_resource(resource)
  resource.save
end
transaction(&block) click to toggle source
# File lib/cyrax/extensions/has_service.rb, line 144
def transaction(&block)
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.transaction do
      block.call
    end
  else
    block.call
  end
end
update(custom_attributes = nil, &block) click to toggle source

Updates a single item and persists to DB Used for :update action in controller @return [Cyrax::Response] response

# File lib/cyrax/extensions/has_service.rb, line 62
def update(custom_attributes = nil, &block)
  resource = build_resource(resource_params_id, custom_attributes||resource_attributes)
  authorize_resource!(:update, resource)
  transaction do
    if save_resource(resource)
      set_message(:updated)
      block.call(resource) if block_given?
    elsif Cyrax.automatically_set_invalid_status
      set_status VALIDATION_ERROR_STATUS
    end
  end
  respond_with(resource)
end
Also aliased as: update!
update!(custom_attributes = nil, &block)
Alias for: update