diff options
author | Nathaniel Bibler <git@nathanielbibler.com> | 2012-06-01 10:11:01 -0400 |
---|---|---|
committer | Nathaniel Bibler <git@nathanielbibler.com> | 2012-06-01 10:38:30 -0400 |
commit | 537ede912895d421b24acfcbc86daf08f8f22157 (patch) | |
tree | f6584959a395e89be9aec64ba1dc8f4ac708bab1 /actionpack | |
parent | b57fa0bf690b299ba4d8a0051bf06b9cecd3975a (diff) | |
download | rails-537ede912895d421b24acfcbc86daf08f8f22157.tar.gz rails-537ede912895d421b24acfcbc86daf08f8f22157.tar.bz2 rails-537ede912895d421b24acfcbc86daf08f8f22157.zip |
Use to_model delegates for polymorphic route generation
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_dispatch/routing/polymorphic_routes.rb | 8 | ||||
-rw-r--r-- | actionpack/test/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 013cf93dbc..055761fed5 100644 --- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb +++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb @@ -97,7 +97,7 @@ module ActionDispatch 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 : @@ -124,6 +124,8 @@ module ActionDispatch 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 @@ -154,6 +156,10 @@ module ActionDispatch 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 @@ class PolymorphicRoutesTest < ActionController::TestCase @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 @@ class PolymorphicRoutesTest < ActionController::TestCase 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 @@ class PolymorphicRoutesTest < ActionController::TestCase resource :bid end resources :series + resources :model_delegates end self.class.send(:include, @routes.url_helpers) @@ -516,5 +543,4 @@ class PolymorphicRoutesTest < ActionController::TestCase yield end end - end |