提交 e1175d99 编写于 作者: R Rafael Mendonça França

Merge pull request #15596 from eileencodes/add-has_one-method-and-reuse

add has_one? method and reuse instead of checking macro
......@@ -179,7 +179,7 @@ def find_target?
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]
......
......@@ -92,7 +92,7 @@ def foreign_key_present?
# 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
......
......@@ -107,7 +107,7 @@ def build_record(attributes)
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
......
......@@ -187,7 +187,7 @@ def add_autosave_association_callbacks(reflection)
# Doesn't use after_save as that would save associations added in after_create/after_update twice
after_create save_method
after_update save_method
elsif reflection.macro == :has_one
elsif reflection.has_one?
define_method(save_method) { save_has_one_association(reflection) } unless method_defined?(save_method)
# Configures two callbacks instead of a single after_save so that
# the model may rely on their execution order relative to its
......
......@@ -400,6 +400,10 @@ def belongs_to?
macro == :belongs_to
end
def has_one?
macro == :has_one
end
def association_class
case macro
when :belongs_to
......@@ -746,7 +750,7 @@ def check_validity!
raise HasManyThroughAssociationPolymorphicSourceError.new(active_record.name, self, source_reflection)
end
if macro == :has_one && through_reflection.collection?
if has_one? && through_reflection.collection?
raise HasOneThroughCantAssociateThroughCollection.new(active_record.name, self, through_reflection)
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册