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
|
||||
|
||||
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?
|
||||
|
|
|
@ -23,11 +23,26 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
def delete(method = options[:dependent])
|
||||
if load_target
|
||||
case method
|
||||
when :delete
|
||||
target.delete
|
||||
when :destroy
|
||||
target.destroyed_by_association = reflection
|
||||
target.destroy
|
||||
throw(:abort) unless target.destroyed?
|
||||
when :nullify
|
||||
target.update_columns(reflection.foreign_key => nil) if target.persisted?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def replace(record, save = true)
|
||||
raise_on_type_mismatch!(record) if record
|
||||
load_target
|
||||
|
||||
return target unless target || record
|
||||
return target unless load_target || record
|
||||
|
||||
assigning_another_record = target != record
|
||||
if assigning_another_record || record.has_changes_to_save?
|
||||
|
@ -52,23 +67,6 @@ module ActiveRecord
|
|||
self.target = record
|
||||
end
|
||||
|
||||
def delete(method = options[:dependent])
|
||||
if load_target
|
||||
case method
|
||||
when :delete
|
||||
target.delete
|
||||
when :destroy
|
||||
target.destroyed_by_association = reflection
|
||||
target.destroy
|
||||
throw(:abort) unless target.destroyed?
|
||||
when :nullify
|
||||
target.update_columns(reflection.foreign_key => nil) if target.persisted?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# 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
|
||||
# the record is instantiated, and so they are set straight away and do not need to be
|
||||
|
|
|
@ -6,12 +6,12 @@ module ActiveRecord
|
|||
class HasOneThroughAssociation < HasOneAssociation #:nodoc:
|
||||
include ThroughAssociation
|
||||
|
||||
private
|
||||
def replace(record, save = true)
|
||||
create_through_record(record, save)
|
||||
self.target = record
|
||||
end
|
||||
|
||||
private
|
||||
def create_through_record(record, save)
|
||||
ensure_not_nested
|
||||
|
||||
|
|
Loading…
Reference in a new issue