Class: ActionDispatch::Routing::Mapper

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

Instance Method Summary collapse

Instance Method Details

#_get_controller_class(name, pluralize: true, fallback_reverse_pluralization: true) ⇒ Object

Internal interface to get the controller class from the name and current scope.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rest_framework/routers.rb', line 7

def _get_controller_class(name, pluralize: true, fallback_reverse_pluralization: true)
  # Get class name.
  name = name.to_s.camelize  # Camelize to leave plural names plural.
  name = name.pluralize if pluralize
  if name == name.pluralize
    name_reverse = name.singularize
  else
    name_reverse = name.pluralize
  end
  name += "Controller"
  name_reverse += "Controller"

  # Get scope for the class.
  if @scope[:module]
    mod = @scope[:module].to_s.camelize.constantize
  else
    mod = Object
  end

  # Convert class name to class.
  begin
    controller = mod.const_get(name)
  rescue NameError
    if fallback_reverse_pluralization
      controller = mod.const_get(name_reverse)
    else
      raise
    end
  end

  return controller
end

#_rest_resources(default_singular, name, **kwargs, &block) ⇒ Object

Internal core implementation of the ‘rest_resource(s)` router, both singular and plural.

Parameters:

  • default_singular (Boolean)

    the default plurality of the resource if the plurality is not otherwise defined by the controller

  • name (Symbol)

    the resource name, from which path and controller are deduced by default



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rest_framework/routers.rb', line 56

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

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

  # Passing `unscoped: true` will prevent a nested resource from being scoped.
  unscoped = kwargs.delete(:unscoped)

  # Determine plural/singular resource.
  force_singular = kwargs.delete(:force_singular)
  force_plural = kwargs.delete(:force_plural)
  if force_singular
    singular = true
  elsif force_plural
    singular = false
  elsif !controller_class.singleton_controller.nil?
    singular = controller_class.singleton_controller
  else
    singular = default_singular
  end
  resource_method = singular ? :resource : :resources

  # Call either `resource` or `resources`, passing appropriate modifiers.
  skip = RESTFramework::Utils.get_skipped_builtin_actions(controller_class)
  public_send(resource_method, name, except: skip, **kwargs) do
    if controller_class.respond_to?(:extra_member_actions)
      member do
        self._route_extra_actions(controller_class.extra_member_actions)
      end
    end

    collection do
      # Route extra controller-defined actions.
      self._route_extra_actions(controller_class.extra_actions)

      # Route extra RRF-defined actions.
      RESTFramework::RRF_BUILTIN_ACTIONS.each do |action, methods|
        next unless controller_class.method_defined?(action)

        [methods].flatten.each do |m|
          public_send(m, "", action: action) if self.respond_to?(m)
        end
      end

      # Route bulk actions, if configured.
      RESTFramework::RRF_BUILTIN_BULK_ACTIONS.each do |action, methods|
        next unless controller_class.method_defined?(action)

        [methods].flatten.each do |m|
          public_send(m, "", action: action) if self.respond_to?(m)
        end
      end
    end

    if unscoped
      yield if block_given?
    else
      scope(module: name, as: name) do
        yield if block_given?
      end
    end
  end
end

#_route_extra_actions(actions, &block) ⇒ Object

Interal interface for routing extra actions.



41
42
43
44
45
46
47
48
49
50
# File 'lib/rest_framework/routers.rb', line 41

def _route_extra_actions(actions, &block)
  parsed_actions = RESTFramework::Utils.parse_extra_actions(actions)

  parsed_actions.each do |action, config|
    [config[:methods]].flatten.each do |m|
      public_send(m, config[:path], action: action, **(config[:kwargs] || {}))
    end
    yield if block_given?
  end
end

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

Public interface for creating singular RESTful resource routes.



127
128
129
130
131
# File 'lib/rest_framework/routers.rb', line 127

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.



134
135
136
137
138
# File 'lib/rest_framework/routers.rb', line 134

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.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rest_framework/routers.rb', line 188

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

  # Remove path if name is nil (routing to the root of current namespace).
  unless name
    kwargs[:path] = ""
  end

  return rest_route(controller, route_root_to: root_action, **kwargs) do
    yield if block_given?
  end
end

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

Route a controller without the default resourceful paths.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rest_framework/routers.rb', line 141

def rest_route(name=nil, **kwargs, &block)
  controller = kwargs.delete(:controller) || name
  route_root_to = kwargs.delete(:route_root_to)
  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]

  # Passing `unscoped: true` will prevent a nested resource from being scoped.
  unscoped = kwargs.delete(:unscoped)

  # Route actions using the resourceful router, but skip all builtin actions.
  public_send(:resource, name, only: [], **kwargs) do
    # Route a root for this resource.
    if route_root_to
      get("", action: route_root_to)
    end

    collection do
      # Route extra controller-defined actions.
      self._route_extra_actions(controller_class.extra_actions)

      # Route extra RRF-defined actions.
      RESTFramework::RRF_BUILTIN_ACTIONS.each do |action, methods|
        next unless controller_class.method_defined?(action)

        [methods].flatten.each do |m|
          public_send(m, "", action: action) if self.respond_to?(m)
        end
      end
    end

    if unscoped
      yield if block_given?
    else
      scope(module: name, as: name) do
        yield if block_given?
      end
    end
  end
end