aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2014-07-17 11:21:06 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2014-07-17 11:21:17 -0700
commit212057b912627b9c4056f911a43c83b18ae3ab34 (patch)
tree626fea2c2a9a505baeca73304ed816b79f0e9787 /actionpack
parent1a300b674810b85e6f55e4106cb27c1b2dbef499 (diff)
downloadrails-212057b912627b9c4056f911a43c83b18ae3ab34.tar.gz
rails-212057b912627b9c4056f911a43c83b18ae3ab34.tar.bz2
rails-212057b912627b9c4056f911a43c83b18ae3ab34.zip
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`.
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/routing/route_set.rb42
-rw-r--r--actionpack/lib/action_dispatch/routing/url_for.rb4
-rw-r--r--actionpack/test/abstract_unit.rb3
3 files changed, 26 insertions, 23 deletions
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb
index 3ecf6a76f1..80c705608d 100644
--- a/actionpack/lib/action_dispatch/routing/route_set.rb
+++ b/actionpack/lib/action_dispatch/routing/route_set.rb
@@ -134,11 +134,11 @@ module ActionDispatch
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 @@ module ActionDispatch
!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 @@ module ActionDispatch
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 @@ module ActionDispatch
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 @@ module ActionDispatch
#
# 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 @@ module ActionDispatch
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 @@ module ActionDispatch
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 @@ module ActionDispatch
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 @@ module ActionDispatch
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 @@ module ActionDispatch
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]
diff --git a/actionpack/lib/action_dispatch/routing/url_for.rb b/actionpack/lib/action_dispatch/routing/url_for.rb
index e624fe3c4a..e1c73f8f07 100644
--- a/actionpack/lib/action_dispatch/routing/url_for.rb
+++ b/actionpack/lib/action_dispatch/routing/url_for.rb
@@ -152,7 +152,9 @@ module ActionDispatch
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
diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb
index 47d968982c..1c143a0f8c 100644
--- a/actionpack/test/abstract_unit.rb
+++ b/actionpack/test/abstract_unit.rb
@@ -350,7 +350,8 @@ end
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)