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:
parent
268dd6966d
commit
bfe4248c78
3 changed files with 44 additions and 47 deletions
|
@ -16,19 +16,6 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
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)
|
def target=(record)
|
||||||
replace_keys(record)
|
replace_keys(record)
|
||||||
super
|
super
|
||||||
|
@ -56,6 +43,18 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
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)
|
def update_counters(by)
|
||||||
if require_counter_update? && foreign_key_present?
|
if require_counter_update? && foreign_key_present?
|
||||||
|
|
|
@ -23,35 +23,6 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
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])
|
def delete(method = options[:dependent])
|
||||||
if load_target
|
if load_target
|
||||||
case method
|
case method
|
||||||
|
@ -68,6 +39,33 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
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),
|
# 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
|
# is because the setting of the foreign keys is actually handled by the scoping when
|
||||||
|
|
|
@ -6,12 +6,12 @@ module ActiveRecord
|
||||||
class HasOneThroughAssociation < HasOneAssociation #:nodoc:
|
class HasOneThroughAssociation < HasOneAssociation #:nodoc:
|
||||||
include ThroughAssociation
|
include ThroughAssociation
|
||||||
|
|
||||||
def replace(record, save = true)
|
|
||||||
create_through_record(record, save)
|
|
||||||
self.target = record
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
def replace(record, save = true)
|
||||||
|
create_through_record(record, save)
|
||||||
|
self.target = record
|
||||||
|
end
|
||||||
|
|
||||||
def create_through_record(record, save)
|
def create_through_record(record, save)
|
||||||
ensure_not_nested
|
ensure_not_nested
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue