module Cyrax::Extensions::HasService

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 94
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 19
def build(&block)
  resource = repository.build(nil)
  authorize_resource!(:build, resource)
  block.call(resource) if block_given?
  respond_with resource
end
Also aliased as: build!
build!(&block)
Alias for: build
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 30
def create(custom_attributes = nil, &block)
  resource = repository.build(nil)
  old_resource = resource.dup
  set_resource_attributes(resource, custom_attributes||resource_attributes)
  authorize_resource!(:create, resource)
  transaction do
    if repository.save(resource)
      set_message(:created)
      block.call(resource, old_resource) if block_given?
    end
  end
  respond_with(resource)
end
Also aliased as: create!
create!(custom_attributes = nil, &block)
Alias for: create
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 78
def destroy(&block)
  resource = repository.find(resource_params_id)
  authorize_resource!(:destroy, resource)
  transaction do
    repository.delete(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
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 48
def read(&block)
  resource = repository.find(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 7
def read_all(&block)
  authorize_resource!(:read_all, resource_class)
  collection = repository.find_all
  block.call(collection) if block_given?
  respond_with collection, name: collection_name, present: :collection
end
Also aliased as: read_all!
read_all!(&block)
Alias for: read_all
resource_params_id() click to toggle source
# File lib/cyrax/extensions/has_service.rb, line 108
def resource_params_id
  params[:id]
end
set_resource_attributes(resource, attributes = {}) click to toggle source
# File lib/cyrax/extensions/has_service.rb, line 112
def set_resource_attributes(resource, attributes = {})
  resource.attributes = attributes
end
transaction(&block) click to toggle source
# 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) 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 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)
  authorize_resource!(:update, resource)
  transaction do
    if repository.save(resource)
      set_message(:updated)
      block.call(resource, old_resource) if block_given?
    end
  end
  respond_with(resource)
end
Also aliased as: update!
update!(custom_attributes = nil, &block)
Alias for: update