Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch to deal with named_route name collisions... #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ardes/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')}
Expand Down
14 changes: 7 additions & 7 deletions lib/ardes/resources_controller/request_path_introspection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -82,4 +82,4 @@ def segment_for_key(key)
end
end
end
end
end
6 changes: 3 additions & 3 deletions spec/controllers/tags_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -81,4 +81,4 @@ def do_get
assigns[:tags].should == @tags
end
end
end
end
8 changes: 4 additions & 4 deletions spec/controllers/tags_controller_via_account_info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -129,4 +129,4 @@ def do_get
do_get
end
end
end
end
8 changes: 4 additions & 4 deletions spec/controllers/tags_controller_via_forum_post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -131,4 +131,4 @@ def do_get
@controller.resource_service.should == @post_tags
end
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/load_enclosing_resources_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
42 changes: 21 additions & 21 deletions spec/lib/request_path_introspection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -127,4 +127,4 @@ class User < ActiveRecord::Base; end
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/resources_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down