From 0e3f96a08c16393349412896bab42d74e307ccc7 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 16 Mar 2010 15:08:38 -0700 Subject: [PATCH] fisting uninitialized ivar warnings. [#4198 state:resolved] --- activemodel/lib/active_model/dirty.rb | 17 +++++++---------- activerecord/lib/active_record/aggregations.rb | 5 +++++ .../associations/association_proxy.rb | 1 + .../lib/active_record/attribute_methods/dirty.rb | 16 ++++++++-------- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index cb70cf7..d3a6bad 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -91,7 +91,7 @@ module ActiveModel # person.name = 'bob' # person.changed? # => true def changed? - !changed_attributes.empty? + !@changed_attributes.empty? end # List of attributes with unsaved changes. @@ -99,7 +99,7 @@ module ActiveModel # person.name = 'bob' # person.changed # => ['name'] def changed - changed_attributes.keys + @changed_attributes.keys end # Map of changed attrs => [original value, new value]. @@ -120,22 +120,19 @@ module ActiveModel end private - # Map of change attr => original value. - attr_reader :changed_attributes - # Handle *_changed? for +method_missing+. def attribute_changed?(attr) - changed_attributes.include?(attr) + @changed_attributes.include?(attr) end # Handle *_change for +method_missing+. def attribute_change(attr) - [changed_attributes[attr], __send__(attr)] if attribute_changed?(attr) + [@changed_attributes[attr], __send__(attr)] if attribute_changed?(attr) end # Handle *_was for +method_missing+. def attribute_was(attr) - attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr) + attribute_changed?(attr) ? @changed_attributes[attr] : __send__(attr) end # Handle *_will_change! for +method_missing+. @@ -146,12 +143,12 @@ module ActiveModel rescue TypeError, NoMethodError end - changed_attributes[attr] = value + @changed_attributes[attr] = value end # Handle reset_*! for +method_missing+. def reset_attribute!(attr) - __send__("#{attr}=", changed_attributes[attr]) if attribute_changed?(attr) + __send__("#{attr}=", @changed_attributes[attr]) if attribute_changed?(attr) end end end diff --git a/activerecord/lib/active_record/aggregations.rb b/activerecord/lib/active_record/aggregations.rb index 9ecf231..0838990 100644 --- a/activerecord/lib/active_record/aggregations.rb +++ b/activerecord/lib/active_record/aggregations.rb @@ -213,6 +213,11 @@ module ActiveRecord module_eval do define_method(name) do |*args| force_reload = args.first || false + + unless instance_variable_defined?("@#{name}") + instance_variable_set("@#{name}", nil) + end + if (instance_variable_get("@#{name}").nil? || force_reload) && (!allow_nil || mapping.any? {|pair| !read_attribute(pair.first).nil? }) attrs = mapping.collect {|pair| read_attribute(pair.first)} object = case constructor diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 0ff89df..4fb1df3 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -53,6 +53,7 @@ module ActiveRecord def initialize(owner, reflection) @owner, @reflection = owner, reflection + @updated = false reflection.check_validity! Array(reflection.options[:extend]).each { |ext| proxy_extend(ext) } reset diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index 64e761e..a8698a2 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -18,7 +18,7 @@ module ActiveRecord def save_with_dirty(*args) #:nodoc: if status = save_without_dirty(*args) @previously_changed = changes - changed_attributes.clear + @changed_attributes.clear end status end @@ -26,8 +26,8 @@ module ActiveRecord # Attempts to save! the record and clears changed attributes if successful. def save_with_dirty!(*args) #:nodoc: save_without_dirty!(*args).tap do - @previously_changed = changes - changed_attributes.clear + @previously_changed = changes + @changed_attributes.clear end end @@ -35,7 +35,7 @@ module ActiveRecord def reload_with_dirty(*args) #:nodoc: reload_without_dirty(*args).tap do @previously_changed.clear - changed_attributes.clear + @changed_attributes.clear end end @@ -45,12 +45,12 @@ module ActiveRecord attr = attr.to_s # The attribute already has an unsaved change. - if changed_attributes.include?(attr) - old = changed_attributes[attr] - changed_attributes.delete(attr) unless field_changed?(attr, old, value) + if attribute_changed?(attr) + old = @changed_attributes[attr] + @changed_attributes.delete(attr) unless field_changed?(attr, old, value) else old = clone_attribute_value(:read_attribute, attr) - changed_attributes[attr] = old if field_changed?(attr, old, value) + @changed_attributes[attr] = old if field_changed?(attr, old, value) end # Carry on. -- 1.6.6