mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add support for belongs_to to has_many inversing.
This commit is contained in:
parent
4ea7769044
commit
d45c9adf27
4 changed files with 20 additions and 10 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
* Add support for `belongs_to` to `has_many` inversing.
|
||||||
|
|
||||||
|
*Gannon McGibbon*
|
||||||
|
|
||||||
* Allow length configuration for `has_secure_token` method. The minimum length
|
* Allow length configuration for `has_secure_token` method. The minimum length
|
||||||
is set at 24 characters.
|
is set at 24 characters.
|
||||||
|
|
||||||
|
|
|
@ -108,11 +108,8 @@ module ActiveRecord
|
||||||
owner._read_attribute(reflection.foreign_key)
|
owner._read_attribute(reflection.foreign_key)
|
||||||
end
|
end
|
||||||
|
|
||||||
# NOTE - for now, we're only supporting inverse setting from belongs_to back onto
|
|
||||||
# has_one associations.
|
|
||||||
def invertible_for?(record)
|
def invertible_for?(record)
|
||||||
inverse = inverse_reflection_for(record)
|
inverse_reflection_for(record)
|
||||||
inverse && inverse.has_one?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def stale_state
|
def stale_state
|
||||||
|
|
|
@ -285,6 +285,15 @@ module ActiveRecord
|
||||||
replace_on_target(record, index, skip_callbacks, &block)
|
replace_on_target(record, index, skip_callbacks, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def target=(record)
|
||||||
|
case record
|
||||||
|
when Array
|
||||||
|
super
|
||||||
|
else
|
||||||
|
add_to_target(record)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def scope
|
def scope
|
||||||
scope = super
|
scope = super
|
||||||
scope.none! if null_scope?
|
scope.none! if null_scope?
|
||||||
|
|
|
@ -607,7 +607,7 @@ class InverseBelongsToTests < ActiveRecord::TestCase
|
||||||
assert_equal f.description, m.face.description, "Description of face should be the same after changes to newly-created-parent-owned instance"
|
assert_equal f.description, m.face.description, "Description of face should be the same after changes to newly-created-parent-owned instance"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
|
def test_should_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
|
||||||
i = interests(:trainspotting)
|
i = interests(:trainspotting)
|
||||||
m = i.man
|
m = i.man
|
||||||
assert_not_nil m.interests
|
assert_not_nil m.interests
|
||||||
|
@ -615,9 +615,9 @@ class InverseBelongsToTests < ActiveRecord::TestCase
|
||||||
assert_not_nil iz
|
assert_not_nil iz
|
||||||
assert_equal i.topic, iz.topic, "Interest topics should be the same before changes to child"
|
assert_equal i.topic, iz.topic, "Interest topics should be the same before changes to child"
|
||||||
i.topic = "Eating cheese with a spoon"
|
i.topic = "Eating cheese with a spoon"
|
||||||
assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to child"
|
assert_equal i.topic, iz.topic, "Interest topics should be the same after changes to child"
|
||||||
iz.topic = "Cow tipping"
|
iz.topic = "Cow tipping"
|
||||||
assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance"
|
assert_equal i.topic, iz.topic, "Interest topics should be the same after changes to parent-owned instance"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_child_instance_should_be_shared_with_replaced_via_accessor_parent
|
def test_child_instance_should_be_shared_with_replaced_via_accessor_parent
|
||||||
|
@ -704,7 +704,7 @@ class InversePolymorphicBelongsToTests < ActiveRecord::TestCase
|
||||||
assert_equal old_inversed_man.object_id, new_inversed_man.object_id
|
assert_equal old_inversed_man.object_id, new_inversed_man.object_id
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
|
def test_should_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
|
||||||
i = interests(:llama_wrangling)
|
i = interests(:llama_wrangling)
|
||||||
m = i.polymorphic_man
|
m = i.polymorphic_man
|
||||||
assert_not_nil m.polymorphic_interests
|
assert_not_nil m.polymorphic_interests
|
||||||
|
@ -712,9 +712,9 @@ class InversePolymorphicBelongsToTests < ActiveRecord::TestCase
|
||||||
assert_not_nil iz
|
assert_not_nil iz
|
||||||
assert_equal i.topic, iz.topic, "Interest topics should be the same before changes to child"
|
assert_equal i.topic, iz.topic, "Interest topics should be the same before changes to child"
|
||||||
i.topic = "Eating cheese with a spoon"
|
i.topic = "Eating cheese with a spoon"
|
||||||
assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to child"
|
assert_equal i.topic, iz.topic, "Interest topics should be the same after changes to child"
|
||||||
iz.topic = "Cow tipping"
|
iz.topic = "Cow tipping"
|
||||||
assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance"
|
assert_equal i.topic, iz.topic, "Interest topics should be the same after changes to parent-owned instance"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_trying_to_access_inverses_that_dont_exist_shouldnt_raise_an_error
|
def test_trying_to_access_inverses_that_dont_exist_shouldnt_raise_an_error
|
||||||
|
|
Loading…
Reference in a new issue