1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

pull formatted up the stack

this reduces the number of times we have to mutate the options hash.
This commit is contained in:
Aaron Patterson 2015-08-12 16:29:33 -07:00
parent d61e3c79dc
commit ec895189e4
2 changed files with 23 additions and 15 deletions

View file

@ -61,9 +61,7 @@ module ActionDispatch
attr_reader :requirements, :conditions, :defaults attr_reader :requirements, :conditions, :defaults
attr_reader :to, :default_controller, :default_action, :as, :anchor attr_reader :to, :default_controller, :default_action, :as, :anchor
def self.build(scope, set, path, as, controller, default_action, to, via, options) def self.build(scope, set, path, as, controller, default_action, to, via, formatted, options)
formatted = options.delete(:format) { scope.mapping_option(:format) }
options = scope[:options].merge(options) if scope[:options] options = scope[:options].merge(options) if scope[:options]
options.delete :shallow_path options.delete :shallow_path
@ -1551,29 +1549,30 @@ module ActionDispatch
via = Mapping.check_via Array(options.delete(:via) { via = Mapping.check_via Array(options.delete(:via) {
@scope[:via] @scope[:via]
}) })
formatted = options.delete(:format) { @scope.mapping_option(:format) }
path_types = paths.group_by(&:class) path_types = paths.group_by(&:class)
path_types.fetch(String, []).each do |_path| path_types.fetch(String, []).each do |_path|
route_options = options.dup 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 end
path_types.fetch(Symbol, []).each do |action| path_types.fetch(Symbol, []).each do |action|
route_options = options.dup 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 end
self self
end 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\)$/, '') path_without_format = path.sub(/\(\.:format\)$/, '')
if using_match_shorthand?(path_without_format, to, options[:action]) if using_match_shorthand?(path_without_format, to, options[:action])
to ||= path_without_format.gsub(%r{^/}, "").sub(%r{/([^/]*)$}, '#\1') to ||= path_without_format.gsub(%r{^/}, "").sub(%r{/([^/]*)$}, '#\1')
to.tr!("-", "_") to.tr!("-", "_")
end end
decomposed_match(path, controller, options, option_path, to, via) decomposed_match(path, controller, options, option_path, to, via, formatted)
end end
def using_match_shorthand?(path, to, action) def using_match_shorthand?(path, to, action)
@ -1582,22 +1581,22 @@ module ActionDispatch
path =~ %r{^/?[-\w]+/[-\w/]+$} path =~ %r{^/?[-\w]+/[-\w/]+$}
end 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) 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 else
case @scope.scope_level case @scope.scope_level
when :resources when :resources
nested { decomposed_match(path, controller, options, _path, to, via) } nested { decomposed_match(path, controller, options, _path, to, via, formatted) }
when :resource when :resource
member { decomposed_match(path, controller, options, _path, to, via) } member { decomposed_match(path, controller, options, _path, to, via, formatted) }
else else
add_route(path, controller, options, _path, to, via) add_route(path, controller, options, _path, to, via, formatted)
end end
end 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) path = path_for_action(action, _path)
raise ArgumentError, "path is required" if path.blank? raise ArgumentError, "path is required" if path.blank?
@ -1617,7 +1616,7 @@ module ActionDispatch
name_for_action(options.delete(:as), action) name_for_action(options.delete(:as), action)
end 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 app, conditions, requirements, defaults, as, anchor = mapping.to_route
@set.add_route(app, conditions, requirements, defaults, as, anchor) @set.add_route(app, conditions, requirements, defaults, as, anchor)
end end

View file

@ -52,6 +52,15 @@ module ActionDispatch
end end
end end
def test_unscoped_formatted
fakeset = FakeSet.new
mapper = Mapper.new fakeset
mapper.get '/foo', :to => 'posts#index', :as => :main, :format => true
assert_equal({:controller=>"posts", :action=>"index"},
fakeset.defaults.first)
assert_equal "/foo.:format", fakeset.conditions.first[:path_info]
end
def test_scoped_formatted def test_scoped_formatted
fakeset = FakeSet.new fakeset = FakeSet.new
mapper = Mapper.new fakeset mapper = Mapper.new fakeset
@ -77,7 +86,7 @@ module ActionDispatch
def test_mapping_requirements def test_mapping_requirements
options = { } options = { }
scope = Mapper::Scope.new({}) scope = Mapper::Scope.new({})
m = Mapper::Mapping.build(scope, FakeSet.new, '/store/:name(*rest)', nil, 'foo', 'bar', nil, [:get], options) m = Mapper::Mapping.build(scope, FakeSet.new, '/store/:name(*rest)', nil, 'foo', 'bar', nil, [:get], nil, options)
_, _, requirements, _ = m.to_route _, _, requirements, _ = m.to_route
assert_equal(/.+?/, requirements[:rest]) assert_equal(/.+?/, requirements[:rest])
end end