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, skip_undefined: true, **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

  • skip_undefined (Boolean) (defaults to: true)

    whether we should skip routing undefined resourceful actions



55
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
# File 'lib/rest_framework/routers.rb', line 55

def _rest_resources(default_singular, name, skip_undefined: true, **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]

  # 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_undefined = kwargs.delete(:skip_undefined) || true
  skip = controller_class.get_skip_actions(skip_undefined: skip_undefined)
  public_send(resource_method, name, except: skip, **kwargs) do
    if controller_class.respond_to?(:extra_member_actions)
      member do
        actions = RESTFramework::Utils.parse_extra_actions(
          controller_class.extra_member_actions,
        )
        self._route_extra_actions(actions)
      end
    end

    collection do
      actions = RESTFramework::Utils.parse_extra_actions(controller_class.extra_actions)
      self._route_extra_actions(actions)
    end

    yield if block_given?
  end
end

#_route_extra_actions(actions, &block) ⇒ Object

Interal interface for routing extra actions.



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

def _route_extra_actions(actions, &block)
  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

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

Public interface for creating singular RESTful resource routes.



103
104
105
106
107
# File 'lib/rest_framework/routers.rb', line 103

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.



110
111
112
113
114
# File 'lib/rest_framework/routers.rb', line 110

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.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rest_framework/routers.rb', line 142

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.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rest_framework/routers.rb', line 117

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]

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

    self._route_extra_actions(actions, &block)
  end
end