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

Make the logic for nested_records_changed_for_autosave? simpler.

[#4648 state:resolved]
This commit is contained in:
José Valim 2010-06-07 11:00:39 +02:00
parent 7eedc3f397
commit 634c9310e3

View file

@ -1,3 +1,5 @@
require 'active_support/core_ext/array/wrap'
module ActiveRecord
# AutosaveAssociation is a module that takes care of automatically saving
# your associations when the parent is saved. In addition to saving, it
@ -238,16 +240,10 @@ module ActiveRecord
# go through nested autosave associations that are loaded in memory (without loading
# any new ones), and return true if is changed for autosave
def nested_records_changed_for_autosave?
self.class.reflect_on_all_autosave_associations.each do |reflection|
if association = association_instance_get(reflection.name)
if [:belongs_to, :has_one].include?(reflection.macro)
return true if association.target && association.target.changed_for_autosave?
else
return true if association.target.detect { |record| record.changed_for_autosave? }
end
end
self.class.reflect_on_all_autosave_associations.any? do |reflection|
association = association_instance_get(reflection.name)
association && Array.wrap(association.target).any?(&:changed_for_autosave?)
end
false
end
# Validate the association if <tt>:validate</tt> or <tt>:autosave</tt> is