aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/routing/route_set.rb
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib/action_dispatch/routing/route_set.rb')
-rw-r--r--actionpack/lib/action_dispatch/routing/route_set.rb52
1 files changed, 38 insertions, 14 deletions
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb
index e9837c6ca8..65d4865baf 100644
--- a/actionpack/lib/action_dispatch/routing/route_set.rb
+++ b/actionpack/lib/action_dispatch/routing/route_set.rb
@@ -167,15 +167,23 @@ module ActionDispatch
#
# foo_url(bar, baz, bang, sort_by: 'baz')
#
- class UrlHelp
- def url_else(t, args, options, seg)
- t.url_for(handle_positional_args(t, args, options, seg))
+ class UrlHelper
+ def self.create(route, options)
+ new options
end
- def url_if(t, options, path)
- options.merge!(t.url_options) if t.respond_to?(:url_options)
- options[:path] = path
- ActionDispatch::Http::URL.url_for(options)
+ def initialize(options)
+ @options = options
+ end
+
+ def url_else(t, args, seg)
+ t.url_for(handle_positional_args(t, args, @options, seg))
+ end
+
+ def url_if(t, path)
+ @options.merge!(t.url_options) if t.respond_to?(:url_options)
+ @options[:path] = path
+ ActionDispatch::Http::URL.url_for(@options)
end
def handle_positional_args(t, args, options, segment_keys)
@@ -197,16 +205,32 @@ module ActionDispatch
def define_url_helper(route, name, options)
@module.remove_possible_method name
- @module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1
- def #{name}(*args)
- if #{optimize_helper?(route)} && args.size == #{route.required_parts.size} && !args.last.is_a?(Hash) && optimize_routes_generation?
- options = #{options.inspect}
- UrlHelp.new.url_if(self, options, "#{optimized_helper(route)}")
+ #@module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1
+ # def #{name}(*args)
+ # if #{optimize_helper?(route)} && args.size == #{route.required_parts.size} && !args.last.is_a?(Hash) && optimize_routes_generation?
+ # options = #{options.inspect}
+ # UrlHelp.new.url_if(self, options, "#{optimized_helper(route)}")
+ # else
+ # UrlHelp.new.url_else(self, args, #{options.inspect}, #{route.segment_keys.inspect})
+ # end
+ # end
+ #END_EVAL
+ ohelp = optimize_helper?(route)
+ ohelper = optimized_helper(route)
+ arg_size = route.required_parts.size
+ segment_keys = route.segment_keys
+
+ helper = UrlHelper.create(route, options.dup)
+
+ @module.module_eval do
+ define_method(name) do |*args|
+ if ohelp && args.size == arg_size && !args.last.is_a?(Hash) && optimize_routes_generation?
+ helper.url_if(self, eval("\"#{ohelper}\""))
else
- UrlHelp.new.url_else(self, args, #{options.inspect}, #{route.segment_keys.inspect})
+ helper.url_else(self, args, segment_keys)
end
end
- END_EVAL
+ end
helpers << name
end