diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-01-25 10:04:46 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-01-30 10:24:57 -0800 |
commit | 621ca05ae40c3d27b12d1681ebe049fcf18bc213 (patch) | |
tree | 6c84994af0ea8049749037173fef7d1667c1d6a1 /actionpack | |
parent | 44dd44ad7018badf4fe95f93675af2a6d5587df4 (diff) | |
download | rails-621ca05ae40c3d27b12d1681ebe049fcf18bc213.tar.gz rails-621ca05ae40c3d27b12d1681ebe049fcf18bc213.tar.bz2 rails-621ca05ae40c3d27b12d1681ebe049fcf18bc213.zip |
moving some stuff to the initializer
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_dispatch/routing/route_set.rb | 52 |
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 |