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

pass the route name to define_url_helper

this allows us to avoid 2 hash allocations per named helper definition,
also we can avoid a `merge` and `delete`.
This commit is contained in:
Aaron Patterson 2014-07-17 11:21:06 -07:00
parent 1a300b6748
commit 212057b912
3 changed files with 26 additions and 23 deletions

View file

@ -134,11 +134,11 @@ module ActionDispatch
end end
class UrlHelper # :nodoc: class UrlHelper # :nodoc:
def self.create(route, options, url_strategy) def self.create(route, options, route_name, url_strategy)
if optimize_helper?(route) if optimize_helper?(route)
OptimizedUrlHelper.new(route, options, url_strategy) OptimizedUrlHelper.new(route, options, route_name, url_strategy)
else else
new route, options, url_strategy new route, options, route_name, url_strategy
end end
end end
@ -146,12 +146,12 @@ module ActionDispatch
!route.glob? && route.path.requirements.empty? !route.glob? && route.path.requirements.empty?
end end
attr_reader :url_strategy attr_reader :url_strategy, :route_name
class OptimizedUrlHelper < UrlHelper # :nodoc: class OptimizedUrlHelper < UrlHelper # :nodoc:
attr_reader :arg_size attr_reader :arg_size
def initialize(route, options, url_strategy) def initialize(route, options, route_name, url_strategy)
super super
@required_parts = @route.required_parts @required_parts = @route.required_parts
@arg_size = @required_parts.size @arg_size = @required_parts.size
@ -203,11 +203,12 @@ module ActionDispatch
end end
end end
def initialize(route, options, url_strategy) def initialize(route, options, route_name, url_strategy)
@options = options @options = options
@segment_keys = route.segment_keys.uniq @segment_keys = route.segment_keys.uniq
@route = route @route = route
@url_strategy = url_strategy @url_strategy = url_strategy
@route_name = route_name
end end
def call(t, args, inner_options) def call(t, args, inner_options)
@ -219,7 +220,7 @@ module ActionDispatch
options, options,
@segment_keys) @segment_keys)
t._routes.url_for(hash, url_strategy) t._routes.url_for(hash, route_name, url_strategy)
end end
def handle_positional_args(controller_options, inner_options, args, result, path_params) def handle_positional_args(controller_options, inner_options, args, result, path_params)
@ -252,8 +253,8 @@ module ActionDispatch
# #
# foo_url(bar, baz, bang, sort_by: 'baz') # foo_url(bar, baz, bang, sort_by: 'baz')
# #
def define_url_helper(route, name, opts, url_strategy) def define_url_helper(route, name, opts, route_key, url_strategy)
helper = UrlHelper.create(route, opts, url_strategy) helper = UrlHelper.create(route, opts, route_key, url_strategy)
@module.remove_possible_method name @module.remove_possible_method name
@module.module_eval do @module.module_eval do
@ -268,11 +269,8 @@ module ActionDispatch
end end
def define_named_route_methods(name, route) def define_named_route_methods(name, route)
define_url_helper route, :"#{name}_path", define_url_helper route, :"#{name}_path", route.defaults, name, PATH
route.defaults.merge(:use_route => name), PATH define_url_helper route, :"#{name}_url", route.defaults, name, FULL
define_url_helper route, :"#{name}_url",
route.defaults.merge(:use_route => name), FULL
end end
end end
@ -512,8 +510,8 @@ module ActionDispatch
attr_reader :options, :recall, :set, :named_route attr_reader :options, :recall, :set, :named_route
def initialize(options, recall, set) def initialize(named_route, options, recall, set)
@named_route = options.delete(:use_route) @named_route = named_route
@options = options.dup @options = options.dup
@recall = recall.dup @recall = recall.dup
@set = set @set = set
@ -629,13 +627,15 @@ module ActionDispatch
end end
def generate_extras(options, recall={}) def generate_extras(options, recall={})
path, params = generate(options, recall) route_key = options.delete :use_route
path, params = generate(route_key, options, recall)
return path, params.keys return path, params.keys
end end
def generate(options, recall = {}) def generate(route_key, options, recall = {})
Generator.new(options, recall, self).generate Generator.new(route_key, options, recall, self).generate
end end
private :generate
RESERVED_OPTIONS = [:host, :protocol, :port, :subdomain, :domain, :tld_length, RESERVED_OPTIONS = [:host, :protocol, :port, :subdomain, :domain, :tld_length,
:trailing_slash, :anchor, :params, :only_path, :script_name, :trailing_slash, :anchor, :params, :only_path, :script_name,
@ -654,7 +654,7 @@ module ActionDispatch
end end
# The +options+ argument must be a hash whose keys are *symbols*. # The +options+ argument must be a hash whose keys are *symbols*.
def url_for(options, url_strategy = UNKNOWN) def url_for(options, route_name = nil, url_strategy = UNKNOWN)
options = default_url_options.merge options options = default_url_options.merge options
user = password = nil user = password = nil
@ -676,7 +676,7 @@ module ActionDispatch
path_options = options.dup path_options = options.dup
RESERVED_OPTIONS.each { |ro| path_options.delete ro } RESERVED_OPTIONS.each { |ro| path_options.delete ro }
path, params = generate(path_options, recall) path, params = generate(route_name, path_options, recall)
if options.key? :params if options.key? :params
params.merge! options[:params] params.merge! options[:params]

View file

@ -152,7 +152,9 @@ module ActionDispatch
when nil when nil
_routes.url_for(url_options.symbolize_keys) _routes.url_for(url_options.symbolize_keys)
when Hash when Hash
_routes.url_for(options.symbolize_keys.reverse_merge!(url_options)) route_name = options.delete :use_route
_routes.url_for(options.symbolize_keys.reverse_merge!(url_options),
route_name)
when String when String
options options
when Symbol when Symbol

View file

@ -350,7 +350,8 @@ end
module RoutingTestHelpers module RoutingTestHelpers
def url_for(set, options) def url_for(set, options)
set.url_for options.merge(:only_path => true) route_name = options.delete :use_route
set.url_for options.merge(:only_path => true), route_name
end end
def make_set(strict = true) def make_set(strict = true)