Class: ActionController::Routing::RouteSet

Inherits:
Object
  • Object
show all
Defined in:
lib/action_controller/routing.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initializeRouteSet

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

Raises:

  • (TypeError)


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

Yields:

  • (_self)

Yield Parameters:



321
322
323
324
# File 'lib/action_controller/routing.rb', line 321

def draw
  @routes.clear
  yield self
end

#eachObject



229
230
231
# File 'lib/action_controller/routing.rb', line 229

def each
  @routes.each {|route| yield route}
end

#empty?Boolean

Returns:

  • (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 expand_controller_path!(options, defaults)
  if options[:controller]
    if /^\// =~ options[:controller]
      options[:controller] = options[:controller][1..-1]
      defaults.clear # Sending to absolute controller implies fresh defaults
    else
      relative_to = defaults[:controller] ? defaults[:controller].split('/')[0..-2].join('/') : ''
      options[:controller] = relative_to.empty? ? options[:controller] : "#{relative_to}/#{options[:controller]}"
      defaults.delete(:action) if options.key?(:controller)
    end
  else
    options[: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

Raises:

  • (RoutingError)


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(options, request)
  raise RoutingError, "There are no routes defined!" if @routes.empty?

  options = options.symbolize_keys
  defaults = request.path_parameters.symbolize_keys
  if options.empty? then options = defaults.clone # Get back the current url if no options was passed
  else expand_controller_path!(options, defaults) # Expand the supplied controller path.
  end
  defaults.delete_if {|k, v| options.key?(k) && options[k].nil?} # Remove defaults that have been manually cleared using :name => nil

  failures = []
  selected = nil
  self.each do |route|
    path, unused = route.generate(options, 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 #{options.inspect}, defaults: #{defaults.inspect}", failures)
end

#recognize!(request) ⇒ Object

Recognize the provided path. Raise RoutingError if the path can’t be recognized.

Raises:

  • (RoutingError)


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, options = route.recognize(path)
    if controller.nil?
      failures << [route, options] if ActionController::Base.debug_routes
    else
      request.path_parameters = options
      return controller
    end
  end
  
  raise RoutingError.new("No route for path: #{path.join('/').inspect}", failures)
end

#reloadObject



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.new("Cannot load config/routes.rb:\n    #{e.message}").copy_blame!(e)
  ensure # Ensure that there is at least one route:
    connect(':controller/:action/:id', :action => 'index', :id => nil) if @routes.empty?
  end
end

#route(*args) ⇒ Object Also known as: connect



305
306
307
# File 'lib/action_controller/routing.rb', line 305

def route(*args)
  add_route(Route.new(*args))
end