Skip to content

Commit

Permalink
Handle irregular plurals in polymorphic_urls [#2212 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
Mike Gunderloy authored and lifo committed Mar 12, 2009
1 parent 3c64c9a commit d771e7d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
7 changes: 5 additions & 2 deletions actionpack/lib/action_controller/polymorphic_routes.rb
Expand Up @@ -163,15 +163,18 @@ def build_named_route_call(records, namespace, inflection, options = {})
if parent.is_a?(Symbol) || parent.is_a?(String)
string << "#{parent}_"
else
string << "#{RecordIdentifier.__send__("singular_class_name", parent)}_"
string << "#{RecordIdentifier.__send__("plural_class_name", parent)}".singularize
string << "_"
end
end
end

if record.is_a?(Symbol) || record.is_a?(String)
route << "#{record}_"
else
route << "#{RecordIdentifier.__send__("#{inflection}_class_name", record)}_"
route << "#{RecordIdentifier.__send__("plural_class_name", record)}"
route = route.singularize if inflection == :singular
route << "_"
end

action_prefix(options) + namespace + route + routing_type(options).to_s
Expand Down
85 changes: 85 additions & 0 deletions actionpack/test/controller/polymorphic_routes_test.rb
Expand Up @@ -18,6 +18,20 @@ class Tag < Article
def response_id; 1 end
end

class Tax
attr_reader :id
def save; @id = 1 end
def new_record?; @id.nil? end
def name
model = self.class.name.downcase
@id.nil? ? "new #{model}" : "#{model} ##{@id}"
end
end

class Fax < Tax
def store_id; 1 end
end

# TODO: test nested models
class Response::Nested < Response; end

Expand All @@ -27,6 +41,8 @@ class PolymorphicRoutesTest < ActiveSupport::TestCase
def setup
@article = Article.new
@response = Response.new
@tax = Tax.new
@fax = Fax.new
end

def test_with_record
Expand Down Expand Up @@ -205,4 +221,73 @@ def test_polymorphic_path_does_not_modify_arguments
polymorphic_url(path)
end
end

# Tests for names where .plural.singular doesn't round-trip
def test_with_irregular_plural_record
@tax.save
expects(:taxis_url).with(@tax)
polymorphic_url(@tax)
end

def test_with_irregular_plural_new_record
expects(:taxes_url).with()
@tax.expects(:new_record?).returns(true)
polymorphic_url(@tax)
end

def test_with_irregular_plural_record_and_action
expects(:new_taxis_url).with()
@tax.expects(:new_record?).never
polymorphic_url(@tax, :action => 'new')
end

def test_irregular_plural_url_helper_prefixed_with_new
expects(:new_taxis_url).with()
new_polymorphic_url(@tax)
end

def test_irregular_plural_url_helper_prefixed_with_edit
@tax.save
expects(:edit_taxis_url).with(@tax)
edit_polymorphic_url(@tax)
end

def test_with_nested_irregular_plurals
@fax.save
expects(:taxis_faxis_url).with(@tax, @fax)
polymorphic_url([@tax, @fax])
end

def test_with_nested_unsaved_irregular_plurals
expects(:taxis_faxes_url).with(@tax)
polymorphic_url([@tax, @fax])
end

def test_new_with_irregular_plural_array_and_namespace
expects(:new_admin_taxis_url).with()
polymorphic_url([:admin, @tax], :action => 'new')
end

def test_unsaved_with_irregular_plural_array_and_namespace
expects(:admin_taxes_url).with()
polymorphic_url([:admin, @tax])
end

def test_nesting_with_irregular_plurals_and_array_ending_in_singleton_resource
expects(:taxis_faxis_url).with(@tax)
polymorphic_url([@tax, :faxis])
end

def test_with_array_containing_single_irregular_plural_object
@tax.save
expects(:taxis_url).with(@tax)
polymorphic_url([nil, @tax])
end

def test_with_array_containing_single_name_irregular_plural
@tax.save
expects(:taxes_url)
polymorphic_url([:taxes])
end

end

0 comments on commit d771e7d

Please sign in to comment.