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

pass habtm :autosave to underlying hm:t association. Closes #13923.

This commit is contained in:
Yves Senn 2014-02-03 14:48:59 +01:00
parent b7c7cb1a63
commit 584a46479b
4 changed files with 36 additions and 9 deletions

View file

@ -1,3 +1,10 @@
* Pass `has_and_belongs_to_many` `:autosave` option to
the underlying `has_many :through` association.
Fixes #13923.
*Yves Senn*
* PostgreSQL implementation of SchemaStatements#index_name_exists?
The database agnostic implementation does not detect with indexes that are

View file

@ -1585,7 +1585,7 @@ module ActiveRecord
hm_options[:through] = middle_reflection.name
hm_options[:source] = join_model.right_reflection.name
[:before_add, :after_add, :before_remove, :after_remove].each do |k|
[:before_add, :after_add, :before_remove, :after_remove, :autosave].each do |k|
hm_options[k] = options[k] if options.key? k
end

View file

@ -1183,15 +1183,15 @@ module AutosaveAssociationOnACollectionAssociationTests
end
def test_should_default_invalid_error_from_i18n
I18n.backend.store_translations(:en, :activerecord => {:errors => { :models =>
{ @association_name.to_s.singularize.to_sym => { :blank => "cannot be blank" } }
I18n.backend.store_translations(:en, activerecord: {errors: { models:
{ @associated_model_name.to_s.to_sym => { blank: "cannot be blank" } }
}})
@pirate.send(@association_name).build(:name => '')
@pirate.send(@association_name).build(name: '')
assert !@pirate.valid?
assert_equal ["cannot be blank"], @pirate.errors["#{@association_name}.name"]
assert_equal ["#{@association_name.to_s.titleize} name cannot be blank"], @pirate.errors.full_messages
assert_equal ["#{@association_name.to_s.humanize} name cannot be blank"], @pirate.errors.full_messages
assert @pirate.errors[@association_name].empty?
ensure
I18n.backend = I18n::Backend::Simple.new
@ -1307,6 +1307,7 @@ class TestAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCase
def setup
super
@association_name = :birds
@associated_model_name = :bird
@pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
@child_1 = @pirate.birds.create(:name => 'Posideons Killer')
@ -1321,12 +1322,30 @@ class TestAutosaveAssociationOnAHasAndBelongsToManyAssociation < ActiveRecord::T
def setup
super
@association_name = :parrots
@association_name = :autosaved_parrots
@associated_model_name = :parrot
@habtm = true
@pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
@child_1 = @pirate.parrots.create(:name => 'Posideons Killer')
@child_2 = @pirate.parrots.create(:name => 'Killer bandita Dionne')
@pirate = Pirate.create(catchphrase: "Don' botharrr talkin' like one, savvy?")
@child_1 = @pirate.parrots.create(name: 'Posideons Killer')
@child_2 = @pirate.parrots.create(name: 'Killer bandita Dionne')
end
include AutosaveAssociationOnACollectionAssociationTests
end
class TestAutosaveAssociationOnAHasAndBelongsToManyAssociationWithAcceptsNestedAttributes < ActiveRecord::TestCase
self.use_transactional_fixtures = false unless supports_savepoints?
def setup
super
@association_name = :parrots
@associated_model_name = :parrot
@habtm = true
@pirate = Pirate.create(catchphrase: "Don' botharrr talkin' like one, savvy?")
@child_1 = @pirate.parrots.create(name: 'Posideons Killer')
@child_2 = @pirate.parrots.create(name: 'Killer bandita Dionne')
end
include AutosaveAssociationOnACollectionAssociationTests

View file

@ -13,6 +13,7 @@ class Pirate < ActiveRecord::Base
:after_add => proc {|p,pa| p.ship_log << "after_adding_proc_parrot_#{pa.id || '<new>'}"},
:before_remove => proc {|p,pa| p.ship_log << "before_removing_proc_parrot_#{pa.id}"},
:after_remove => proc {|p,pa| p.ship_log << "after_removing_proc_parrot_#{pa.id}"}
has_and_belongs_to_many :autosaved_parrots, class_name: "Parrot", autosave: true
has_many :treasures, :as => :looter
has_many :treasure_estimates, :through => :treasures, :source => :price_estimates