1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix form_for to work with objects that implement to_model

Previously, if you tried to use form_for with a presenter object
that implements to_model, it would crash in
action_dispatch/routing/polymorphic_routes.rb when asking the presenter
whether it is .persisted?

Now, we always ask .persisted? of the to_model object instead.

This seems to been an issue since 1606fc9d84

Signed-off-by: Eugenia Dellapenna <eugenia.dellapenna@gmail.com>
This commit is contained in:
Travis Grathwell 2014-12-29 18:12:03 -08:00 committed by Eugenia Dellapenna
parent 241ccaef88
commit 3efd90ac5b
2 changed files with 21 additions and 10 deletions

View file

@ -247,7 +247,7 @@ module ActionDispatch
args = [] args = []
model = record.to_model model = record.to_model
name = if record.persisted? name = if model.persisted?
args << model args << model
model.model_name.singular_route_key model.model_name.singular_route_key
else else
@ -290,11 +290,12 @@ module ActionDispatch
when Class when Class
@key_strategy.call record.model_name @key_strategy.call record.model_name
else else
if record.persisted? model = record.to_model
args << record.to_model if model.persisted?
record.to_model.model_name.singular_route_key args << model
model.model_name.singular_route_key
else else
@key_strategy.call record.to_model.model_name @key_strategy.call model.model_name
end end
end end

View file

@ -25,15 +25,17 @@ class Series < ActiveRecord::Base
self.table_name = 'projects' self.table_name = 'projects'
end end
class ModelDelegator < ActiveRecord::Base class ModelDelegator
self.table_name = 'projects'
def to_model def to_model
ModelDelegate.new ModelDelegate.new
end end
end end
class ModelDelegate class ModelDelegate
def persisted?
true
end
def model_name def model_name
ActiveModel::Name.new(self.class) ActiveModel::Name.new(self.class)
end end
@ -605,13 +607,18 @@ class PolymorphicRoutesTest < ActionController::TestCase
end end
end end
def test_routing_a_to_model_delegate def test_routing_to_a_model_delegate
with_test_routes do with_test_routes do
@delegator.save
assert_url "http://example.com/model_delegates/overridden", @delegator assert_url "http://example.com/model_delegates/overridden", @delegator
end end
end end
def test_nested_routing_to_a_model_delegate
with_test_routes do
assert_url "http://example.com/foo/model_delegates/overridden", [:foo, @delegator]
end
end
def with_namespaced_routes(name) def with_namespaced_routes(name)
with_routing do |set| with_routing do |set|
set.draw do set.draw do
@ -645,6 +652,9 @@ class PolymorphicRoutesTest < ActionController::TestCase
end end
resources :series resources :series
resources :model_delegates resources :model_delegates
namespace :foo do
resources :model_delegates
end
end end
extend @routes.url_helpers extend @routes.url_helpers