From 634c9310e302aba0c113363ccec748460113b523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 7 Jun 2010 11:00:39 +0200 Subject: Make the logic for nested_records_changed_for_autosave? simpler. [#4648 state:resolved] --- activerecord/lib/active_record/autosave_association.rb | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 59a7bc1da1..0dcadfab5f 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -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 :validate or :autosave is -- cgit v1.2.3