Permalink
Browse files

moving some stuff to the initializer

  • Loading branch information...
1 parent 44dd44a commit 621ca05ae40c3d27b12d1681ebe049fcf18bc213 @tenderlove tenderlove committed Jan 25, 2013
Showing with 38 additions and 14 deletions.
  1. +38 −14 actionpack/lib/action_dispatch/routing/route_set.rb
@@ -167,15 +167,23 @@ def define_named_route_methods(name, route)
#
# 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 @@ def handle_positional_args(t, args, options, segment_keys)
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

0 comments on commit 621ca05

Please sign in to comment.