Class: ActionDispatch::Routing::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rest_framework/routers.rb

Instance Method Summary collapse

Instance Method Details

#rest_resource(*names, **kwargs, &block) ⇒ Object

Public interface for creating singular RESTful resource routes.



131
132
133
134
135
# File 'lib/rest_framework/routers.rb', line 131

def rest_resource(*names, **kwargs, &block)
  names.each do |n|
    self._rest_resources(true, n, **kwargs, &block)
  end
end

#rest_resources(*names, **kwargs, &block) ⇒ Object

Public interface for creating plural RESTful resource routes.



138
139
140
141
142
# File 'lib/rest_framework/routers.rb', line 138

def rest_resources(*names, **kwargs, &block)
  names.each do |n|
    self._rest_resources(false, n, **kwargs, &block)
  end
end

#rest_root(name = nil, **kwargs, &block) ⇒ Object

Route a controller’s ‘#root` to ’/‘ in the current scope/namespace, along with other actions.

Parameters:

  • label (Symbol)

    the snake_case name of the controller



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rest_framework/routers.rb', line 170

def rest_root(name=nil, **kwargs, &block)
  # By default, use RootController#root.
  root_action = kwargs.delete(:action) || :root
  controller = kwargs.delete(:controller) || name || :root

  # Route the root.
  get name.to_s, controller: controller, action: root_action

  # Route any additional actions.
  controller_class = self._get_controller_class(controller, pluralize: false)
  actions = self._parse_extra_actions(controller_class.extra_actions)
  actions.each do |action_config|
    # Add :action unless kwargs defines it.
    unless action_config[:kwargs].key?(:action)
      action_config[:kwargs][:action] = action_config[:path]
    end

    action_config[:methods].each do |m|
      public_send(
        m,
        File.join(name.to_s, action_config[:path].to_s),
        controller: controller,
        **action_config[:kwargs],
      )
    end
    yield if block_given?
  end
end

#rest_route(name = nil, **kwargs, &block) ⇒ Object

Route a controller without the default resourceful paths.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rest_framework/routers.rb', line 145

def rest_route(name=nil, **kwargs, &block)
  controller = kwargs.delete(:controller) || name
  if controller.is_a?(Class)
    controller_class = controller
  else
    controller_class = self._get_controller_class(controller, pluralize: false)
  end

  # Set controller if it's not explicitly set.
  kwargs[:controller] = name unless kwargs[:controller]

  # Route actions using the resourceful router, but skip all builtin actions.
  actions = self._parse_extra_actions(controller_class.extra_actions)
  public_send(:resource, name, only: [], **kwargs) do
    actions.each do |action_config|
      action_config[:methods].each do |m|
        public_send(m, action_config[:path], **action_config[:kwargs])
      end
      yield if block_given?
    end
  end
end