diff options
author | Ian White <ian.w.white@gmail.com> | 2010-05-18 10:28:26 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-05-18 16:13:00 +0200 |
commit | b439d85a19c02eefab1ee308fff334ac1049524d (patch) | |
tree | 75fa2fa34a6d8ac398d25755d0b84de416500d5c /activerecord/lib/active_record/autosave_association.rb | |
parent | ce20b93606195eff1ea2dfeb2aa311130dd9977e (diff) | |
download | rails-b439d85a19c02eefab1ee308fff334ac1049524d.tar.gz rails-b439d85a19c02eefab1ee308fff334ac1049524d.tar.bz2 rails-b439d85a19c02eefab1ee308fff334ac1049524d.zip |
Nested records (re: autosave) are now updated even when the intermediate parent record is unchanged [#4242 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activerecord/lib/active_record/autosave_association.rb')
-rw-r--r-- | activerecord/lib/active_record/autosave_association.rb | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index fd1082a268..c553e95bad 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -214,6 +214,12 @@ module ActiveRecord @marked_for_destruction end + # Returns whether or not this record has been changed in any way (including whether + # any of its nested autosave associations are likewise changed) + def changed_for_autosave? + new_record? || changed? || marked_for_destruction? || nested_records_changed_for_autosave? + end + private # Returns the record for an association collection that should be validated @@ -223,12 +229,27 @@ module ActiveRecord if new_record association elsif autosave - association.target.find_all { |record| record.new_record? || record.changed? || record.marked_for_destruction? } + association.target.find_all { |record| record.changed_for_autosave? } else association.target.find_all { |record| record.new_record? } end end + # 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 + association.target.each {|record| return true if record.changed_for_autosave? } + end + end + end + false + end + # Validate the association if <tt>:validate</tt> or <tt>:autosave</tt> is # turned on for the association specified by +reflection+. def validate_single_association(reflection) |