From 7cb26b5d2d0ecb4945f01b1aa1d398eb85c9f1a4 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Mon, 17 Sep 2007 09:30:18 +0000 Subject: Disable optimisation code for UrlWriter as request.host doesn't make sense there. Don't try to use the .to_query method when the route has no dynamic segments. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7501 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/routing.rb | 20 ++++++++++++++------ .../lib/action_controller/routing_optimisation.rb | 22 ++++++++++++++++++++-- actionpack/lib/action_controller/url_rewriter.rb | 6 +++++- 3 files changed, 39 insertions(+), 9 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/routing.rb b/actionpack/lib/action_controller/routing.rb index adeb70235c..faaecb8964 100644 --- a/actionpack/lib/action_controller/routing.rb +++ b/actionpack/lib/action_controller/routing.rb @@ -342,7 +342,7 @@ module ActionController # Indicates whether the routes should be optimised with the string interpolation # version of the named routes methods. def optimise? - @optimise + @optimise && ActionController::Routing::optimise_named_routes end def segment_keys @@ -1004,8 +1004,7 @@ module ActionController # Routes cannot use the current string interpolation method # if there are user-supplied :requirements as the interpolation # code won't raise RoutingErrors when generating - route.optimise = !options.key?(:requirements) && ActionController::Routing.optimise_named_routes - + route.optimise = !options.key?(:requirements) if !route.significant_keys.include?(:action) && !route.requirements[:action] route.requirements[:action] = "index" route.significant_keys << :action @@ -1119,7 +1118,16 @@ module ActionController routes.length end - def install(destinations = [ActionController::Base, ActionView::Base]) + def reset! + old_routes = routes.dup + clear! + old_routes.each do |name, route| + add(name, route) + end + end + + def install(destinations = [ActionController::Base, ActionView::Base], regenerate = false) + reset! if regenerate Array(destinations).each { |dest| dest.send :include, @module } end @@ -1219,9 +1227,9 @@ module ActionController @routes_by_controller = nil end - def install_helpers(destinations = [ActionController::Base, ActionView::Base]) + def install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) Array(destinations).each { |d| d.send :include, Helpers } - named_routes.install(destinations) + named_routes.install(destinations, regenerate_code) end def empty? diff --git a/actionpack/lib/action_controller/routing_optimisation.rb b/actionpack/lib/action_controller/routing_optimisation.rb index eaff1869dd..7cf85c9b9a 100644 --- a/actionpack/lib/action_controller/routing_optimisation.rb +++ b/actionpack/lib/action_controller/routing_optimisation.rb @@ -13,8 +13,8 @@ module ActionController def generate_optimisation_block(route, kind) return "" unless route.optimise? OPTIMISERS.inject("") do |memo, klazz| - optimiser = klazz.new(route, kind) - memo << "return #{optimiser.generation_code} if #{optimiser.guard_condition}\n" + memo << klazz.new(route, kind).source_code + memo end end @@ -32,6 +32,18 @@ module ActionController def generation_code 'nil' end + + def source_code + if applicable? + "return #{generation_code} if #{guard_condition}\n" + else + "\n" + end + end + + def applicable? + true + end end # Given a route: @@ -89,6 +101,12 @@ module ActionController def generation_code super.insert(-2, '?#{args.last.to_query}') end + + # To avoid generating http://localhost/?host=foo.example.com we + # can't use this optimisation on routes without any segments + def applicable? + route.segment_keys.size > 0 + end end OPTIMISERS = [PositionalArguments, PositionalArgumentsWithAdditionalParams] diff --git a/actionpack/lib/action_controller/url_rewriter.rb b/actionpack/lib/action_controller/url_rewriter.rb index 8248696431..7aae619681 100644 --- a/actionpack/lib/action_controller/url_rewriter.rb +++ b/actionpack/lib/action_controller/url_rewriter.rb @@ -21,9 +21,13 @@ module ActionController self.default_url_options = {} def self.included(base) #:nodoc: - ActionController::Routing::Routes.install_helpers base + original_optimise = ActionController::Routing.optimise_named_routes + ActionController::Routing.optimise_named_routes = false + ActionController::Routing::Routes.install_helpers base, :regenerate base.mattr_accessor :default_url_options base.default_url_options ||= default_url_options + ensure + ActionController::Routing.optimise_named_routes = original_optimise end # Generate a url based on the options provided, default_url_options and the -- cgit v1.2.3