aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/routing/mapper.rb
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib/action_dispatch/routing/mapper.rb')
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb71
1 files changed, 28 insertions, 43 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 00fe350276..887b5957df 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -61,16 +61,9 @@ module ActionDispatch
attr_reader :requirements, :conditions, :defaults
attr_reader :to, :default_controller, :default_action, :as, :anchor
- def self.build(scope, set, path, as, controller, default_action, to, via, options)
- formatted = options.delete(:format) { scope.mapping_option(:format) }
-
+ def self.build(scope, set, path, as, controller, default_action, to, via, formatted, options)
options = scope[:options].merge(options) if scope[:options]
- options.delete :shallow_path
- options.delete :shallow_prefix
- options.delete :shallow
- options.delete :format
-
defaults = (scope[:defaults] || {}).dup
scope_constraints = scope[:constraints] || {}
@@ -128,14 +121,14 @@ module ActionDispatch
@requirements = formats[:requirements].merge Hash[requirements]
@conditions = Hash[conditions]
- @defaults = formats[:defaults].merge @defaults
+ @defaults = formats[:defaults].merge(@defaults).merge(normalize_defaults(options))
@conditions[:required_defaults] = (split_options[:required_defaults] || []).map(&:first)
@conditions[:path_info] = path
@conditions[:parsed_path_info] = ast
-
- add_request_method(via, @conditions)
- normalize_defaults!(options)
+ unless via == [:all]
+ @conditions[:request_method] = via.map { |m| m.to_s.dasherize.upcase }
+ end
end
def to_route
@@ -226,17 +219,8 @@ module ActionDispatch
end
end
- def normalize_defaults!(options)
- options.each_pair do |key, default|
- unless Regexp === default
- @defaults[key] = default
- end
- end
- end
-
- def add_request_method(via, conditions)
- return if via == [:all]
- conditions[:request_method] = via.map { |m| m.to_s.dasherize.upcase }
+ def normalize_defaults(options)
+ Hash[options.reject { |_, default| Regexp === default }]
end
def app(blocks)
@@ -810,10 +794,10 @@ module ActionDispatch
elsif option == :options
value = options
else
- value = options.delete(option)
+ value = options.delete(option) { POISON }
end
- if value
+ unless POISON == value
scope[option] = send("merge_#{option}_scope", @scope[option], value)
end
end
@@ -825,6 +809,8 @@ module ActionDispatch
@scope = @scope.parent
end
+ POISON = Object.new # :nodoc:
+
# Scopes routes to a specific controller
#
# controller "food" do
@@ -994,6 +980,10 @@ module ActionDispatch
child
end
+ def merge_format_scope(parent, child) #:nodoc:
+ child
+ end
+
def merge_path_names_scope(parent, child) #:nodoc:
merge_options_scope(parent, child)
end
@@ -1551,29 +1541,30 @@ module ActionDispatch
via = Mapping.check_via Array(options.delete(:via) {
@scope[:via]
})
+ formatted = options.delete(:format) { @scope[:format] }
path_types = paths.group_by(&:class)
path_types.fetch(String, []).each do |_path|
route_options = options.dup
- process_path(route_options, controller, _path, option_path || _path, to, via)
+ process_path(route_options, controller, _path, option_path || _path, to, via, formatted)
end
path_types.fetch(Symbol, []).each do |action|
route_options = options.dup
- decomposed_match(action, controller, route_options, option_path, to, via)
+ decomposed_match(action, controller, route_options, option_path, to, via, formatted)
end
self
end
- def process_path(options, controller, path, option_path, to, via)
+ def process_path(options, controller, path, option_path, to, via, formatted)
path_without_format = path.sub(/\(\.:format\)$/, '')
if using_match_shorthand?(path_without_format, to, options[:action])
to ||= path_without_format.gsub(%r{^/}, "").sub(%r{/([^/]*)$}, '#\1')
to.tr!("-", "_")
end
- decomposed_match(path, controller, options, option_path, to, via)
+ decomposed_match(path, controller, options, option_path, to, via, formatted)
end
def using_match_shorthand?(path, to, action)
@@ -1582,22 +1573,22 @@ module ActionDispatch
path =~ %r{^/?[-\w]+/[-\w/]+$}
end
- def decomposed_match(path, controller, options, _path, to, via) # :nodoc:
+ def decomposed_match(path, controller, options, _path, to, via, formatted) # :nodoc:
if on = options.delete(:on)
- send(on) { decomposed_match(path, controller, options, _path, to, via) }
+ send(on) { decomposed_match(path, controller, options, _path, to, via, formatted) }
else
case @scope.scope_level
when :resources
- nested { decomposed_match(path, controller, options, _path, to, via) }
+ nested { decomposed_match(path, controller, options, _path, to, via, formatted) }
when :resource
- member { decomposed_match(path, controller, options, _path, to, via) }
+ member { decomposed_match(path, controller, options, _path, to, via, formatted) }
else
- add_route(path, controller, options, _path, to, via)
+ add_route(path, controller, options, _path, to, via, formatted)
end
end
end
- def add_route(action, controller, options, _path, to, via) # :nodoc:
+ def add_route(action, controller, options, _path, to, via, formatted) # :nodoc:
path = path_for_action(action, _path)
raise ArgumentError, "path is required" if path.blank?
@@ -1617,7 +1608,7 @@ module ActionDispatch
name_for_action(options.delete(:as), action)
end
- mapping = Mapping.build(@scope, @set, URI.parser.escape(path), as, controller, default_action, to, via, options)
+ mapping = Mapping.build(@scope, @set, URI.parser.escape(path), as, controller, default_action, to, via, formatted, options)
app, conditions, requirements, defaults, as, anchor = mapping.to_route
@set.add_route(app, conditions, requirements, defaults, as, anchor)
end
@@ -1943,7 +1934,7 @@ module ActionDispatch
class Scope # :nodoc:
OPTIONS = [:path, :shallow_path, :as, :shallow_prefix, :module,
:controller, :action, :path_names, :constraints,
- :shallow, :blocks, :defaults, :via, :options]
+ :shallow, :blocks, :defaults, :via, :format, :options]
RESOURCE_SCOPES = [:resource, :resources]
RESOURCE_METHOD_SCOPES = [:collection, :member, :new]
@@ -1993,12 +1984,6 @@ module ActionDispatch
OPTIONS
end
- def mapping_option(name)
- options = self[:options]
- return unless options
- options[name]
- end
-
def new(hash)
self.class.new hash, self, scope_level
end