Class: ActionDispatch::Routing::Mapper
- Inherits:
-
Object
- Object
- ActionDispatch::Routing::Mapper
- Defined in:
- lib/rest_framework/routers.rb
Instance Method Summary collapse
-
#_get_controller_class(name, pluralize: true, fallback_reverse_pluralization: true) ⇒ Object
Internal interface to get the controller class from the name and current scope.
-
#_rest_resources(default_singular, name, **kwargs, &block) ⇒ Object
Internal core implementation of the ‘rest_resource(s)` router, both singular and plural.
-
#_route_extra_actions(actions, &block) ⇒ Object
Internal interface for routing extra actions.
-
#rest_resource(*names, **kwargs, &block) ⇒ Object
Public interface for creating singular RESTful resource routes.
-
#rest_resources(*names, **kwargs, &block) ⇒ Object
Public interface for creating plural RESTful resource routes.
-
#rest_root(name = nil, **kwargs, &block) ⇒ Object
Route a controller’s ‘#root` to ’/‘ in the current scope/namespace, along with other actions.
-
#rest_route(name = nil, **kwargs, &block) ⇒ Object
Route a controller without the default resourceful paths.
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.
6 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 39 40 41 42 43 44 45 46 47 |
# File 'lib/rest_framework/routers.rb', line 6 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 reraise = false begin controller = mod.const_get(name_reverse) rescue NameError reraise = true end if reraise raise end else raise end end controller end |
#_rest_resources(default_singular, name, **kwargs, &block) ⇒ Object
Internal core implementation of the ‘rest_resource(s)` router, both singular and plural.
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/rest_framework/routers.rb', line 72 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. if !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, singular) 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| # Anchor the route since Rails 8.1 OPTIONS routes are non-anchored by default, which # causes parent OPTIONS routes to greedily intercept sub-path requests. public_send(m, "", action: action, anchor: true) if self.respond_to?(m) end end # Route bulk actions, if configured. These require a model and are gated by the `bulk` # attribute, and may be individually excluded via `excluded_actions`. if controller_class.model && controller_class.bulk bulk_exclude = controller_class.excluded_actions&.to_set || Set.new RESTFramework::RRF_BUILTIN_BULK_ACTIONS.each do |action, methods| next unless controller_class.method_defined?(action) next if bulk_exclude.include?(action) [ methods ].flatten.each do |m| # Anchor the route since Rails 8.1 OPTIONS routes are non-anchored by default, which # causes parent OPTIONS routes to greedily intercept sub-path requests. public_send(m, "", action: action, anchor: true) if self.respond_to?(m) end 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
Internal interface for routing extra actions.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rest_framework/routers.rb', line 50 def _route_extra_actions(actions, &block) parsed_actions = RESTFramework::Utils.parse_extra_actions(actions) parsed_actions.each do |action, config| config[:methods].each do |m| public_send(m, config[:path], action: action, **config[:kwargs]) end # Record that this route is an extra action and any metadata associated with it. = config[:metadata] key = "#{@scope[:path]}/#{config[:path]}" RESTFramework::EXTRA_ACTION_ROUTES.add(key) RESTFramework::ROUTE_METADATA[key] = if yield if block_given? end end |
#rest_resource(*names, **kwargs, &block) ⇒ Object
Public interface for creating singular RESTful resource routes.
146 147 148 149 150 |
# File 'lib/rest_framework/routers.rb', line 146 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.
153 154 155 156 157 |
# File 'lib/rest_framework/routers.rb', line 153 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.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/rest_framework/routers.rb', line 209 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 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.
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/rest_framework/routers.rb', line 160 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, as: "") 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| # Anchor the route since Rails 8.1 OPTIONS routes are non-anchored by default, which # causes parent OPTIONS routes to greedily intercept sub-path requests. public_send(m, "", action: action, anchor: true) 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 |