class ActionDispatch::Routing::Mapper::Mapping
:nodoc:
def self.build(scope, set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, options)
def self.build(scope, set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, options) options = scope[:options].merge(options) if scope[:options] defaults = (scope[:defaults] || {}).dup scope_constraints = scope[:constraints] || {} new set, ast, defaults, controller, default_action, scope[:module], to, formatted, scope_constraints, scope[:blocks] || [], via, options_constraints, anchor, options end
def self.check_via(via)
def self.check_via(via) if via.empty? msg = "You should not use the `match` method in your router without specifying an HTTP method.\n" \ "If you want to expose your action to both GET and POST, add `via: [:get, :post]` option.\n" \ "If you want to expose your action to GET, use `get` in the router:\n" \ " Instead of: match \"controller#action\"\n" \ " Do: get \"controller#action\"" raise ArgumentError, msg end via end
def self.normalize_path(path, format)
def self.normalize_path(path, format) path = Mapper.normalize_path(path) if format == true "#{path}.:format" elsif optional_format?(path, format) "#{path}(.:format)" else path end end
def self.optional_format?(path, format)
def self.optional_format?(path, format) format != false && path !~ OPTIONAL_FORMAT_REGEX end
def add_controller_module(controller, modyoule)
def add_controller_module(controller, modyoule) if modyoule && !controller.is_a?(Regexp) if controller =~ %r{\A/} controller[1..-1] else [modyoule, controller].compact.join("/") end else controller end end
def add_wildcard_options(options, formatted, path_ast)
def add_wildcard_options(options, formatted, path_ast) # Add a constraint for wildcard route to make it non-greedy and match the # optional format part of the route by default if formatted != false path_ast.grep(Journey::Nodes::Star).each_with_object({}) { |node, hash| hash[node.name.to_sym] ||= /.+?/ }.merge options else options end end
def app(blocks)
def app(blocks) if to.respond_to?(:action) Routing::RouteSet::StaticDispatcher.new to elsif to.respond_to?(:call) Constraints.new(to, blocks, Constraints::CALL) elsif blocks.any? Constraints.new(dispatcher(defaults.key?(:controller)), blocks, Constraints::SERVE) else dispatcher(defaults.key?(:controller)) end end
def application
def application app(@blocks) end
def blocks(callable_constraint)
def blocks(callable_constraint) unless callable_constraint.respond_to?(:call) || callable_constraint.respond_to?(:matches?) raise ArgumentError, "Invalid constraint: #{callable_constraint.inspect} must respond to :call or :matches?" end [callable_constraint] end
def build_conditions(current_conditions, request_class)
def build_conditions(current_conditions, request_class) conditions = current_conditions.dup conditions.keep_if do |k, _| request_class.public_method_defined?(k) end end
def build_path(ast, requirements, anchor)
def build_path(ast, requirements, anchor) pattern = Journey::Path::Pattern.new(ast, requirements, JOINED_SEPARATORS, anchor) # Find all the symbol nodes that are adjacent to literal nodes and alter # the regexp so that Journey will partition them into custom routes. ast.find_all { |node| next unless node.cat? if node.left.literal? && node.right.symbol? symbol = node.right elsif node.left.literal? && node.right.cat? && node.right.left.symbol? symbol = node.right.left elsif node.left.symbol? && node.right.literal? symbol = node.left elsif node.left.symbol? && node.right.cat? && node.right.left.literal? symbol = node.left else next end if symbol symbol.regexp = /(?:#{Regexp.union(symbol.regexp, '-')})+/ end } pattern end
def check_controller_and_action(path_params, controller, action)
def check_controller_and_action(path_params, controller, action) hash = check_part(:controller, controller, path_params, {}) do |part| translate_controller(part) { message = "'#{part}' is not a supported controller name. This can lead to potential routing problems." message << " See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use" raise ArgumentError, message } end check_part(:action, action, path_params, hash) { |part| part.is_a?(Regexp) ? part : part.to_s } end
def check_part(name, part, path_params, hash)
def check_part(name, part, path_params, hash) if part hash[name] = yield(part) else unless path_params.include?(name) message = "Missing :#{name} key on routes definition, please check your routes." raise ArgumentError, message end end hash end
def conditions
def conditions build_conditions @conditions, @set.request_class end
def constraints(options, path_params)
def constraints(options, path_params) options.group_by do |key, option| if Regexp === option :constraints else if path_params.include?(key) :path_params else :required_defaults end end end end
def dispatcher(raise_on_name_error)
def dispatcher(raise_on_name_error) Routing::RouteSet::Dispatcher.new raise_on_name_error end
def initialize(set, ast, defaults, controller, default_action, modyoule, to, formatted, scope_constraints, blocks, via, options_constraints, anchor, options)
def initialize(set, ast, defaults, controller, default_action, modyoule, to, formatted, scope_constraints, blocks, via, options_constraints, anchor, options) @defaults = defaults @set = set @to = to @default_controller = controller @default_action = default_action @ast = ast @anchor = anchor @via = via @internal = options.delete(:internal) path_params = ast.find_all(&:symbol?).map(&:to_sym) options = add_wildcard_options(options, formatted, ast) options = normalize_options!(options, path_params, modyoule) split_options = constraints(options, path_params) constraints = scope_constraints.merge Hash[split_options[:constraints] || []] if options_constraints.is_a?(Hash) @defaults = Hash[options_constraints.find_all { |key, default| URL_OPTIONS.include?(key) && (String === default || Integer === default) }].merge @defaults @blocks = blocks constraints.merge! options_constraints else @blocks = blocks(options_constraints) end requirements, conditions = split_constraints path_params, constraints verify_regexp_requirements requirements.map(&:last).grep(Regexp) formats = normalize_format(formatted) @requirements = formats[:requirements].merge Hash[requirements] @conditions = Hash[conditions] @defaults = formats[:defaults].merge(@defaults).merge(normalize_defaults(options)) if path_params.include?(:action) && !@requirements.key?(:action) @defaults[:action] ||= "index" end @required_defaults = (split_options[:required_defaults] || []).map(&:first) end
def make_route(name, precedence)
def make_route(name, precedence) route = Journey::Route.new(name, application, path, conditions, required_defaults, defaults, request_method, precedence, @internal) route end
def normalize_defaults(options)
def normalize_defaults(options) Hash[options.reject { |_, default| Regexp === default }] end
def normalize_format(formatted)
def normalize_format(formatted) case formatted when true { requirements: { format: /.+/ }, defaults: {} } when Regexp { requirements: { format: formatted }, defaults: { format: nil } } when String { requirements: { format: Regexp.compile(formatted) }, defaults: { format: formatted } } else { requirements: {}, defaults: {} } end end
def normalize_options!(options, path_params, modyoule)
def normalize_options!(options, path_params, modyoule) if path_params.include?(:controller) raise ArgumentError, ":controller segment is not allowed within a namespace block" if modyoule # Add a default constraint for :controller path segments that matches namespaced # controllers with default routes like :controller/:action/:id(.:format), e.g: # GET /admin/products/show/1 # => { controller: 'admin/products', action: 'show', id: '1' } options[:controller] ||= /.+?/ end if to.respond_to?(:action) || to.respond_to?(:call) options else to_endpoint = split_to to controller = to_endpoint[0] || default_controller action = to_endpoint[1] || default_action controller = add_controller_module(controller, modyoule) options.merge! check_controller_and_action(path_params, controller, action) end end
def path
def path build_path @ast, requirements, @anchor end
def request_method
def request_method @via.map { |x| Journey::Route.verb_matcher(x) } end
def split_constraints(path_params, constraints)
def split_constraints(path_params, constraints) constraints.partition do |key, requirement| path_params.include?(key) || key == :controller end end
def split_to(to)
def split_to(to) if to =~ /#/ to.split("#") else [] end end
def translate_controller(controller)
def translate_controller(controller) return controller if Regexp === controller return controller.to_s if controller =~ /\A[a-z_0-9][a-z_0-9\/]*\z/ yield end
def verify_regexp_requirements(requirements)
def verify_regexp_requirements(requirements) requirements.each do |requirement| if requirement.source =~ ANCHOR_CHARACTERS_REGEX raise ArgumentError, "Regexp anchor characters are not allowed in routing requirements: #{requirement.inspect}" end if requirement.multiline? raise ArgumentError, "Regexp multiline option is not allowed in routing requirements: #{requirement.inspect}" end end end