Skip to content

Commit

Permalink
fisting uninitialized ivar warnings. [#4198 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: wycats <wycats@gmail.com>
  • Loading branch information
tenderlove authored and wycats committed Mar 16, 2010
1 parent 12bf636 commit df735cf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
17 changes: 7 additions & 10 deletions activemodel/lib/active_model/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ module Dirty
# person.name = 'bob'
# person.changed? # => true
def changed?
!changed_attributes.empty?
!@changed_attributes.empty?
end

# List of attributes with unsaved changes.
# person.changed # => []
# person.name = 'bob'
# person.changed # => ['name']
def changed
changed_attributes.keys
@changed_attributes.keys
end

# Map of changed attrs => [original value, new value].
Expand All @@ -120,22 +120,19 @@ def previous_changes
end

private
# Map of change <tt>attr => original value</tt>.
attr_reader :changed_attributes

# Handle <tt>*_changed?</tt> for +method_missing+.
def attribute_changed?(attr)
changed_attributes.include?(attr)
@changed_attributes.include?(attr)
end

# Handle <tt>*_change</tt> 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 <tt>*_was</tt> 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 <tt>*_will_change!</tt> for +method_missing+.
Expand All @@ -146,12 +143,12 @@ def attribute_will_change!(attr)
rescue TypeError, NoMethodError
end

changed_attributes[attr] = value
@changed_attributes[attr] = value
end

# Handle <tt>reset_*!</tt> 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
5 changes: 5 additions & 0 deletions activerecord/lib/active_record/aggregations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ def reader_method(name, class_name, mapping, allow_nil, constructor)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class AssociationProxy #:nodoc:

def initialize(owner, reflection)
@owner, @reflection = owner, reflection
@updated = false
reflection.check_validity!
Array(reflection.options[:extend]).each { |ext| proxy_extend(ext) }
reset
Expand Down
16 changes: 8 additions & 8 deletions activerecord/lib/active_record/attribute_methods/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ module Dirty
def save_with_dirty(*args) #:nodoc:
if status = save_without_dirty(*args)
@previously_changed = changes
changed_attributes.clear
@changed_attributes.clear
end
status
end

# Attempts to <tt>save!</tt> 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

# <tt>reload</tt> the record and clears changed attributes.
def reload_with_dirty(*args) #:nodoc:
reload_without_dirty(*args).tap do
@previously_changed.clear
changed_attributes.clear
@changed_attributes.clear
end
end

Expand All @@ -45,12 +45,12 @@ def write_attribute(attr, value)
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.
Expand Down

0 comments on commit df735cf

Please sign in to comment.