Skip to content

Commit

Permalink
Merge pull request #241 from dim/master
Browse files Browse the repository at this point in the history
Incorrect `collection_path` is generated for uncountable shallow resources
  • Loading branch information
joelmoss committed Nov 9, 2012
2 parents 797e2ba + ae42352 commit aa52a0a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/inherited_resources/url_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def create_resources_url_helpers!
resource_segments, resource_ivars = [], []
resource_config = self.resources_configuration[:self]

singleton = self.resources_configuration[:self][:singleton]
singleton = resource_config[:singleton]
uncountable = !singleton && resource_config[:route_collection_name] == resource_config[:route_instance_name]
polymorphic = self.parents_symbols.include?(:polymorphic)

# Add route_prefix if any.
Expand Down Expand Up @@ -123,9 +124,8 @@ def create_resources_url_helpers!
end

# If route is uncountable then add "_index" suffix to collection index route name
#
if !singleton && resource_config[:route_collection_name] == resource_config[:route_instance_name]
collection_segments << :index
if uncountable
collection_segments << :"#{collection_segments.pop}_index"
end

generate_url_and_path_helpers nil, :collection, collection_segments, collection_ivars
Expand Down
40 changes: 40 additions & 0 deletions test/url_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ class SheepController < InheritedResources::Base
belongs_to :news, :table, :polymorphic => true
end

class Fish
extend ActiveModel::Naming
end
class FishController < InheritedResources::Base
belongs_to :bed, :shallow => true
end

class Desk
extend ActiveModel::Naming
end
Expand Down Expand Up @@ -505,6 +512,39 @@ def test_url_helpers_on_polymorphic_belongs_to_using_uncountable
controller.send("parent_url", :arg)
end

def test_url_helpers_on_shallow_belongs_to_using_uncountable
fish = Fish.new
bed = Bed.new

new_fish = Fish.new
new_fish.stubs(:persisted?).returns(false)
Sheep.stubs(:new).returns(new_fish)

controller = FishController.new
controller.instance_variable_set('@bed', bed)
controller.instance_variable_set('@fish', fish)

[:url, :path].each do |path_or_url|
controller.expects("bed_fish_index_#{path_or_url}").with(bed, {}).once
controller.send("collection_#{path_or_url}")

controller.expects("fish_#{path_or_url}").with(fish, {}).once
controller.send("resource_#{path_or_url}")

controller.expects("new_bed_fish_#{path_or_url}").with(bed, {}).once
controller.send("new_resource_#{path_or_url}")

controller.expects("edit_fish_#{path_or_url}").with(fish, {}).once
controller.send("edit_resource_#{path_or_url}")

controller.expects("bed_#{path_or_url}").with(bed, {}).once
controller.send("parent_#{path_or_url}")

controller.expects("edit_bed_#{path_or_url}").with(bed, {}).once
controller.send("edit_parent_#{path_or_url}")
end
end

def test_url_helpers_on_namespaced_polymorphic_belongs_to
house = House.new
desk = Desk.new
Expand Down

0 comments on commit aa52a0a

Please sign in to comment.