class TestNestedAttributesInGeneral
Public Instance Methods
parrot_attributes=(attrs)
click to toggle source
Calls superclass method
# File activerecord/test/cases/nested_attributes_test.rb, line 200 def parrot_attributes=(attrs) super(attrs.merge(color: "blue")) end
test_a_model_should_respond_to_underscore_destroy_and_return_if_it_is_marked_for_destruction()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 84 def test_a_model_should_respond_to_underscore_destroy_and_return_if_it_is_marked_for_destruction ship = Ship.create!(name: "Nights Dirty Lightning") assert !ship._destroy ship.mark_for_destruction assert ship._destroy end
test_accepts_nested_attributes_for_can_be_overridden_in_subclasses()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 210 def test_accepts_nested_attributes_for_can_be_overridden_in_subclasses Pirate.accepts_nested_attributes_for(:parrot) mean_pirate_class = Class.new(Pirate) do accepts_nested_attributes_for :parrot end mean_pirate = mean_pirate_class.new mean_pirate.parrot_attributes = { name: "James" } assert_equal "James", mean_pirate.parrot.name end
test_allows_class_to_override_setter_and_call_super()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 197 def test_allows_class_to_override_setter_and_call_super mean_pirate_class = Class.new(Pirate) do accepts_nested_attributes_for :parrot def parrot_attributes=(attrs) super(attrs.merge(color: "blue")) end end mean_pirate = mean_pirate_class.new mean_pirate.parrot_attributes = { name: "James" } assert_equal "James", mean_pirate.parrot.name assert_equal "blue", mean_pirate.parrot.color end
test_base_should_have_an_empty_nested_attributes_options()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 21 def test_base_should_have_an_empty_nested_attributes_options assert_equal Hash.new, ActiveRecord::Base.nested_attributes_options end
test_destroy_works_independent_of_reject_if()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 150 def test_destroy_works_independent_of_reject_if Man.accepts_nested_attributes_for :interests, reject_if: proc { |attributes| true }, allow_destroy: true man = Man.create(name: "Jon") interest = man.interests.create(topic: "the ladies") man.update(interests_attributes: { _destroy: "1", id: interest.id }) assert man.reload.interests.empty? end
test_do_not_allow_assigning_foreign_key_when_reusing_existing_new_record()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 135 def test_do_not_allow_assigning_foreign_key_when_reusing_existing_new_record pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?") pirate.build_ship pirate.ship_attributes = { name: "Ship 1", pirate_id: pirate.id + 1 } assert_equal pirate.id, pirate.ship.pirate_id end
test_first_and_array_index_zero_methods_return_the_same_value_when_nested_attributes_are_set_to_update_existing_record()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 188 def test_first_and_array_index_zero_methods_return_the_same_value_when_nested_attributes_are_set_to_update_existing_record Man.accepts_nested_attributes_for(:interests) man = Man.create(name: "John") interest = man.interests.create topic: "gardening" man = Man.find man.id man.interests_attributes = [{ id: interest.id, topic: "gardening" }] assert_equal man.interests.first.topic, man.interests[0].topic end
test_has_many_association_updating_a_single_record()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 171 def test_has_many_association_updating_a_single_record Man.accepts_nested_attributes_for(:interests) man = Man.create(name: "John") interest = man.interests.create(topic: "photography") man.update(interests_attributes: { topic: "gardening", id: interest.id }) assert_equal "gardening", interest.reload.topic end
test_reject_if_is_not_short_circuited_if_allow_destroy_is_false()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 158 def test_reject_if_is_not_short_circuited_if_allow_destroy_is_false Pirate.accepts_nested_attributes_for :ship, reject_if: ->(a) { a[:name] == "The Golden Hind" }, allow_destroy: false pirate = Pirate.create!(catchphrase: "Stop wastin' me time", ship_attributes: { name: "White Pearl", _destroy: "1" }) assert_equal "White Pearl", pirate.reload.ship.name pirate.update!(ship_attributes: { id: pirate.ship.id, name: "The Golden Hind", _destroy: "1" }) assert_equal "White Pearl", pirate.reload.ship.name pirate.update!(ship_attributes: { id: pirate.ship.id, name: "Black Pearl", _destroy: "1" }) assert_equal "Black Pearl", pirate.reload.ship.name end
test_reject_if_method_with_arguments()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 99 def test_reject_if_method_with_arguments Pirate.accepts_nested_attributes_for :ship, reject_if: :reject_empty_ships_on_create pirate = Pirate.new(catchphrase: "Stop wastin' me time") pirate.ship_attributes = { name: "Red Pearl", _reject_me_if_new: true } assert_no_difference("Ship.count") { pirate.save! } # pirate.reject_empty_ships_on_create returns false for saved pirate records # in the previous step note that pirate gets saved but ship fails pirate.ship_attributes = { name: "Red Pearl", _reject_me_if_new: true } assert_difference("Ship.count") { pirate.save! } end
test_reject_if_method_without_arguments()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 91 def test_reject_if_method_without_arguments Pirate.accepts_nested_attributes_for :ship, reject_if: :new_record? pirate = Pirate.new(catchphrase: "Stop wastin' me time") pirate.ship_attributes = { name: "Black Pearl" } assert_no_difference("Ship.count") { pirate.save! } end
test_reject_if_with_a_proc_which_returns_true_always_for_has_many()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 142 def test_reject_if_with_a_proc_which_returns_true_always_for_has_many Man.accepts_nested_attributes_for :interests, reject_if: proc { |attributes| true } man = Man.create(name: "John") interest = man.interests.create(topic: "photography") man.update(interests_attributes: { topic: "gardening", id: interest.id }) assert_equal "photography", interest.reload.topic end
test_reject_if_with_a_proc_which_returns_true_always_for_has_one()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 120 def test_reject_if_with_a_proc_which_returns_true_always_for_has_one Pirate.accepts_nested_attributes_for :ship, reject_if: proc { |attributes| true } pirate = Pirate.create(catchphrase: "Stop wastin' me time") ship = pirate.create_ship(name: "s1") pirate.update(ship_attributes: { name: "s2", id: ship.id }) assert_equal "s1", ship.reload.name end
test_reject_if_with_blank_nested_attributes_id()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 179 def test_reject_if_with_blank_nested_attributes_id # When using a select list to choose an existing 'ship' id, with include_blank: true Pirate.accepts_nested_attributes_for :ship, reject_if: proc { |attributes| attributes[:id].blank? } pirate = Pirate.new(catchphrase: "Stop wastin' me time") pirate.ship_attributes = { id: "" } assert_nothing_raised { pirate.save! } end
test_reject_if_with_indifferent_keys()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 112 def test_reject_if_with_indifferent_keys Pirate.accepts_nested_attributes_for :ship, reject_if: proc { |attributes| attributes[:name].blank? } pirate = Pirate.new(catchphrase: "Stop wastin' me time") pirate.ship_attributes = { name: "Hello Pearl" } assert_difference("Ship.count") { pirate.save! } end
test_reuse_already_built_new_record()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 128 def test_reuse_already_built_new_record pirate = Pirate.new ship_built_first = pirate.build_ship pirate.ship_attributes = { name: "Ship 1" } assert_equal ship_built_first.object_id, pirate.ship.object_id end
test_should_add_a_proc_to_nested_attributes_options()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 25 def test_should_add_a_proc_to_nested_attributes_options assert_equal ActiveRecord::NestedAttributes::ClassMethods::REJECT_ALL_BLANK_PROC, Pirate.nested_attributes_options[:birds_with_reject_all_blank][:reject_if] [:parrots, :birds].each do |name| assert_instance_of Proc, Pirate.nested_attributes_options[name][:reject_if] end end
test_should_build_a_new_record_if_reject_all_blank_does_not_return_false()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 50 def test_should_build_a_new_record_if_reject_all_blank_does_not_return_false pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?") pirate.birds_with_reject_all_blank_attributes = [{ name: "Tweetie", color: "" }] pirate.save! assert_equal 1, pirate.birds_with_reject_all_blank.count assert_equal "Tweetie", pirate.birds_with_reject_all_blank.first.name end
test_should_disable_allow_destroy_by_default()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 73 def test_should_disable_allow_destroy_by_default Pirate.accepts_nested_attributes_for :ship pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?") ship = pirate.create_ship(name: "Nights Dirty Lightning") pirate.update(ship_attributes: { "_destroy" => true, :id => ship.id }) assert_nothing_raised { pirate.ship.reload } end
test_should_not_build_a_new_record_if_reject_all_blank_returns_false()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 42 def test_should_not_build_a_new_record_if_reject_all_blank_returns_false pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?") pirate.birds_with_reject_all_blank_attributes = [{ name: "", color: "" }] pirate.save! assert pirate.birds_with_reject_all_blank.empty? end
test_should_not_build_a_new_record_using_reject_all_even_if_destroy_is_given()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 34 def test_should_not_build_a_new_record_using_reject_all_even_if_destroy_is_given pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?") pirate.birds_with_reject_all_blank_attributes = [{ name: "", color: "", _destroy: "0" }] pirate.save! assert pirate.birds_with_reject_all_blank.empty? end
test_should_raise_an_ArgumentError_for_non_existing_associations()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 59 def test_should_raise_an_ArgumentError_for_non_existing_associations exception = assert_raise ArgumentError do Pirate.accepts_nested_attributes_for :honesty end assert_equal "No association found for name `honesty'. Has it been defined yet?", exception.message end
test_should_raise_an_UnknownAttributeError_for_non_existing_nested_attributes()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 66 def test_should_raise_an_UnknownAttributeError_for_non_existing_nested_attributes exception = assert_raise ActiveModel::UnknownAttributeError do Pirate.new(ship_attributes: { sail: true }) end assert_equal "unknown attribute 'sail' for Ship.", exception.message end