Module: Cyrax::Extensions::HasService

Extended by:
ActiveSupport::Concern
Included in:
Resource
Defined in:
lib/cyrax/extensions/has_service.rb

Instance Method Summary collapse

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.

Parameters:

  • action (Symbol)

    The action to authorize

  • resource (object)

    The resource to authorize



94
95
96
# File 'lib/cyrax/extensions/has_service.rb', line 94

def authorize_resource!(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

Returns:



19
20
21
22
23
24
# 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

#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

Returns:



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)
  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

#destroy(&block) ⇒ Cyrax::Response Also known as: destroy!

Destroys a resource from the DB Used for :destroy action in controller

Returns:



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)
  authorize_resource!(: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

Returns:



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

Returns:



7
8
9
10
11
12
# 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

#resource_params_idObject



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

Returns:



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)
  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