From 5c565a294224911ba7b3929baabf3465172b40f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 1 Jun 2012 07:40:41 -0700 Subject: [PATCH] Merge pull request #6588 from nbibler/polymorphic_to_model Correct the use of to_model in polymorphic routing --- .../routing/polymorphic_routes.rb | 8 +++++- .../activerecord/polymorphic_routes_test.rb | 28 ++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb index 8fde667108..86ce7a83b9 100644 --- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb +++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb @@ -95,7 +95,7 @@ def polymorphic_url(record_or_hash_or_array, options = {}) end record = extract_record(record_or_hash_or_array) - record = record.to_model if record.respond_to?(:to_model) + record = convert_to_model(record) args = Array === record_or_hash_or_array ? record_or_hash_or_array.dup : @@ -122,6 +122,8 @@ def polymorphic_url(record_or_hash_or_array, options = {}) args.last.kind_of?(Hash) ? args.last.merge!(url_options) : args << url_options end + args.collect! { |a| convert_to_model(a) } + (proxy || self).send(named_route, *args) end @@ -152,6 +154,10 @@ def action_prefix(options) options[:action] ? "#{options[:action]}_" : '' end + def convert_to_model(object) + object.respond_to?(:to_model) ? object.to_model : object + end + def routing_type(options) options[:routing_type] || :url end diff --git a/actionpack/test/activerecord/polymorphic_routes_test.rb b/actionpack/test/activerecord/polymorphic_routes_test.rb index 90e7f4ae59..afb714484b 100644 --- a/actionpack/test/activerecord/polymorphic_routes_test.rb +++ b/actionpack/test/activerecord/polymorphic_routes_test.rb @@ -25,6 +25,24 @@ class Series < ActiveRecord::Base self.table_name = 'projects' end +class ModelDelegator < ActiveRecord::Base + self.table_name = 'projects' + + def to_model + ModelDelegate.new + end +end + +class ModelDelegate + def self.model_name + ActiveModel::Name.new(self) + end + + def to_param + 'overridden' + end +end + module Blog class Post < ActiveRecord::Base self.table_name = 'projects' @@ -50,6 +68,7 @@ def setup @bid = Bid.new @tax = Tax.new @fax = Fax.new + @delegator = ModelDelegator.new @series = Series.new @blog_post = Blog::Post.new @blog_blog = Blog::Blog.new @@ -439,6 +458,13 @@ def test_uncountable_resource end end + def test_routing_a_to_model_delegate + with_test_routes do + @delegator.save + assert_equal "http://example.com/model_delegates/overridden", polymorphic_url(@delegator) + end + end + def with_namespaced_routes(name) with_routing do |set| set.draw do @@ -469,6 +495,7 @@ def with_test_routes(options = {}) resource :bid end resources :series + resources :model_delegates end self.class.send(:include, @routes.url_helpers) @@ -516,5 +543,4 @@ def with_admin_and_site_test_routes(options = {}) yield end end - end