From 43dae996457caa520e845e038b5a4aa6297c4a17 Mon Sep 17 00:00:00 2001 From: Evan Phoenix Date: Thu, 19 Feb 2015 14:20:56 -0800 Subject: 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. --- actionpack/lib/action_dispatch/routing/route_set.rb | 4 +++- actionpack/lib/action_dispatch/routing/routes_proxy.rb | 9 +++++---- 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 65d6a5eb1e..ce04f0b08a 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -395,9 +395,11 @@ module ActionDispatch 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 diff --git a/actionpack/lib/action_dispatch/routing/routes_proxy.rb b/actionpack/lib/action_dispatch/routing/routes_proxy.rb index e2393d3799..68602e1eb2 100644 --- a/actionpack/lib/action_dispatch/routing/routes_proxy.rb +++ b/actionpack/lib/action_dispatch/routing/routes_proxy.rb @@ -8,8 +8,9 @@ module ActionDispatch 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 @@ module ActionDispatch 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) -- cgit v1.2.3 From e002a68a4ec19c1d4278e9b523283f59fbe56be1 Mon Sep 17 00:00:00 2001 From: Evan Phoenix Date: Thu, 19 Feb 2015 15:11:08 -0800 Subject: Make the helpers a required argument --- actionpack/lib/action_dispatch/routing/routes_proxy.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_dispatch/routing/routes_proxy.rb b/actionpack/lib/action_dispatch/routing/routes_proxy.rb index 68602e1eb2..040ea04046 100644 --- a/actionpack/lib/action_dispatch/routing/routes_proxy.rb +++ b/actionpack/lib/action_dispatch/routing/routes_proxy.rb @@ -8,9 +8,9 @@ module ActionDispatch attr_accessor :scope, :routes alias :_routes :routes - def initialize(routes, scope, helpers=nil) + def initialize(routes, scope, helpers) @routes, @scope = routes, scope - @helpers = helpers || routes.url_helpers + @helpers = helpers end def url_options -- cgit v1.2.3