Skip to content
This repository
Browse code

Handle irregular plurals in polymorphic_urls [#2212 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit d771e7d17f4a2c175676f7c8354aa5b161b63c2e 1 parent 3c64c9a
Mike Gunderloy authored lifo committed
7  actionpack/lib/action_controller/polymorphic_routes.rb
@@ -163,7 +163,8 @@ def build_named_route_call(records, namespace, inflection, options = {})
163 163
             if parent.is_a?(Symbol) || parent.is_a?(String)
164 164
               string << "#{parent}_"
165 165
             else
166  
-              string << "#{RecordIdentifier.__send__("singular_class_name", parent)}_"
  166
+              string << "#{RecordIdentifier.__send__("plural_class_name", parent)}".singularize
  167
+              string << "_"
167 168
             end
168 169
           end
169 170
         end
@@ -171,7 +172,9 @@ def build_named_route_call(records, namespace, inflection, options = {})
171 172
         if record.is_a?(Symbol) || record.is_a?(String)
172 173
           route << "#{record}_"
173 174
         else
174  
-          route << "#{RecordIdentifier.__send__("#{inflection}_class_name", record)}_"
  175
+          route << "#{RecordIdentifier.__send__("plural_class_name", record)}"
  176
+          route = route.singularize if inflection == :singular
  177
+          route << "_"
175 178
         end
176 179
 
177 180
         action_prefix(options) + namespace + route + routing_type(options).to_s
85  actionpack/test/controller/polymorphic_routes_test.rb
@@ -18,6 +18,20 @@ class Tag < Article
18 18
   def response_id; 1 end
19 19
 end
20 20
 
  21
+class Tax
  22
+  attr_reader :id
  23
+  def save; @id = 1 end
  24
+  def new_record?; @id.nil? end
  25
+  def name
  26
+    model = self.class.name.downcase
  27
+    @id.nil? ? "new #{model}" : "#{model} ##{@id}"
  28
+  end
  29
+end
  30
+
  31
+class Fax < Tax
  32
+  def store_id; 1 end
  33
+end
  34
+
21 35
 # TODO: test nested models
22 36
 class Response::Nested < Response; end
23 37
 
@@ -27,6 +41,8 @@ class PolymorphicRoutesTest < ActiveSupport::TestCase
27 41
   def setup
28 42
     @article = Article.new
29 43
     @response = Response.new
  44
+    @tax = Tax.new
  45
+    @fax = Fax.new
30 46
   end
31 47
 
32 48
   def test_with_record
@@ -205,4 +221,73 @@ def test_polymorphic_path_does_not_modify_arguments
205 221
       polymorphic_url(path)
206 222
     end
207 223
   end
  224
+
  225
+  # Tests for names where .plural.singular doesn't round-trip
  226
+  def test_with_irregular_plural_record
  227
+    @tax.save
  228
+    expects(:taxis_url).with(@tax)
  229
+    polymorphic_url(@tax)
  230
+  end
  231
+
  232
+  def test_with_irregular_plural_new_record
  233
+    expects(:taxes_url).with()
  234
+    @tax.expects(:new_record?).returns(true)
  235
+    polymorphic_url(@tax)
  236
+  end
  237
+
  238
+  def test_with_irregular_plural_record_and_action
  239
+    expects(:new_taxis_url).with()
  240
+    @tax.expects(:new_record?).never
  241
+    polymorphic_url(@tax, :action => 'new')
  242
+  end
  243
+
  244
+  def test_irregular_plural_url_helper_prefixed_with_new
  245
+    expects(:new_taxis_url).with()
  246
+    new_polymorphic_url(@tax)
  247
+  end
  248
+
  249
+  def test_irregular_plural_url_helper_prefixed_with_edit
  250
+    @tax.save
  251
+    expects(:edit_taxis_url).with(@tax)
  252
+    edit_polymorphic_url(@tax)
  253
+  end
  254
+
  255
+  def test_with_nested_irregular_plurals
  256
+    @fax.save
  257
+    expects(:taxis_faxis_url).with(@tax, @fax)
  258
+    polymorphic_url([@tax, @fax])
  259
+  end
  260
+
  261
+  def test_with_nested_unsaved_irregular_plurals
  262
+    expects(:taxis_faxes_url).with(@tax)
  263
+    polymorphic_url([@tax, @fax])
  264
+  end
  265
+
  266
+  def test_new_with_irregular_plural_array_and_namespace
  267
+    expects(:new_admin_taxis_url).with()
  268
+    polymorphic_url([:admin, @tax], :action => 'new')
  269
+  end
  270
+
  271
+  def test_unsaved_with_irregular_plural_array_and_namespace
  272
+    expects(:admin_taxes_url).with()
  273
+    polymorphic_url([:admin, @tax])
  274
+  end
  275
+
  276
+  def test_nesting_with_irregular_plurals_and_array_ending_in_singleton_resource
  277
+    expects(:taxis_faxis_url).with(@tax)
  278
+    polymorphic_url([@tax, :faxis])
  279
+  end
  280
+
  281
+  def test_with_array_containing_single_irregular_plural_object
  282
+    @tax.save
  283
+    expects(:taxis_url).with(@tax)
  284
+    polymorphic_url([nil, @tax])
  285
+  end
  286
+
  287
+  def test_with_array_containing_single_name_irregular_plural
  288
+    @tax.save
  289
+    expects(:taxes_url)
  290
+    polymorphic_url([:taxes])
  291
+  end
  292
+
208 293
 end

0 notes on commit d771e7d

Please sign in to comment.
Something went wrong with that request. Please try again.