Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Generate correct URLs for uncountable, shallow child resources

  • Loading branch information...
commit ae4235257819822e0853666cb637074600ed1321 1 parent cf88956
@dim dim authored
Showing with 44 additions and 4 deletions.
  1. +4 −4 lib/inherited_resources/url_helpers.rb
  2. +40 −0 test/url_helpers_test.rb
View
8 lib/inherited_resources/url_helpers.rb
@@ -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.
@@ -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
View
40 test/url_helpers_test.rb
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.