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

Methods with association name are defined as public methods

This commit is contained in:
Akira Matsuda 2020-10-02 22:00:48 +09:00
parent 632cb80c1b
commit e57d9202a7
6 changed files with 65 additions and 65 deletions

View file

@ -65,7 +65,7 @@ module ActiveRecord::Associations::Builder # :nodoc:
end
end
record = o.send name
record = o.public_send name
if record && record.persisted?
if touch != true
record.public_send(touch_method, touch)

View file

@ -1022,7 +1022,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
d3 = find_all_ordered(className, [:posts, post_type])
assert_equal(d1[i], d3[i])
assert_equal_after_sort(d1[i].posts, d3[i].posts)
assert_equal_after_sort(d1[i].send(post_type), d2[i].send(post_type), d3[i].send(post_type))
assert_equal_after_sort(d1[i].public_send(post_type), d2[i].public_send(post_type), d3[i].public_send(post_type))
end
end
end
@ -1048,10 +1048,10 @@ class EagerAssociationTest < ActiveRecord::TestCase
d1.each_index do |i|
assert_equal(d1[i], d2[i])
firm_types.each do |type|
if (expected = d1[i].send(type)).nil?
assert_nil(d2[i].send(type))
if (expected = d1[i].public_send(type)).nil?
assert_nil(d2[i].public_send(type))
else
assert_equal(expected, d2[i].send(type))
assert_equal(expected, d2[i].public_send(type))
end
end
end

View file

@ -781,7 +781,7 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
Post.find(post_id).update_columns type: class_name
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
klass.table_name = "posts"
klass.send(association, association_name, as: :taggable, dependent: dependency)
klass.public_send(association, association_name, as: :taggable, dependent: dependency)
klass.find(post_id)
end
end

View file

@ -1087,7 +1087,7 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
association_name_with_callbacks = "birds_with_#{callback_type}_callbacks"
pirate = Pirate.new(catchphrase: "Arr")
pirate.send(association_name_with_callbacks).build(name: "Crowe the One-Eyed")
pirate.public_send(association_name_with_callbacks).build(name: "Crowe the One-Eyed")
expected = [
"before_adding_#{callback_type}_bird_<new>",
@ -1100,9 +1100,9 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
define_method("test_should_run_remove_callback_#{callback_type}s_for_has_many") do
association_name_with_callbacks = "birds_with_#{callback_type}_callbacks"
@pirate.send(association_name_with_callbacks).create!(name: "Crowe the One-Eyed")
@pirate.send(association_name_with_callbacks).each(&:mark_for_destruction)
child_id = @pirate.send(association_name_with_callbacks).first.id
@pirate.public_send(association_name_with_callbacks).create!(name: "Crowe the One-Eyed")
@pirate.public_send(association_name_with_callbacks).each(&:mark_for_destruction)
child_id = @pirate.public_send(association_name_with_callbacks).first.id
@pirate.ship_log.clear
@pirate.save
@ -1193,7 +1193,7 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
association_name_with_callbacks = "parrots_with_#{callback_type}_callbacks"
pirate = Pirate.new(catchphrase: "Arr")
pirate.send(association_name_with_callbacks).build(name: "Crowe the One-Eyed")
pirate.public_send(association_name_with_callbacks).build(name: "Crowe the One-Eyed")
expected = [
"before_adding_#{callback_type}_parrot_<new>",
@ -1206,9 +1206,9 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
define_method("test_should_run_remove_callback_#{callback_type}s_for_habtm") do
association_name_with_callbacks = "parrots_with_#{callback_type}_callbacks"
@pirate.send(association_name_with_callbacks).create!(name: "Crowe the One-Eyed")
@pirate.send(association_name_with_callbacks).each(&:mark_for_destruction)
child_id = @pirate.send(association_name_with_callbacks).first.id
@pirate.public_send(association_name_with_callbacks).create!(name: "Crowe the One-Eyed")
@pirate.public_send(association_name_with_callbacks).each(&:mark_for_destruction)
child_id = @pirate.public_send(association_name_with_callbacks).first.id
@pirate.ship_log.clear
@pirate.save
@ -1510,18 +1510,18 @@ end
module AutosaveAssociationOnACollectionAssociationTests
def test_should_automatically_save_the_associated_models
new_names = ["Grace OMalley", "Privateers Greed"]
@pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
@pirate.public_send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
@pirate.save
assert_equal new_names.sort, @pirate.reload.send(@association_name).map(&:name).sort
assert_equal new_names.sort, @pirate.reload.public_send(@association_name).map(&:name).sort
end
def test_should_automatically_save_bang_the_associated_models
new_names = ["Grace OMalley", "Privateers Greed"]
@pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
@pirate.public_send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
@pirate.save!
assert_equal new_names.sort, @pirate.reload.send(@association_name).map(&:name).sort
assert_equal new_names.sort, @pirate.reload.public_send(@association_name).map(&:name).sort
end
def test_should_update_children_when_autosave_is_true_and_parent_is_new_but_child_is_not
@ -1543,7 +1543,7 @@ module AutosaveAssociationOnACollectionAssociationTests
end
def test_should_automatically_validate_the_associated_models
@pirate.send(@association_name).each { |child| child.name = "" }
@pirate.public_send(@association_name).each { |child| child.name = "" }
assert_not_predicate @pirate, :valid?
assert_equal ["can't be blank"], @pirate.errors["#{@association_name}.name"]
@ -1551,7 +1551,7 @@ module AutosaveAssociationOnACollectionAssociationTests
end
def test_should_not_use_default_invalid_error_on_associated_models
@pirate.send(@association_name).build(name: "")
@pirate.public_send(@association_name).build(name: "")
assert_not_predicate @pirate, :valid?
assert_equal ["can't be blank"], @pirate.errors["#{@association_name}.name"]
@ -1563,7 +1563,7 @@ module AutosaveAssociationOnACollectionAssociationTests
{ @associated_model_name.to_s.to_sym => { blank: "cannot be blank" } }
} })
@pirate.send(@association_name).build(name: "")
@pirate.public_send(@association_name).build(name: "")
assert_not_predicate @pirate, :valid?
assert_equal ["cannot be blank"], @pirate.errors["#{@association_name}.name"]
@ -1574,7 +1574,7 @@ module AutosaveAssociationOnACollectionAssociationTests
end
def test_should_merge_errors_on_the_associated_models_onto_the_parent_even_if_it_is_not_valid
@pirate.send(@association_name).each { |child| child.name = "" }
@pirate.public_send(@association_name).each { |child| child.name = "" }
@pirate.catchphrase = nil
assert_not_predicate @pirate, :valid?
@ -1584,35 +1584,35 @@ module AutosaveAssociationOnACollectionAssociationTests
def test_should_allow_to_bypass_validations_on_the_associated_models_on_update
@pirate.catchphrase = ""
@pirate.send(@association_name).each { |child| child.name = "" }
@pirate.public_send(@association_name).each { |child| child.name = "" }
assert @pirate.save(validate: false)
# Oracle saves empty string as NULL
if current_adapter?(:OracleAdapter)
assert_equal [nil, nil, nil], [
@pirate.reload.catchphrase,
@pirate.send(@association_name).first.name,
@pirate.send(@association_name).last.name
@pirate.public_send(@association_name).first.name,
@pirate.public_send(@association_name).last.name
]
else
assert_equal ["", "", ""], [
@pirate.reload.catchphrase,
@pirate.send(@association_name).first.name,
@pirate.send(@association_name).last.name
@pirate.public_send(@association_name).first.name,
@pirate.public_send(@association_name).last.name
]
end
end
def test_should_validation_the_associated_models_on_create
assert_no_difference("#{ @association_name == :birds ? 'Bird' : 'Parrot' }.count") do
2.times { @pirate.send(@association_name).build }
2.times { @pirate.public_send(@association_name).build }
@pirate.save
end
end
def test_should_allow_to_bypass_validations_on_the_associated_models_on_create
assert_difference("#{ @association_name == :birds ? 'Bird' : 'Parrot' }.count", 2) do
2.times { @pirate.send(@association_name).build }
2.times { @pirate.public_send(@association_name).build }
@pirate.save(validate: false)
end
end
@ -1627,7 +1627,7 @@ module AutosaveAssociationOnACollectionAssociationTests
assert_equal "Posideons Killer", @child_1.reload.name
new_pirate = Pirate.new(catchphrase: "Arr")
new_child = new_pirate.send(@association_name).build(name: "Grace OMalley")
new_child = new_pirate.public_send(@association_name).build(name: "Grace OMalley")
new_child.cancel_save_from_callback = true
assert_no_difference "Pirate.count" do
@ -1638,14 +1638,14 @@ module AutosaveAssociationOnACollectionAssociationTests
end
def test_should_rollback_any_changes_if_an_exception_occurred_while_saving
before = [@pirate.catchphrase, *@pirate.send(@association_name).map(&:name)]
before = [@pirate.catchphrase, *@pirate.public_send(@association_name).map(&:name)]
new_names = ["Grace OMalley", "Privateers Greed"]
@pirate.catchphrase = "Arr"
@pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
@pirate.public_send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
# Stub the save method of the first child instance to raise an exception
class << @pirate.send(@association_name).first
class << @pirate.public_send(@association_name).first
def save(**)
super
raise "Oh noes!"
@ -1657,7 +1657,7 @@ module AutosaveAssociationOnACollectionAssociationTests
end
def test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that
@pirate.send(@association_name).each { |child| child.name = "" }
@pirate.public_send(@association_name).each { |child| child.name = "" }
assert_raise(ActiveRecord::RecordInvalid) do
@pirate.save!
end
@ -1666,12 +1666,12 @@ module AutosaveAssociationOnACollectionAssociationTests
def test_should_not_load_the_associated_models_if_they_were_not_loaded_yet
assert_queries(1) { @pirate.catchphrase = "Arr"; @pirate.save! }
@pirate.send(@association_name).load_target
@pirate.public_send(@association_name).load_target
assert_queries(3) do
@pirate.catchphrase = "Yarr"
new_names = ["Grace OMalley", "Privateers Greed"]
@pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
@pirate.public_send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
@pirate.save!
end
end

View file

@ -614,7 +614,7 @@ module NestedAttributesOnACollectionAssociationTests
:catchphrase => "Arr",
association_getter => { "foo" => { name: "Grace OMalley" } })
assert_equal 1, pirate.reload.send(@association_name).count
assert_equal 1, pirate.reload.public_send(@association_name).count
end
def test_should_take_a_hash_with_string_keys_and_assign_the_attributes_to_the_associated_models
@ -637,45 +637,45 @@ module NestedAttributesOnACollectionAssociationTests
def test_should_take_a_hash_and_assign_the_attributes_to_the_associated_models
@pirate.attributes = @alternate_params
assert_equal "Grace OMalley", @pirate.send(@association_name).first.name
assert_equal "Privateers Greed", @pirate.send(@association_name).last.name
assert_equal "Grace OMalley", @pirate.public_send(@association_name).first.name
assert_equal "Privateers Greed", @pirate.public_send(@association_name).last.name
end
def test_should_not_load_association_when_updating_existing_records
@pirate.reload
@pirate.send(association_setter, [{ id: @child_1.id, name: "Grace OMalley" }])
assert_not_predicate @pirate.send(@association_name), :loaded?
assert_not_predicate @pirate.public_send(@association_name), :loaded?
@pirate.save
assert_not_predicate @pirate.send(@association_name), :loaded?
assert_not_predicate @pirate.public_send(@association_name), :loaded?
assert_equal "Grace OMalley", @child_1.reload.name
end
def test_should_not_overwrite_unsaved_updates_when_loading_association
@pirate.reload
@pirate.send(association_setter, [{ id: @child_1.id, name: "Grace OMalley" }])
assert_equal "Grace OMalley", @pirate.send(@association_name).load_target.find { |r| r.id == @child_1.id }.name
assert_equal "Grace OMalley", @pirate.public_send(@association_name).load_target.find { |r| r.id == @child_1.id }.name
end
def test_should_preserve_order_when_not_overwriting_unsaved_updates
@pirate.reload
@pirate.send(association_setter, [{ id: @child_1.id, name: "Grace OMalley" }])
assert_equal @child_1.id, @pirate.send(@association_name).load_target.first.id
assert_equal @child_1.id, @pirate.public_send(@association_name).load_target.first.id
end
def test_should_refresh_saved_records_when_not_overwriting_unsaved_updates
@pirate.reload
record = @pirate.class.reflect_on_association(@association_name).klass.new(name: "Grace OMalley")
@pirate.send(@association_name) << record
@pirate.public_send(@association_name) << record
record.save!
@pirate.send(@association_name).last.update!(name: "Polly")
assert_equal "Polly", @pirate.send(@association_name).load_target.last.name
assert_equal "Polly", @pirate.public_send(@association_name).load_target.last.name
end
def test_should_not_remove_scheduled_destroys_when_loading_association
@pirate.reload
@pirate.send(association_setter, [{ id: @child_1.id, _destroy: "1" }])
assert_predicate @pirate.send(@association_name).load_target.find { |r| r.id == @child_1.id }, :marked_for_destruction?
assert_predicate @pirate.public_send(@association_name).load_target.find { |r| r.id == @child_1.id }, :marked_for_destruction?
end
def test_should_take_a_hash_with_composite_id_keys_and_assign_the_attributes_to_the_associated_models
@ -702,7 +702,7 @@ module NestedAttributesOnACollectionAssociationTests
def test_should_raise_RecordNotFound_if_an_id_belonging_to_a_different_record_is_given
other_pirate = Pirate.create! catchphrase: "Ahoy!"
other_child = other_pirate.send(@association_name).create! name: "Buccaneers Servant"
other_child = other_pirate.public_send(@association_name).create! name: "Buccaneers Servant"
exception = assert_raise ActiveRecord::RecordNotFound do
@pirate.attributes = { association_getter => [{ id: other_child.id }] }
@ -711,16 +711,16 @@ module NestedAttributesOnACollectionAssociationTests
end
def test_should_automatically_build_new_associated_models_for_each_entry_in_a_hash_where_the_id_is_missing
@pirate.send(@association_name).destroy_all
@pirate.public_send(@association_name).destroy_all
@pirate.reload.attributes = {
association_getter => { "foo" => { name: "Grace OMalley" }, "bar" => { name: "Privateers Greed" } }
}
assert_not_predicate @pirate.send(@association_name).first, :persisted?
assert_equal "Grace OMalley", @pirate.send(@association_name).first.name
assert_not_predicate @pirate.public_send(@association_name).first, :persisted?
assert_equal "Grace OMalley", @pirate.public_send(@association_name).first.name
assert_not_predicate @pirate.send(@association_name).last, :persisted?
assert_equal "Privateers Greed", @pirate.send(@association_name).last.name
assert_not_predicate @pirate.public_send(@association_name).last, :persisted?
assert_equal "Privateers Greed", @pirate.public_send(@association_name).last.name
end
def test_should_not_assign_destroy_key_to_a_record
@ -730,7 +730,7 @@ module NestedAttributesOnACollectionAssociationTests
end
def test_should_ignore_new_associated_records_with_truthy_destroy_attribute
@pirate.send(@association_name).destroy_all
@pirate.public_send(@association_name).destroy_all
@pirate.reload.attributes = {
association_getter => {
"foo" => { name: "Grace OMalley" },
@ -738,13 +738,13 @@ module NestedAttributesOnACollectionAssociationTests
}
}
assert_equal 1, @pirate.send(@association_name).length
assert_equal "Grace OMalley", @pirate.send(@association_name).first.name
assert_equal 1, @pirate.public_send(@association_name).length
assert_equal "Grace OMalley", @pirate.public_send(@association_name).first.name
end
def test_should_ignore_new_associated_records_if_a_reject_if_proc_returns_false
@alternate_params[association_getter]["baz"] = {}
assert_no_difference("@pirate.send(@association_name).count") do
assert_no_difference("@pirate.public_send(@association_name).count") do
@pirate.attributes = @alternate_params
end
end
@ -755,7 +755,7 @@ module NestedAttributesOnACollectionAssociationTests
attributes["2"] = { name: "Privateers Greed" } # 2 is lower then 123726353
@pirate.send(association_setter, attributes)
assert_equal ["Posideons Killer", "Killer bandita Dionne", "Privateers Greed", "Grace OMalley"].to_set, @pirate.send(@association_name).map(&:name).to_set
assert_equal ["Posideons Killer", "Killer bandita Dionne", "Privateers Greed", "Grace OMalley"].to_set, @pirate.public_send(@association_name).map(&:name).to_set
end
def test_should_raise_an_argument_error_if_something_else_than_a_hash_is_passed
@ -777,20 +777,20 @@ module NestedAttributesOnACollectionAssociationTests
def test_should_update_existing_records_and_add_new_ones_that_have_no_id
@alternate_params[association_getter]["baz"] = { name: "Buccaneers Servant" }
assert_difference("@pirate.send(@association_name).count", +1) do
assert_difference("@pirate.public_send(@association_name).count", +1) do
@pirate.update @alternate_params
end
assert_equal ["Grace OMalley", "Privateers Greed", "Buccaneers Servant"].to_set, @pirate.reload.send(@association_name).map(&:name).to_set
assert_equal ["Grace OMalley", "Privateers Greed", "Buccaneers Servant"].to_set, @pirate.reload.public_send(@association_name).map(&:name).to_set
end
def test_should_be_possible_to_destroy_a_record
["1", 1, "true", true].each do |true_variable|
record = @pirate.reload.send(@association_name).create!(name: "Grace OMalley")
record = @pirate.reload.public_send(@association_name).create!(name: "Grace OMalley")
@pirate.send(association_setter,
@alternate_params[association_getter].merge("baz" => { :id => record.id, "_destroy" => true_variable })
)
assert_difference("@pirate.send(@association_name).count", -1) do
assert_difference("@pirate.public_send(@association_name).count", -1) do
@pirate.save
end
end
@ -799,17 +799,17 @@ module NestedAttributesOnACollectionAssociationTests
def test_should_not_destroy_the_associated_model_with_a_non_truthy_argument
[nil, "", "0", 0, "false", false].each do |false_variable|
@alternate_params[association_getter]["foo"]["_destroy"] = false_variable
assert_no_difference("@pirate.send(@association_name).count") do
assert_no_difference("@pirate.public_send(@association_name).count") do
@pirate.update(@alternate_params)
end
end
end
def test_should_not_destroy_the_associated_model_until_the_parent_is_saved
assert_no_difference("@pirate.send(@association_name).count") do
assert_no_difference("@pirate.public_send(@association_name).count") do
@pirate.send(association_setter, @alternate_params[association_getter].merge("baz" => { :id => @child_1.id, "_destroy" => true }))
end
assert_difference("@pirate.send(@association_name).count", -1) { @pirate.save }
assert_difference("@pirate.public_send(@association_name).count", -1) { @pirate.save }
end
def test_should_automatically_enable_autosave_on_the_association

View file

@ -137,7 +137,7 @@ class NestedAttributesWithCallbacksTest < ActiveRecord::TestCase
end
def assert_assignment_affects_records_in_target(association_name)
association = @pirate.send(association_name)
association = @pirate.public_send(association_name)
assert association.detect { |b| b == bird_to_update }.name_changed?,
"Update record not updated"
assert association.detect { |b| b == bird_to_destroy }.marked_for_destruction?,