diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-01-25 09:39:13 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-01-30 10:24:56 -0800 |
commit | fc8f45a3c168ed5f0bc859e9f3a64e61cfe4b50a (patch) | |
tree | a9c050cb00a9b9ac6099670b55d3f802ee25cb45 /actionpack/lib/action_dispatch/routing | |
parent | 19e56400f7075fee3cfe1cdaf196eb32b5532381 (diff) | |
download | rails-fc8f45a3c168ed5f0bc859e9f3a64e61cfe4b50a.tar.gz rails-fc8f45a3c168ed5f0bc859e9f3a64e61cfe4b50a.tar.bz2 rails-fc8f45a3c168ed5f0bc859e9f3a64e61cfe4b50a.zip |
factored out some of the dynamic code
Diffstat (limited to 'actionpack/lib/action_dispatch/routing')
-rw-r--r-- | actionpack/lib/action_dispatch/routing/route_set.rb | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 705314f8ab..b3a85be8aa 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -103,21 +103,6 @@ module ActionDispatch @module = Module.new do protected - def handle_positional_args(args, options, segment_keys) - inner_options = args.extract_options! - result = options.dup - - if args.size > 0 - keys = segment_keys - if args.size < keys.size - 1 # take format into account - keys -= self.url_options.keys if self.respond_to?(:url_options) - keys -= options.keys - end - result.merge!(Hash[keys.zip(args)]) - end - - result.merge!(inner_options) - end end end @@ -182,17 +167,44 @@ 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)) + end + + def handle_positional_args(t, args, options, segment_keys) + inner_options = args.extract_options! + result = options.dup + + if args.size > 0 + keys = segment_keys + if args.size < keys.size - 1 # take format into account + keys -= t.url_options.keys if t.respond_to?(:url_options) + keys -= options.keys + end + result.merge!(Hash[keys.zip(args)]) + end + + result.merge!(inner_options) + end + end + def define_url_helper(route, name, options) @module.remove_possible_method name @module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1 + + def if_#{name}(t, options, path) + options.merge!(url_options) if t.respond_to?(:url_options) + options[:path] = path + ActionDispatch::Http::URL.url_for(options) + end + def #{name}(*args) if #{optimize_helper?(route)} && args.size == #{route.required_parts.size} && !args.last.is_a?(Hash) && optimize_routes_generation? options = #{options.inspect} - options.merge!(url_options) if respond_to?(:url_options) - options[:path] = "#{optimized_helper(route)}" - ActionDispatch::Http::URL.url_for(options) + if_#{name}(self, options, "#{optimized_helper(route)}") else - url_for(handle_positional_args(args, #{options.inspect}, #{route.segment_keys.inspect})) + UrlHelp.new.url_else(self, args, #{options.inspect}, #{route.segment_keys.inspect}) end end END_EVAL |