提交 739f39b7 编写于 作者: P Piotr Sarnacki

Extract set_through_association_source_target to a module

上级 b85c142f
# frozen_string_literal: true
require "active_record/associations/set_through_association_source_target"
module ActiveRecord
module Associations
# = Active Record Belongs To Association
class BelongsToAssociation < SingularAssociation #:nodoc:
include SetThroughAssociationSourceTarget
def handle_dependency
return unless load_target
......@@ -72,14 +76,7 @@ def replace(record)
self.target = record
through_reflection = self.reflection.active_record.reflections.values.find { |r| r.through_reflection && r.through_reflection.name == self.reflection.name }
if through_reflection && !through_reflection.collection?
through_target = record.send(through_reflection.name)
if through_target
source_target_name = through_reflection.source_reflection.name
owner.send("#{source_target_name}=", through_target)
end
end
set_through_association_source_target(record)
end
def update_counters(by)
......
# frozen_string_literal: true
require "active_record/associations/set_through_association_source_target"
module ActiveRecord
module Associations
# = Active Record Has One Association
class HasOneAssociation < SingularAssociation #:nodoc:
include ForeignAssociation
include SetThroughAssociationSourceTarget
def handle_dependency
case options[:dependent]
......@@ -66,14 +70,7 @@ def replace(record, save = true)
self.target = record
through_reflection = self.reflection.active_record.reflections.values.find { |r| r.through_reflection && r.through_reflection.name == self.reflection.name }
if through_reflection
through_target = record.send(through_reflection.name)
if through_target
source_target_name = through_reflection.source_reflection.name
owner.send("#{source_target_name}=", through_target)
end
end
set_through_association_source_target(record)
end
# The reason that the save param for replace is false, if for create (not just build),
......
# frozen_string_literal: true
module ActiveRecord
module Associations
module SetThroughAssociationSourceTarget
def set_through_association_source_target(record)
through_reflection = self.reflection.active_record.reflections.values.find { |r|
r.through_reflection && r.through_reflection.name == self.reflection.name
}
if through_reflection && !through_reflection.collection?
through_target = record.send(through_reflection.source_reflection.name)
if through_target
target_name = through_reflection.name
owner.send("#{target_name}=", through_target)
end
end
end
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册