Module: StandardAPI::RouteHelpers

Defined in:
lib/standard_api/route_helpers.rb

Instance Method Summary collapse

Instance Method Details

#standard_resource(*resource, &block) ⇒ Object

StandardAPI wrapper for ActionDispatch::Routing::Mapper::Resources#resource

Includes the following routes

GET /schema
GET /calculate

For example:

standard_resource :account

is equivilent to:

resource :account do
  get :schema, on: :collection
  get :calculate, on: :collection
end


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/standard_api/route_helpers.rb', line 77

def standard_resource(*resource, &block)
  options = resource.extract_options!.dup

  resource(*resource, options) do
    available_actions = if only = parent_resource.instance_variable_get(:@only)
      Array(only).map(&:to_sym)
    else
      if parent_resource.instance_variable_get(:@api_only)
        [:index, :create, :show, :update, :destroy]
      else
        [:index, :create, :new, :show, :update, :destroy, :edit]
      end + [ :schema, :calculate, :add_resource, :remove_resource ]
    end

    actions = if except = parent_resource.instance_variable_get(:@except)
      available_actions - Array(except).map(&:to_sym)
    else
      available_actions
    end

    get :schema, on: :collection if actions.include?(:schema)
    get :calculate, on: :collection if actions.include?(:calculate)

    if actions.include?(:add_resource)
      post ':relationship/:resource_id' => :add_resource, on: :member
    end

    if actions.include?(:remove_resource)
      delete ':relationship/:resource_id' => :remove_resource, on: :member
    end

    block.call if block
  end
end

#standard_resources(*resources, &block) ⇒ Object

StandardAPI wrapper for ActionDispatch::Routing::Mapper::Resources#resources

Includes the following routes

GET /schema
GET /calculate

For example

standard_resources :views

is equivilent to:

resources :api_keys do
  get :schema, on: :collection
  get :calculate, on: :collection
end


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/standard_api/route_helpers.rb', line 21

def standard_resources(*resources, &block)
  options = resources.extract_options!.dup

  resources(*resources, options) do
    block.call if block # custom routes take precedence over standardapi routes

    available_actions = if only = parent_resource.instance_variable_get(:@only)
      Array(only).map(&:to_sym)
    else
      if parent_resource.instance_variable_get(:@api_only)
        [:index, :create, :show, :update, :destroy]
      else
        [:index, :create, :new, :show, :update, :destroy, :edit]
      end + [ :schema, :calculate, :add_resource, :remove_resource, :create_resource ]
    end

    actions = if except = parent_resource.instance_variable_get(:@except)
      available_actions - Array(except).map(&:to_sym)
    else
      available_actions
    end

    get :schema, on: :collection if actions.include?(:schema)
    get :calculate, on: :collection if actions.include?(:calculate)

    if actions.include?(:add_resource)
      post ':relationship/:resource_id' => :add_resource, on: :member
    end
    
    if actions.include?(:create_resource)
      post ':relationship' => :create_resource, on: :member
    end

    if actions.include?(:remove_resource)
      delete ':relationship/:resource_id' => :remove_resource, on: :member
    end
  end
end