提交 43dae996 编写于 作者: E Evan Phoenix

Cache url_helpers instead of creating each time

This has 2 effects:

1. RoutesProxy is CRAZY faster because it's no longer creating a new
Module each time method_missing is hit.
2. It bypasses an existing bug in ruby that makes `class << obj` unsafe
to be used in threading contexts.
上级 da2987af
......@@ -395,9 +395,11 @@ def define_mounted_helper(name)
return if MountedHelpers.method_defined?(name)
routes = self
helpers = routes.url_helpers
MountedHelpers.class_eval do
define_method "_#{name}" do
RoutesProxy.new(routes, _routes_context)
RoutesProxy.new(routes, _routes_context, helpers)
end
end
......
......@@ -8,8 +8,9 @@ class RoutesProxy #:nodoc:
attr_accessor :scope, :routes
alias :_routes :routes
def initialize(routes, scope)
def initialize(routes, scope, helpers=nil)
@routes, @scope = routes, scope
@helpers = helpers || routes.url_helpers
end
def url_options
......@@ -19,16 +20,16 @@ def url_options
end
def respond_to?(method, include_private = false)
super || routes.url_helpers.respond_to?(method)
super || @helpers.respond_to?(method)
end
def method_missing(method, *args)
if routes.url_helpers.respond_to?(method)
if @helpers.respond_to?(method)
self.class.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(*args)
options = args.extract_options!
args << url_options.merge((options || {}).symbolize_keys)
routes.url_helpers.#{method}(*args)
@helpers.#{method}(*args)
end
RUBY
send(method, *args)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册