From d8a90d7dc64b86f6cfcc306f8ccb793de112e1ad Mon Sep 17 00:00:00 2001 From: Robert Thau Date: Mon, 18 Oct 2010 18:46:15 -0400 Subject: [PATCH] Rename 'request_path' to avoid collisions with named routes. Resources_controller defined an internal controller method called 'request_path'. If you've got a 'map.resources :requests' in your routes.rb file, that defines a named route called 'request_path'. When these two meet... wackiness ensues! To avoid the wackiness (which manifests as complaints from the code that implements the named route), this patch renames the 'request_path' method, and its companion 'nested_request_path', so that they're no longer at risk of being trampled by eccentric named routes. --- lib/ardes/resources_controller.rb | 2 +- .../request_path_introspection.rb | 14 +++---- spec/controllers/tags_controller_spec.rb | 6 +-- .../tags_controller_via_account_info_spec.rb | 8 ++-- .../tags_controller_via_forum_post_spec.rb | 8 ++-- spec/lib/load_enclosing_resources_spec.rb | 2 +- spec/lib/request_path_introspection_spec.rb | 42 +++++++++---------- spec/lib/resources_controller_spec.rb | 2 +- 8 files changed, 42 insertions(+), 42 deletions(-) diff --git a/lib/ardes/resources_controller.rb b/lib/ardes/resources_controller.rb index 6bc952b..6725c96 100644 --- a/lib/ardes/resources_controller.rb +++ b/lib/ardes/resources_controller.rb @@ -849,7 +849,7 @@ def raise_cant_find_singleton(name, klass) #:nodoc: def raise_resource_mismatch(controller) #:nodoc: raise ResourceMismatch, <<-end_str resources_controller can't match the route to the resource specification - path: #{controller.send(:request_path)} + path: #{controller.send(:path_of_request)} specification: enclosing: [#{controller.specifications.collect{|s| s.is_a?(Specification) ? ":#{s.segment}" : s}.join(', ')}], resource :#{controller.resource_specification.segment} the successfully loaded enclosing resources are: #{controller.enclosing_resources.join(', ')} diff --git a/lib/ardes/resources_controller/request_path_introspection.rb b/lib/ardes/resources_controller/request_path_introspection.rb index 3032983..2f6a068 100644 --- a/lib/ardes/resources_controller/request_path_introspection.rb +++ b/lib/ardes/resources_controller/request_path_introspection.rb @@ -6,17 +6,17 @@ module ResourcesController # these methods are aware of resource specifications specified either by map_enclosing_resource. module RequestPathIntrospection protected - def request_path - @request_path ||= params[:resource_path] || request.path + def path_of_request + @path_of_request ||= params[:resource_path] || request.path end - def nesting_request_path - @nesting_request_path ||= remove_namespace(remove_current_segment(request_path)) + def nesting_path_of_request + @nesting_path_of_request ||= remove_namespace(remove_current_segment(path_of_request)) end # returns an array of hashes like {:segment => 'forum', :singleton => false} def nesting_segments - @nesting_segments ||= segments_for_path_and_keys(nesting_request_path, param_keys) + @nesting_segments ||= segments_for_path_and_keys(nesting_path_of_request, param_keys) end # returns an array of segments correspopnding to the namespace of the controller. @@ -25,7 +25,7 @@ def nesting_segments def namespace_segments unless @namespace_segments namespace = controller_path.sub(%r(#{controller_name}$), '') - @namespace_segments = (request_path =~ %r(^/#{namespace}) ? namespace.split('/') : []) + @namespace_segments = (path_of_request =~ %r(^/#{namespace}) ? namespace.split('/') : []) end @namespace_segments end @@ -82,4 +82,4 @@ def segment_for_key(key) end end end -end \ No newline at end of file +end diff --git a/spec/controllers/tags_controller_spec.rb b/spec/controllers/tags_controller_spec.rb index 2b67fb3..f5c5dde 100644 --- a/spec/controllers/tags_controller_spec.rb +++ b/spec/controllers/tags_controller_spec.rb @@ -9,7 +9,7 @@ @tag.stub!(:to_param).and_return('2') Tag.stub!(:find).and_return(@tag) - @controller.stub!(:request_path).and_return('/tags/2') + @controller.stub!(:path_of_request).and_return('/tags/2') get :show, :id => "2" end @@ -67,7 +67,7 @@ end def do_get - @controller.stub!(:request_path).and_return('/tags/index') + @controller.stub!(:path_of_request).and_return('/tags/index') get :index end @@ -81,4 +81,4 @@ def do_get assigns[:tags].should == @tags end end -end \ No newline at end of file +end diff --git a/spec/controllers/tags_controller_via_account_info_spec.rb b/spec/controllers/tags_controller_via_account_info_spec.rb index 81f6cb4..42ef9fa 100644 --- a/spec/controllers/tags_controller_via_account_info_spec.rb +++ b/spec/controllers/tags_controller_via_account_info_spec.rb @@ -25,7 +25,7 @@ def setup_mocks @tag.stub!(:to_param).and_return('2') @info_tags.stub!(:find).and_return(@tag) - @controller.stub!(:request_path).and_return('/account/info/tags/2') + @controller.stub!(:path_of_request).and_return('/account/info/tags/2') get :show, :id => 2 end @@ -68,7 +68,7 @@ def setup_mocks @other_tag = Tag.create @controller.instance_variable_set('@current_user', @account) - @controller.stub!(:request_path).and_return('/account/info/tags') + @controller.stub!(:path_of_request).and_return('/account/info/tags') get :index @resource_service = controller.send :resource_service end @@ -105,7 +105,7 @@ def setup_mocks end def do_get - @controller.stub!(:request_path).and_return('/account/info/tags') + @controller.stub!(:path_of_request).and_return('/account/info/tags') get :index end @@ -129,4 +129,4 @@ def do_get do_get end end -end \ No newline at end of file +end diff --git a/spec/controllers/tags_controller_via_forum_post_spec.rb b/spec/controllers/tags_controller_via_forum_post_spec.rb index 01ea02d..15d18cb 100644 --- a/spec/controllers/tags_controller_via_forum_post_spec.rb +++ b/spec/controllers/tags_controller_via_forum_post_spec.rb @@ -27,7 +27,7 @@ def setup_mocks @tag.stub!(:to_param).and_return('3') @post_tags.stub!(:find).and_return(@tag) - @controller.stub!(:request_path).and_return('/forums/1/posts/1/tags/3') + @controller.stub!(:path_of_request).and_return('/forums/1/posts/1/tags/3') get :show, :forum_id => "1", :post_id => "2", :id => "3" end @@ -69,7 +69,7 @@ def setup_mocks @other_post = Post.create :forum_id => @forum.id @other_tag = Tag.create :taggable_id => @other_post.id, :taggable_type => 'Post' - @controller.stub!(:request_path).and_return("/forums/:id/posts/:id/tags") + @controller.stub!(:path_of_request).and_return("/forums/:id/posts/:id/tags") get :index, :forum_id => @forum.id, :post_id => @post.id @resource_service = controller.send :resource_service end @@ -106,7 +106,7 @@ def setup_mocks end def do_get - @controller.stub!(:request_path).and_return("/forums/1/posts/2/tags") + @controller.stub!(:path_of_request).and_return("/forums/1/posts/2/tags") get :index, :forum_id => '1', :post_id => '2' end @@ -131,4 +131,4 @@ def do_get @controller.resource_service.should == @post_tags end end -end \ No newline at end of file +end diff --git a/spec/lib/load_enclosing_resources_spec.rb b/spec/lib/load_enclosing_resources_spec.rb index 22b62c2..0472e8e 100644 --- a/spec/lib/load_enclosing_resources_spec.rb +++ b/spec/lib/load_enclosing_resources_spec.rb @@ -11,7 +11,7 @@ def setup_tags_controller(options = {}) def setup_common @controller = @klass.new - @controller.stub!(:request_path).and_return('') + @controller.stub!(:path_of_request).and_return('') # stub :load_enclosing_resource_from_specification, increase enclosing_resources by one, and return a mock resource @controller.stub!(:load_enclosing_resource_from_specification).and_return do |name, _| mock("resource: #{name}").tap do |resource| diff --git a/spec/lib/request_path_introspection_spec.rb b/spec/lib/request_path_introspection_spec.rb index 879f486..31e4e09 100644 --- a/spec/lib/request_path_introspection_spec.rb +++ b/spec/lib/request_path_introspection_spec.rb @@ -15,61 +15,61 @@ class User < ActiveRecord::Base; end @controller.stub!(:request).and_return(mock('request', :path => '/forums')) end - describe "#request_path" do + describe "#path_of_request" do it "should default to request.path" do - @controller.send(:request_path).should == '/forums' + @controller.send(:path_of_request).should == '/forums' end it " should be params[:resource_path], when set" do @controller.params[:resource_path] = '/foo' - @controller.send(:request_path).should == '/foo' + @controller.send(:path_of_request).should == '/foo' end end - describe "#nesting_request_path" do + describe "#nesting_path_of_request" do it "should remove the controller_name segment" do - @controller.stub!(:request_path).and_return('/users/1/forums/2') - @controller.send(:nesting_request_path).should == '/users/1' + @controller.stub!(:path_of_request).and_return('/users/1/forums/2') + @controller.send(:nesting_path_of_request).should == '/users/1' end it "when resource_specification present, whould remove taht segment" do @controller.stub!(:resource_specification).and_return(Ardes::ResourcesController::Specification.new(:forum, :class => RequestPathIntrospectionSpec::Forum, :segment => 'foromas')) - @controller.stub!(:request_path).and_return('/users/1/foromas/2') - @controller.send(:nesting_request_path).should == '/users/1' + @controller.stub!(:path_of_request).and_return('/users/1/foromas/2') + @controller.send(:nesting_path_of_request).should == '/users/1' end it "should remove only the controller_name segment, when nesting is same name" do - @controller.stub!(:request_path).and_return('/forums/1/forums/2') - @controller.send(:nesting_request_path).should == '/forums/1' + @controller.stub!(:path_of_request).and_return('/forums/1/forums/2') + @controller.send(:nesting_path_of_request).should == '/forums/1' end it "should remove the controller_name segment, even when id matches controller name" do - @controller.stub!(:request_path).and_return('/forums/1/forums/forums.atom') - @controller.send(:nesting_request_path).should == '/forums/1' + @controller.stub!(:path_of_request).and_return('/forums/1/forums/forums.atom') + @controller.send(:nesting_path_of_request).should == '/forums/1' end it "should remove only the controller_name segment even when nesting is same name" do @controller.stub!(:resource_specification).and_return(Ardes::ResourcesController::Specification.new(:forum, :class => RequestPathIntrospectionSpec::Forum, :singleton => true)) - @controller.stub!(:request_path).and_return('/users/1/forum/forum.atom') - @controller.send(:nesting_request_path).should == '/users/1/forum' + @controller.stub!(:path_of_request).and_return('/users/1/forum/forum.atom') + @controller.send(:nesting_path_of_request).should == '/users/1/forum' end it "should remove any controller namespace" do @controller.stub!(:controller_path).and_return('some/name/space/forums') - @controller.stub!(:request_path).and_return('/some/name/space/users/1/secret/forums') - @controller.send(:nesting_request_path).should == '/users/1/secret' + @controller.stub!(:path_of_request).and_return('/some/name/space/users/1/secret/forums') + @controller.send(:nesting_path_of_request).should == '/users/1/secret' end end - it "#namespace_segments should return [] segments if NOT present in request_path" do + it "#namespace_segments should return [] segments if NOT present in path_of_request" do @controller.stub!(:controller_path).and_return('some/name/space/forums') - @controller.stub!(:request_path).and_return('/SAM/name/space/users/1/secret/forums') + @controller.stub!(:path_of_request).and_return('/SAM/name/space/users/1/secret/forums') @controller.send(:namespace_segments).should == [] end - it "#namespace_segments should return namespace segments if present in request_path" do + it "#namespace_segments should return namespace segments if present in path_of_request" do @controller.stub!(:controller_path).and_return('some/name/space/forums') - @controller.stub!(:request_path).and_return('/some/name/space/users/1/secret/forums') + @controller.stub!(:path_of_request).and_return('/some/name/space/users/1/secret/forums') @controller.send(:namespace_segments).should == ['some', 'name', 'space'] end @@ -127,4 +127,4 @@ class User < ActiveRecord::Base; end end end end -end \ No newline at end of file +end diff --git a/spec/lib/resources_controller_spec.rb b/spec/lib/resources_controller_spec.rb index f76ed8c..290146e 100644 --- a/spec/lib/resources_controller_spec.rb +++ b/spec/lib/resources_controller_spec.rb @@ -22,7 +22,7 @@ @controller = TagsController.new info = mock_model(Info, :tags => []) @controller.stub!(:current_user).and_return(mock_model(User, :info => info)) - @controller.stub!(:request_path).and_return('/account/info/tags') + @controller.stub!(:path_of_request).and_return('/account/info/tags') @controller.stub!(:params).and_return({}) @controller.send :load_enclosing_resources end