Class: ActionController::Routing::RouteSet
- Defined in:
- lib/action_controller/routing.rb
Overview
:nodoc:
Instance Method Summary collapse
- #add_route(route) ⇒ Object
- #draw {|_self| ... } ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #expand_controller_path!(options, defaults) ⇒ Object
-
#generate(options, request) ⇒ Object
Generate a path for the provided options Returns the path as an array of components and a hash of unused names Raises RoutingError if not route can handle the provided components.
-
#initialize ⇒ RouteSet
constructor
A new instance of RouteSet.
-
#recognize!(request) ⇒ Object
Recognize the provided path.
- #reload ⇒ Object
- #route(*args) ⇒ Object (also: #connect)
Constructor Details
#initialize ⇒ RouteSet
Returns a new instance of RouteSet.
218 219 220 |
# File 'lib/action_controller/routing.rb', line 218 def initialize @routes = [] end |
Instance Method Details
#add_route(route) ⇒ Object
222 223 224 225 |
# File 'lib/action_controller/routing.rb', line 222 def add_route(route) raise TypeError, "#{route.inspect} is not a Route instance!" unless route.kind_of?(Route) @routes << route end |
#draw {|_self| ... } ⇒ Object
321 322 323 324 |
# File 'lib/action_controller/routing.rb', line 321 def draw @routes.clear yield self end |
#each ⇒ Object
229 230 231 |
# File 'lib/action_controller/routing.rb', line 229 def each @routes.each {|route| yield route} end |
#empty? ⇒ Boolean
226 227 228 |
# File 'lib/action_controller/routing.rb', line 226 def empty? @routes.empty? end |
#expand_controller_path!(options, defaults) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/action_controller/routing.rb', line 290 def (, defaults) if [:controller] if /^\// =~ [:controller] [:controller] = [:controller][1..-1] defaults.clear # Sending to absolute controller implies fresh defaults else relative_to = defaults[:controller] ? defaults[:controller].split('/')[0..-2].join('/') : '' [:controller] = relative_to.empty? ? [:controller] : "#{relative_to}/#{[:controller]}" defaults.delete(:action) if .key?(:controller) end else [:controller] = defaults[:controller] end end |
#generate(options, request) ⇒ Object
Generate a path for the provided options Returns the path as an array of components and a hash of unused names Raises RoutingError if not route can handle the provided components.
Note that we don’t return the first generated path. We do this so that when a route generates a path from a subset of the available options we can keep looking for a route which can generate a path that uses more options. Note that we do return immediately if
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/action_controller/routing.rb', line 241 def generate(, request) raise RoutingError, "There are no routes defined!" if @routes.empty? = .symbolize_keys defaults = request.path_parameters.symbolize_keys if .empty? then = defaults.clone # Get back the current url if no options was passed else (, defaults) # Expand the supplied controller path. end defaults.delete_if {|k, v| .key?(k) && [k].nil?} # Remove defaults that have been manually cleared using :name => nil failures = [] selected = nil self.each do |route| path, unused = route.generate(, defaults) if path.nil? failures << [route, unused] if ActionController::Base.debug_routes else return path, unused if unused.empty? # Found a perfect route -- we're finished. if selected.nil? || unused.length < selected.last.length failures << [selected.first, "A better url than #{selected[1]} was found."] if selected selected = [route, path, unused] end end end return selected[1..-1] unless selected.nil? raise RoutingError.new("Generation failure: No route for url_options #{.inspect}, defaults: #{defaults.inspect}", failures) end |
#recognize!(request) ⇒ Object
Recognize the provided path. Raise RoutingError if the path can’t be recognized.
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/action_controller/routing.rb', line 272 def recognize!(request) path = ((%r{^/?(.*)/?$} =~ request.path) ? $1 : request.path).split('/') raise RoutingError, "There are no routes defined!" if @routes.empty? failures = [] self.each do |route| controller, = route.recognize(path) if controller.nil? failures << [route, ] if ActionController::Base.debug_routes else request.path_parameters = return controller end end raise RoutingError.new("No route for path: #{path.join('/').inspect}", failures) end |
#reload ⇒ Object
310 311 312 313 314 315 316 317 318 319 |
# File 'lib/action_controller/routing.rb', line 310 def reload begin route_file = defined?(RAILS_ROOT) ? File.join(RAILS_ROOT, 'config', 'routes') : nil require_dependency(route_file) if route_file rescue LoadError, ScriptError => e raise RoutingError, "Cannot load config/routes.rb:\n #{e.}" ensure # Ensure that there is at least one route: connect(':controller/:action/:id', :action => 'index', :id => nil) if @routes.empty? end end |