aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2015-02-01 14:30:59 -0800
committereileencodes <eileencodes@gmail.com>2015-02-01 14:33:53 -0800
commit3b637802b26fd9b4b80e87d9df24e25580d1a85b (patch)
treeea30683b567cdb7990a028c80afb7292bcbb011f /actionpack
parente6d8f435a921dce360bce9faae7dabaf753d4d23 (diff)
downloadrails-3b637802b26fd9b4b80e87d9df24e25580d1a85b.tar.gz
rails-3b637802b26fd9b4b80e87d9df24e25580d1a85b.tar.bz2
rails-3b637802b26fd9b4b80e87d9df24e25580d1a85b.zip
Cache `url_helpers`
`url_helpers` used to be memoized. This was lost in a refactoring and this PR adds it back. We noticed this while investigating why integration tests are slower than controller tests.
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/routing/route_set.rb86
1 files changed, 44 insertions, 42 deletions
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb
index 2b24b896db..3dee42a0f0 100644
--- a/actionpack/lib/action_dispatch/routing/route_set.rb
+++ b/actionpack/lib/action_dispatch/routing/route_set.rb
@@ -410,61 +410,63 @@ module ActionDispatch
end
def url_helpers(supports_path = true)
- routes = self
-
- Module.new do
- extend ActiveSupport::Concern
- include UrlFor
+ @url_helpers ||= begin
+ routes = self
+
+ Module.new do
+ extend ActiveSupport::Concern
+ include UrlFor
+
+ # Define url_for in the singleton level so one can do:
+ # Rails.application.routes.url_helpers.url_for(args)
+ @_routes = routes
+ class << self
+ def url_for(options)
+ @_routes.url_for(options)
+ end
- # Define url_for in the singleton level so one can do:
- # Rails.application.routes.url_helpers.url_for(args)
- @_routes = routes
- class << self
- def url_for(options)
- @_routes.url_for(options)
- end
+ def optimize_routes_generation?
+ @_routes.optimize_routes_generation?
+ end
- def optimize_routes_generation?
- @_routes.optimize_routes_generation?
+ attr_reader :_routes
+ def url_options; {}; end
end
- attr_reader :_routes
- def url_options; {}; end
- end
+ url_helpers = routes.named_routes.url_helpers_module
- url_helpers = routes.named_routes.url_helpers_module
+ # Make named_routes available in the module singleton
+ # as well, so one can do:
+ # Rails.application.routes.url_helpers.posts_path
+ extend url_helpers
- # Make named_routes available in the module singleton
- # as well, so one can do:
- # Rails.application.routes.url_helpers.posts_path
- extend url_helpers
+ # Any class that includes this module will get all
+ # named routes...
+ include url_helpers
- # Any class that includes this module will get all
- # named routes...
- include url_helpers
+ if supports_path
+ path_helpers = routes.named_routes.path_helpers_module
- if supports_path
- path_helpers = routes.named_routes.path_helpers_module
+ include path_helpers
+ extend path_helpers
+ end
- include path_helpers
- extend path_helpers
- end
+ # plus a singleton class method called _routes ...
+ included do
+ singleton_class.send(:redefine_method, :_routes) { routes }
+ end
- # plus a singleton class method called _routes ...
- included do
- singleton_class.send(:redefine_method, :_routes) { routes }
- end
+ # And an instance method _routes. Note that
+ # UrlFor (included in this module) add extra
+ # conveniences for working with @_routes.
+ define_method(:_routes) { @_routes || routes }
- # And an instance method _routes. Note that
- # UrlFor (included in this module) add extra
- # conveniences for working with @_routes.
- define_method(:_routes) { @_routes || routes }
+ define_method(:_generate_paths_by_default) do
+ supports_path
+ end
- define_method(:_generate_paths_by_default) do
- supports_path
+ private :_generate_paths_by_default
end
-
- private :_generate_paths_by_default
end
end