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



125
126
127
# File 'lib/cyrax/extensions/has_service.rb', line 125

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:



20
21
22
23
24
25
# File 'lib/cyrax/extensions/has_service.rb', line 20

def build(&block)
  resource = build_resource(nil)
  authorize_resource!(:build, resource)
  block.call(resource) if block_given?
  respond_with resource
end

#build_collectiontype

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.

Returns:

  • (type)

    The collection



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

Parameters:

  • id (int)

    ID or nil if you want a new object

  • attributes (hash) (defaults to: {})

    Attributes you want to add to the resource

Returns:

  • (object)

    The object



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

Returns:



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)
  authorize_resource!(:create, resource)
  transaction do
    if save_resource(resource)
      set_message(: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

Parameters:

  • resource (object)

    The resource to destroy



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

Returns:



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

Parameters:

  • id (int)

    ID of item

Returns:

  • (object)

    The object



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

Returns:



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

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 = build_collection
  block.call(collection) if block_given?
  respond_with collection, name: collection_name, present: :collection
end

#resource_params_idObject



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

Parameters:

  • resource (object)

    The resource to save



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

Returns:



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)
  authorize_resource!(:update, resource)
  transaction do
    if save_resource(resource)
      set_message(:updated)
      block.call(resource) if block_given?
    end
  end
  respond_with(resource)
end