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

Don't expose SingularAssociation#replace which is internal API

Originally `SingularAssociation#replace` abstract method is private, and
doesn't intend to be called directly.
This commit is contained in:
Ryuta Kamizono 2018-05-16 06:43:52 +09:00
parent 268dd6966d
commit bfe4248c78
3 changed files with 44 additions and 47 deletions

View file

@ -16,19 +16,6 @@ module ActiveRecord
end
end
def replace(record)
if record
raise_on_type_mismatch!(record)
update_counters_on_replace(record)
set_inverse_instance(record)
@updated = true
else
decrement_counters
end
self.target = record
end
def target=(record)
replace_keys(record)
super
@ -56,6 +43,18 @@ module ActiveRecord
end
private
def replace(record)
if record
raise_on_type_mismatch!(record)
update_counters_on_replace(record)
set_inverse_instance(record)
@updated = true
else
decrement_counters
end
self.target = record
end
def update_counters(by)
if require_counter_update? && foreign_key_present?

View file

@ -23,35 +23,6 @@ module ActiveRecord
end
end
def replace(record, save = true)
raise_on_type_mismatch!(record) if record
load_target
return target unless target || record
assigning_another_record = target != record
if assigning_another_record || record.has_changes_to_save?
save &&= owner.persisted?
transaction_if(save) do
remove_target!(options[:dependent]) if target && !target.destroyed? && assigning_another_record
if record
set_owner_attributes(record)
set_inverse_instance(record)
if save && !record.save
nullify_owner_attributes(record)
set_owner_attributes(target) if target
raise RecordNotSaved, "Failed to save the new associated #{reflection.name}."
end
end
end
end
self.target = record
end
def delete(method = options[:dependent])
if load_target
case method
@ -68,6 +39,33 @@ module ActiveRecord
end
private
def replace(record, save = true)
raise_on_type_mismatch!(record) if record
return target unless load_target || record
assigning_another_record = target != record
if assigning_another_record || record.has_changes_to_save?
save &&= owner.persisted?
transaction_if(save) do
remove_target!(options[:dependent]) if target && !target.destroyed? && assigning_another_record
if record
set_owner_attributes(record)
set_inverse_instance(record)
if save && !record.save
nullify_owner_attributes(record)
set_owner_attributes(target) if target
raise RecordNotSaved, "Failed to save the new associated #{reflection.name}."
end
end
end
end
self.target = record
end
# The reason that the save param for replace is false, if for create (not just build),
# is because the setting of the foreign keys is actually handled by the scoping when

View file

@ -6,12 +6,12 @@ module ActiveRecord
class HasOneThroughAssociation < HasOneAssociation #:nodoc:
include ThroughAssociation
def replace(record, save = true)
create_through_record(record, save)
self.target = record
end
private
def replace(record, save = true)
create_through_record(record, save)
self.target = record
end
def create_through_record(record, save)
ensure_not_nested