Class: Gizmo::Make

Inherits:
BaseOperation show all
Defined in:
lib/gizmo/make.rb

Instance Attribute Summary

Attributes inherited from BaseOperation

#context

Instance Method Summary collapse

Methods inherited from BaseOperation

#create_response, #initialize, #set_response_headers

Constructor Details

This class inherits a constructor from Gizmo::BaseOperation

Instance Method Details

#call(criteria, attrs, location_proc = nil) ⇒ Gizmo::Response

Make a single item using the provided criteria.

Parameters:

  • criteria (Mongoid::Criteria)

    the criteria to use for making a single item

  • attrs (Hash)

    the attributes used to make a new instance

  • location_proc (Proc) (defaults to: nil)

    a Proc that will be invoked with the ID of the newly created item, defaults to nil.

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gizmo/make.rb', line 13

def call(criteria, attrs, location_proc=nil)

  response = create_response
  item = criteria.create!(attrs)
  response.data = item

  if context.gizmo.respond_to?(:url_for)
    if location_proc.nil?
      url = context.gizmo.url_for item
    else
      url = location_proc.call(item.id)
    end

    response.header :Location, url unless url.nil?
  end
  response
end

#default_statusObject



31
32
33
# File 'lib/gizmo/make.rb', line 31

def default_status
  201
end