From e1b73b975264c622f692a46eec060ff35a588d96 Mon Sep 17 00:00:00 2001 From: Bence Nagy Date: Sat, 27 Dec 2008 23:28:37 +0100 Subject: path_names could be used to customize collection actions too Signed-off-by: Jeremy Kemper --- actionpack/lib/action_controller/routing/resources.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_controller/routing') diff --git a/actionpack/lib/action_controller/routing/resources.rb b/actionpack/lib/action_controller/routing/resources.rb index 2dee0a3d87..c1ebd46b48 100644 --- a/actionpack/lib/action_controller/routing/resources.rb +++ b/actionpack/lib/action_controller/routing/resources.rb @@ -589,7 +589,10 @@ module ActionController resource.collection_methods.each do |method, actions| actions.each do |action| [method].flatten.each do |m| - map_resource_routes(map, resource, action, "#{resource.path}#{resource.action_separator}#{action}", "#{action}_#{resource.name_prefix}#{resource.plural}", m) + action_path = resource.options[:path_names][action] if resource.options[:path_names].is_a?(Hash) + action_path ||= action + + map_resource_routes(map, resource, action, "#{resource.path}#{resource.action_separator}#{action_path}", "#{action}_#{resource.name_prefix}#{resource.plural}", m) end end end -- cgit v1.2.3 From 202b091373ed4d6c78adc7af5e89a359ff0fff2d Mon Sep 17 00:00:00 2001 From: Hugo Peixoto Date: Sun, 9 Aug 2009 07:28:29 +0100 Subject: Added both the documentation and a test case for the collection path name customization feature. [#1218 state:committed] Signed-off-by: Jeremy Kemper --- actionpack/lib/action_controller/routing/resources.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_controller/routing') diff --git a/actionpack/lib/action_controller/routing/resources.rb b/actionpack/lib/action_controller/routing/resources.rb index c1ebd46b48..7fd3ffd3f1 100644 --- a/actionpack/lib/action_controller/routing/resources.rb +++ b/actionpack/lib/action_controller/routing/resources.rb @@ -320,9 +320,10 @@ module ActionController # notes.resources :attachments # end # - # * :path_names - Specify different names for the 'new' and 'edit' actions. For example: + # * :path_names - Specify different path names for the actions. For example: # # new_products_path == '/productos/nuevo' - # map.resources :products, :as => 'productos', :path_names => { :new => 'nuevo', :edit => 'editar' } + # # bids_product_path(1) == '/productos/1/licitacoes' + # map.resources :products, :as => 'productos', :member => { :bids => :get }, :path_names => { :new => 'nuevo', :bids => 'licitacoes' } # # You can also set default action names from an environment, like this: # config.action_controller.resources_path_names = { :new => 'nuevo', :edit => 'editar' } -- cgit v1.2.3 From 734e903af5913342c65d4c294e45f9095fa89986 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 9 Aug 2009 22:38:50 -0500 Subject: Deprecate router generation "best match" sorting --- .../lib/action_controller/routing/resources.rb | 4 +-- .../lib/action_controller/routing/route_set.rb | 32 +++++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) (limited to 'actionpack/lib/action_controller/routing') diff --git a/actionpack/lib/action_controller/routing/resources.rb b/actionpack/lib/action_controller/routing/resources.rb index 7fd3ffd3f1..4862cf7115 100644 --- a/actionpack/lib/action_controller/routing/resources.rb +++ b/actionpack/lib/action_controller/routing/resources.rb @@ -529,13 +529,13 @@ module ActionController resource = Resource.new(entities, options) with_options :controller => resource.controller do |map| + map_associations(resource, options) + map_collection_actions(map, resource) map_default_collection_actions(map, resource) map_new_actions(map, resource) map_member_actions(map, resource) - map_associations(resource, options) - if block_given? with_options(options.slice(*INHERITABLE_OPTIONS).merge(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix), &block) end diff --git a/actionpack/lib/action_controller/routing/route_set.rb b/actionpack/lib/action_controller/routing/route_set.rb index 0c499f3b46..09f6024d39 100644 --- a/actionpack/lib/action_controller/routing/route_set.rb +++ b/actionpack/lib/action_controller/routing/route_set.rb @@ -407,9 +407,24 @@ module ActionController # don't use the recalled keys when determining which routes to check routes = routes_by_controller[controller][action][options.reject {|k,v| !v}.keys.sort_by { |x| x.object_id }] - routes.each do |route| + routes[1].each_with_index do |route, index| results = route.__send__(method, options, merged, expire_on) - return results if results && (!results.is_a?(Array) || results.first) + if results && (!results.is_a?(Array) || results.first) + + # Compare results with Rails 3.0 behavior + if routes[0][index] != route + routes[0].each do |route2| + new_results = route2.__send__(method, options, merged, expire_on) + if new_results && (!new_results.is_a?(Array) || new_results.first) + ActiveSupport::Deprecation.warn "The URL you generated will use the first matching route in routes.rb rather than the \"best\" match. " + + "In Rails 3.0 #{new_results} would of been generated instead of #{results}" + break + end + end + end + + return results + end end end @@ -448,7 +463,10 @@ module ActionController @routes_by_controller ||= Hash.new do |controller_hash, controller| controller_hash[controller] = Hash.new do |action_hash, action| action_hash[action] = Hash.new do |key_hash, keys| - key_hash[keys] = routes_for_controller_and_action_and_keys(controller, action, keys) + key_hash[keys] = [ + routes_for_controller_and_action_and_keys(controller, action, keys), + deprecated_routes_for_controller_and_action_and_keys(controller, action, keys) + ] end end end @@ -460,10 +478,16 @@ module ActionController merged = options if expire_on[:controller] action = merged[:action] || 'index' - routes_by_controller[controller][action][merged.keys] + routes_by_controller[controller][action][merged.keys][1] end def routes_for_controller_and_action_and_keys(controller, action, keys) + routes.select do |route| + route.matches_controller_and_action? controller, action + end + end + + def deprecated_routes_for_controller_and_action_and_keys(controller, action, keys) selected = routes.select do |route| route.matches_controller_and_action? controller, action end -- cgit v1.2.3 From df6617bc8ac1677ab2b2cf7ed2859cdcc393ccb5 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 15 Aug 2009 15:56:52 -0700 Subject: Normalize route generation order: associations, yield block, then own routes. --- actionpack/lib/action_controller/routing/resources.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_controller/routing') diff --git a/actionpack/lib/action_controller/routing/resources.rb b/actionpack/lib/action_controller/routing/resources.rb index 4862cf7115..06506435a2 100644 --- a/actionpack/lib/action_controller/routing/resources.rb +++ b/actionpack/lib/action_controller/routing/resources.rb @@ -531,14 +531,14 @@ module ActionController with_options :controller => resource.controller do |map| map_associations(resource, options) + if block_given? + with_options(options.slice(*INHERITABLE_OPTIONS).merge(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix), &block) + end + map_collection_actions(map, resource) map_default_collection_actions(map, resource) map_new_actions(map, resource) map_member_actions(map, resource) - - if block_given? - with_options(options.slice(*INHERITABLE_OPTIONS).merge(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix), &block) - end end end @@ -546,16 +546,16 @@ module ActionController resource = SingletonResource.new(entities, options) with_options :controller => resource.controller do |map| - map_collection_actions(map, resource) - map_new_actions(map, resource) - map_member_actions(map, resource) - map_default_singleton_actions(map, resource) - map_associations(resource, options) if block_given? with_options(options.slice(*INHERITABLE_OPTIONS).merge(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix), &block) end + + map_collection_actions(map, resource) + map_new_actions(map, resource) + map_member_actions(map, resource) + map_default_singleton_actions(map, resource) end end -- cgit v1.2.3 From 911acc10de483e0d7a9e6b3c475aeaecad49bfc5 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 15 Aug 2009 18:08:46 -0500 Subject: Axe "best fit" generation support --- .../lib/action_controller/routing/route_set.rb | 29 ++-------------------- 1 file changed, 2 insertions(+), 27 deletions(-) (limited to 'actionpack/lib/action_controller/routing') diff --git a/actionpack/lib/action_controller/routing/route_set.rb b/actionpack/lib/action_controller/routing/route_set.rb index 09f6024d39..a4f54ad662 100644 --- a/actionpack/lib/action_controller/routing/route_set.rb +++ b/actionpack/lib/action_controller/routing/route_set.rb @@ -407,22 +407,9 @@ module ActionController # don't use the recalled keys when determining which routes to check routes = routes_by_controller[controller][action][options.reject {|k,v| !v}.keys.sort_by { |x| x.object_id }] - routes[1].each_with_index do |route, index| + routes.each_with_index do |route, index| results = route.__send__(method, options, merged, expire_on) if results && (!results.is_a?(Array) || results.first) - - # Compare results with Rails 3.0 behavior - if routes[0][index] != route - routes[0].each do |route2| - new_results = route2.__send__(method, options, merged, expire_on) - if new_results && (!new_results.is_a?(Array) || new_results.first) - ActiveSupport::Deprecation.warn "The URL you generated will use the first matching route in routes.rb rather than the \"best\" match. " + - "In Rails 3.0 #{new_results} would of been generated instead of #{results}" - break - end - end - end - return results end end @@ -463,10 +450,7 @@ module ActionController @routes_by_controller ||= Hash.new do |controller_hash, controller| controller_hash[controller] = Hash.new do |action_hash, action| action_hash[action] = Hash.new do |key_hash, keys| - key_hash[keys] = [ - routes_for_controller_and_action_and_keys(controller, action, keys), - deprecated_routes_for_controller_and_action_and_keys(controller, action, keys) - ] + key_hash[keys] = routes_for_controller_and_action_and_keys(controller, action, keys) end end end @@ -487,15 +471,6 @@ module ActionController end end - def deprecated_routes_for_controller_and_action_and_keys(controller, action, keys) - selected = routes.select do |route| - route.matches_controller_and_action? controller, action - end - selected.sort_by do |route| - (keys - route.significant_keys).length - end - end - # Subclasses and plugins may override this method to extract further attributes # from the request, for use by route conditions and such. def extract_request_environment(request) -- cgit v1.2.3 From 24ad9ae3d228acdf9aa31cf28bfe6dfb0139c247 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 16 Aug 2009 21:14:26 -0500 Subject: Cleanup route reloading in tests. Prefer with_routing over using ActionController::Routing::Routes directly --- actionpack/lib/action_controller/routing/route_set.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_controller/routing') diff --git a/actionpack/lib/action_controller/routing/route_set.rb b/actionpack/lib/action_controller/routing/route_set.rb index a4f54ad662..25fdbf480e 100644 --- a/actionpack/lib/action_controller/routing/route_set.rb +++ b/actionpack/lib/action_controller/routing/route_set.rb @@ -213,7 +213,7 @@ module ActionController self.routes = [] self.named_routes = NamedRouteCollection.new - clear_recognize_optimized! + clear! end # Subclasses and plugins may override this method to specify a different @@ -223,6 +223,7 @@ module ActionController end def draw + clear! yield Mapper.new(self) install_helpers end @@ -230,8 +231,10 @@ module ActionController def clear! routes.clear named_routes.clear + @combined_regexp = nil @routes_by_controller = nil + # This will force routing/recognition_optimization.rb # to refresh optimisations. clear_recognize_optimized! @@ -262,7 +265,6 @@ module ActionController def load! Routing.use_controllers!(nil) # Clear the controller cache so we may discover new ones - clear! load_routes! end @@ -286,10 +288,12 @@ module ActionController configuration_files.each { |config| load(config) } @routes_last_modified = routes_changed_at else - add_route ":controller/:action/:id" + draw do |map| + map.connect ":controller/:action/:id" + end end end - + def routes_changed_at routes_changed_at = nil -- cgit v1.2.3 From 05b529ca57f75ce64540b9d34597e0c3bfe1fca7 Mon Sep 17 00:00:00 2001 From: Jeffrey Hardy Date: Tue, 2 Jun 2009 13:28:44 -0400 Subject: UrlRewriter#rewrite_url should call #to_param on the value given in :anchor option, just as #url_for does [#2746 state:committed] Signed-off-by: Jeremy Kemper --- actionpack/lib/action_controller/routing/generation/url_rewriter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_controller/routing') diff --git a/actionpack/lib/action_controller/routing/generation/url_rewriter.rb b/actionpack/lib/action_controller/routing/generation/url_rewriter.rb index 9717582b5e..52b66c9303 100644 --- a/actionpack/lib/action_controller/routing/generation/url_rewriter.rb +++ b/actionpack/lib/action_controller/routing/generation/url_rewriter.rb @@ -172,7 +172,7 @@ module ActionController path = rewrite_path(options) rewritten_url << ActionController::Base.relative_url_root.to_s unless options[:skip_relative_url_root] rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path) - rewritten_url << "##{options[:anchor]}" if options[:anchor] + rewritten_url << "##{CGI.escape(options[:anchor].to_param.to_s)}" if options[:anchor] rewritten_url end -- cgit v1.2.3