Skip to content
Browse files

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`.
  • Loading branch information...
1 parent 1a300b6 commit 212057b912627b9c4056f911a43c83b18ae3ab34 @tenderlove tenderlove committed
View
42 actionpack/lib/action_dispatch/routing/route_set.rb
@@ -134,11 +134,11 @@ def length
end
class UrlHelper # :nodoc:
- def self.create(route, options, url_strategy)
+ def self.create(route, options, route_name, url_strategy)
if optimize_helper?(route)
- OptimizedUrlHelper.new(route, options, url_strategy)
+ OptimizedUrlHelper.new(route, options, route_name, url_strategy)
else
- new route, options, url_strategy
+ new route, options, route_name, url_strategy
end
end
@@ -146,12 +146,12 @@ def self.optimize_helper?(route)
!route.glob? && route.path.requirements.empty?
end
- attr_reader :url_strategy
+ attr_reader :url_strategy, :route_name
class OptimizedUrlHelper < UrlHelper # :nodoc:
attr_reader :arg_size
- def initialize(route, options, url_strategy)
+ def initialize(route, options, route_name, url_strategy)
super
@required_parts = @route.required_parts
@arg_size = @required_parts.size
@@ -203,11 +203,12 @@ def raise_generation_error(args, missing_keys)
end
end
- def initialize(route, options, url_strategy)
+ def initialize(route, options, route_name, url_strategy)
@options = options
@segment_keys = route.segment_keys.uniq
@route = route
@url_strategy = url_strategy
+ @route_name = route_name
end
def call(t, args, inner_options)
@@ -219,7 +220,7 @@ def call(t, args, inner_options)
options,
@segment_keys)
- t._routes.url_for(hash, url_strategy)
+ t._routes.url_for(hash, route_name, url_strategy)
end
def handle_positional_args(controller_options, inner_options, args, result, path_params)
@@ -252,8 +253,8 @@ def handle_positional_args(controller_options, inner_options, args, result, path
#
# foo_url(bar, baz, bang, sort_by: 'baz')
#
- def define_url_helper(route, name, opts, url_strategy)
- helper = UrlHelper.create(route, opts, url_strategy)
+ def define_url_helper(route, name, opts, route_key, url_strategy)
+ helper = UrlHelper.create(route, opts, route_key, url_strategy)
@module.remove_possible_method name
@module.module_eval do
@@ -268,11 +269,8 @@ def define_url_helper(route, name, opts, url_strategy)
end
def define_named_route_methods(name, route)
- define_url_helper route, :"#{name}_path",
- route.defaults.merge(:use_route => name), PATH
-
- define_url_helper route, :"#{name}_url",
- route.defaults.merge(:use_route => name), FULL
+ define_url_helper route, :"#{name}_path", route.defaults, name, PATH
+ define_url_helper route, :"#{name}_url", route.defaults, name, FULL
end
end
@@ -512,8 +510,8 @@ class Generator #:nodoc:
attr_reader :options, :recall, :set, :named_route
- def initialize(options, recall, set)
- @named_route = options.delete(:use_route)
+ def initialize(named_route, options, recall, set)
+ @named_route = named_route
@options = options.dup
@recall = recall.dup
@set = set
@@ -629,13 +627,15 @@ def extra_keys(options, recall={})
end
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
end
- def generate(options, recall = {})
- Generator.new(options, recall, self).generate
+ def generate(route_key, options, recall = {})
+ Generator.new(route_key, options, recall, self).generate
end
+ private :generate
RESERVED_OPTIONS = [:host, :protocol, :port, :subdomain, :domain, :tld_length,
:trailing_slash, :anchor, :params, :only_path, :script_name,
@@ -654,7 +654,7 @@ def find_script_name(options)
end
# 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
user = password = nil
@@ -676,7 +676,7 @@ def url_for(options, url_strategy = UNKNOWN)
path_options = options.dup
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
params.merge! options[:params]
View
4 actionpack/lib/action_dispatch/routing/url_for.rb
@@ -152,7 +152,9 @@ def url_for(options = nil)
when nil
_routes.url_for(url_options.symbolize_keys)
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
options
when Symbol
View
3 actionpack/test/abstract_unit.rb
@@ -350,7 +350,8 @@ def patch(uri_or_host, path = nil)
module RoutingTestHelpers
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
def make_set(strict = true)

0 comments on commit 212057b

Please sign in to comment.
Something went wrong with that request. Please try again.