diff options
author | eileencodes <eileencodes@gmail.com> | 2014-06-05 15:27:07 -0400 |
---|---|---|
committer | eileencodes <eileencodes@gmail.com> | 2014-06-09 19:12:43 -0400 |
commit | 5a0b184c53bf649930b153f3c39c542eca25d74a (patch) | |
tree | de847eed7b12932fa4434ef5e835bed82d8ae80a /activerecord/lib/active_record/associations | |
parent | 3fe33a318f29c03966ca5892413884414421108c (diff) | |
download | rails-5a0b184c53bf649930b153f3c39c542eca25d74a.tar.gz rails-5a0b184c53bf649930b153f3c39c542eca25d74a.tar.bz2 rails-5a0b184c53bf649930b153f3c39c542eca25d74a.zip |
add has_one? method and reuse instead of checking macro
Instead of checking for `macro == :has_one` throughout the
codebase we can create a `has_one?` method to match the `belongs_to?`,
`polymorphic?` and other methods.
Diffstat (limited to 'activerecord/lib/active_record/associations')
3 files changed, 3 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 4a04303fb8..89cbb50035 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -179,7 +179,7 @@ module ActiveRecord def creation_attributes attributes = {} - if (reflection.macro == :has_one || reflection.macro == :has_many) && !options[:through] + if (reflection.has_one? || reflection.macro == :has_many) && !options[:through] attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key] if reflection.options[:as] diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 1edd4fa3aa..81fdd681de 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -92,7 +92,7 @@ module ActiveRecord # has_one associations. def invertible_for?(record) inverse = inverse_reflection_for(record) - inverse && inverse.macro == :has_one + inverse && inverse.has_one? end def target_id diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index f6e08991f7..f155ab1d2a 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -107,7 +107,7 @@ module ActiveRecord if inverse if inverse.macro == :has_many record.send(inverse.name) << build_through_record(record) - elsif inverse.macro == :has_one + elsif inverse.has_one? record.send("#{inverse.name}=", build_through_record(record)) end end |