class ActionDispatch::Routing::RouteSet
Constants
- RESERVED_OPTIONS
Attributes
_routes[R]
default_scope[RW]
default_url_options[RW]
disable_clear_and_finalize[RW]
formatter[RW]
named_routes[RW]
request_class[RW]
resources_path_names[RW]
router[RW]
routes[RW]
set[RW]
Public Class Methods
default_resources_path_names()
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 377 def self.default_resources_path_names { :new => 'new', :edit => 'edit' } end
new(request_class = ActionDispatch::Request)
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 381 def initialize(request_class = ActionDispatch::Request) self.named_routes = NamedRouteCollection.new self.resources_path_names = self.class.default_resources_path_names self.default_url_options = {} self.request_class = request_class @append = [] @prepend = [] @disable_clear_and_finalize = false @finalized = false @set = Journey::Routes.new @router = Journey::Router.new @set @formatter = Journey::Formatter.new @set end
url_options()
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 487 def url_options; {}; end
Public Instance Methods
add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true)
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 532 def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true) raise ArgumentError, "Invalid route name: '#{name}'" unless name.blank? || name.to_s.match(/^[_a-z]\w*$/i) if name && named_routes[name] raise ArgumentError, "Invalid route name, already in use: '#{name}' \n" "You may have defined two routes with the same name using the `:as` option, or " "you may be overriding a route already defined by a resource with the same naming. " "For the latter, you can restrict the routes created with `resources` as explained here: \n" "http://guides.rubyonrails.org/routing.html#restricting-the-routes-created" end path = conditions.delete :path_info ast = conditions.delete :parsed_path_info path = build_path(path, ast, requirements, anchor) conditions = build_conditions(conditions, path.names.map { |x| x.to_sym }) route = @set.add_route(app, path, conditions, defaults, name) named_routes[name] = route if name route end
append(&block)
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 404 def append(&block) @append << block end
call(env)
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 797 def call(env) req = request_class.new(env) req.path_info = Journey::Router::Utils.normalize_path(req.path_info) @router.serve(req) end
clear!()
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 432 def clear! @finalized = false named_routes.clear set.clear formatter.clear @prepend.each { |blk| eval_block(blk) } end
define_mounted_helper(name)
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 457 def define_mounted_helper(name) return if MountedHelpers.method_defined?(name) routes = self MountedHelpers.class_eval do define_method "_#{name}" do RoutesProxy.new(routes, _routes_context) end end MountedHelpers.class_eval(" def #{name} @_#{name} ||= _#{name} end ", __FILE__, __LINE__ + 1) end
dispatcher(defaults)
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 440 def dispatcher(defaults) Routing::RouteSet::Dispatcher.new(defaults) end
draw(&block)
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 397 def draw(&block) clear! unless @disable_clear_and_finalize eval_block(block) finalize! unless @disable_clear_and_finalize nil end
empty?()
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 528 def empty? routes.empty? end
extra_keys(options, recall={})
click to toggle source
Generate the path indicated by the arguments, and return an array of the keys that were not used to generate it.
# File lib/action_dispatch/routing/route_set.rb, line 728 def extra_keys(options, recall={}) generate_extras(options, recall).last end
finalize!()
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 426 def finalize! return if @finalized @append.each { |blk| eval_block(blk) } @finalized = true end
find_script_name(options)
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 751 def find_script_name(options) options.delete(:script_name) || '' end
generate_extras(options, recall={})
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 732 def generate_extras(options, recall={}) route_key = options.delete :use_route path, params = generate(route_key, options, recall) return path, params.keys end
mounted_helpers()
click to toggle source
Contains all the mounted helpers across different engines and the `main_app` helper for the application. You can include this in your classes if you want to access routes for other engines.
# File lib/action_dispatch/routing/route_set.rb, line 453 def mounted_helpers MountedHelpers end
optimize_routes_generation?()
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 747 def optimize_routes_generation? default_url_options.empty? end
prepend(&block)
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 408 def prepend(&block) @prepend << block end
recognize_path(path, environment = {})
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 803 def recognize_path(path, environment = {}) method = (environment[:method] || "GET").to_s.upcase path = Journey::Router::Utils.normalize_path(path) unless path =~ %r{://} extras = environment[:extras] || {} begin env = Rack::MockRequest.env_for(path, {:method => method}) rescue URI::InvalidURIError => e raise ActionController::RoutingError, e.message end req = request_class.new(env) @router.recognize(req) do |route, params| params.merge!(extras) params.each do |key, value| if value.is_a?(String) value = value.dup.force_encoding(Encoding::BINARY) params[key] = URI.parser.unescape(value) end end old_params = req.path_parameters req.path_parameters = old_params.merge params app = route.app if app.matches?(req) && app.dispatcher? dispatcher = app.app if dispatcher.controller(params, false) dispatcher.prepare_params!(params) return params else raise ActionController::RoutingError, "A route matches #{path.inspect}, but references missing controller: #{params[:controller].camelize}Controller" end end end raise ActionController::RoutingError, "No route matches #{path.inspect}" end
url_for(options, route_name = nil, url_strategy = UNKNOWN)
click to toggle source
The options
argument must be a hash whose keys are
symbols.
# File lib/action_dispatch/routing/route_set.rb, line 760 def url_for(options, route_name = nil, url_strategy = UNKNOWN) options = default_url_options.merge options user = password = nil if options[:user] && options[:password] user = options.delete :user password = options.delete :password end recall = options.delete(:_recall) { {} } original_script_name = options.delete(:original_script_name) script_name = find_script_name options if original_script_name script_name = original_script_name + script_name end path_options = options.dup RESERVED_OPTIONS.each { |ro| path_options.delete ro } path, params = generate(route_name, path_options, recall) if options.key? :params params.merge! options[:params] end options[:path] = path options[:script_name] = script_name options[:params] = params options[:user] = user options[:password] = password url_strategy.call options end
url_helpers(supports_path = true)
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 474 def url_helpers(supports_path = true) routes = self Module.new do extend ActiveSupport::Concern include UrlFor # Define url_for in the singleton level so one can do: # Rails.application.routes.url_helpers.url_for(args) @_routes = routes class << self delegate :url_for, :optimize_routes_generation?, to: '@_routes' attr_reader :_routes def url_options; {}; end end url_helpers = routes.named_routes.url_helpers_module # Make named_routes available in the module singleton # as well, so one can do: # Rails.application.routes.url_helpers.posts_path extend url_helpers # Any class that includes this module will get all # named routes... include url_helpers if supports_path path_helpers = routes.named_routes.path_helpers_module else path_helpers = routes.named_routes.path_helpers_module(true) end include path_helpers extend path_helpers # plus a singleton class method called _routes ... included do singleton_class.send(:redefine_method, :_routes) { routes } end # And an instance method _routes. Note that # UrlFor (included in this module) add extra # conveniences for working with @_routes. define_method(:_routes) { @_routes || routes } define_method(:_generate_paths_by_default) do supports_path end private :_generate_paths_by_default end end
Private Instance Methods
build_conditions(current_conditions, path_values)
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 584 def build_conditions(current_conditions, path_values) conditions = current_conditions.dup # Rack-Mount requires that :request_method be a regular expression. # :request_method represents the HTTP verb that matches this route. # # Here we munge values before they get sent on to rack-mount. verbs = conditions[:request_method] || [] unless verbs.empty? conditions[:request_method] = %r[^#{verbs.join('|')}$] end conditions.keep_if do |k, _| k == :action || k == :controller || k == :required_defaults || @request_class.public_method_defined?(k) || path_values.include?(k) end end
build_path(path, ast, requirements, anchor)
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 553 def build_path(path, ast, requirements, anchor) strexp = Journey::Router::Strexp.new( ast, path, requirements, SEPARATORS, anchor) pattern = Journey::Path::Pattern.new(strexp) builder = Journey::GTG::Builder.new pattern.spec # Get all the symbol nodes followed by literals that are not the # dummy node. symbols = pattern.spec.grep(Journey::Nodes::Symbol).find_all { |n| builder.followpos(n).first.literal? } # Get all the symbol nodes preceded by literals. symbols.concat pattern.spec.find_all(&:literal?).map { |n| builder.followpos(n).first }.find_all(&:symbol?) symbols.each { |x| x.regexp = /(?:#{Regexp.union(x.regexp, '-')})+/ } pattern end
eval_block(block)
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 412 def eval_block(block) if block.arity == 1 raise "You are using the old router DSL which has been removed in Rails 3.1. " << "Please check how to update your routes file at: http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/" end mapper = Mapper.new(self) if default_scope mapper.with_default_scope(default_scope, &block) else mapper.instance_exec(&block) end end
generate(route_key, options, recall = {})
click to toggle source
# File lib/action_dispatch/routing/route_set.rb, line 738 def generate(route_key, options, recall = {}) Generator.new(route_key, options, recall, self).generate end