Permalink
Browse files

r3387@asus: jeremy | 2005-12-07 20:48:42 -0800

 Apply [3242] to stable.  Reloading an instance refreshes its aggregations as well as its associations.  Closes #3024.


git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/stable@3243 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent c6120ac commit bd9b27df8b372c6cc3eb2978e4ef95f68226cdfb @jeremy jeremy committed Dec 8, 2005
View
@@ -1,5 +1,7 @@
* SVN*
+* Reloading an instance refreshes its aggregations as well as its associations. #3024 [François Beausolei]
+
* Fixed that using :include together with :conditions array in Base.find would cause NoMethodError #2887 [Paul Hammmond]
* PostgreSQL: more robust sequence name discovery. #3087 [Rick Olson]
@@ -5,6 +5,12 @@ def self.append_features(base)
base.extend(ClassMethods)
end
+ def clear_aggregation_cache #:nodoc:
+ self.class.reflect_on_all_aggregations.to_a.each do |assoc|
+ instance_variable_set "@#{assoc.name}", nil
+ end unless self.new_record?
+ end
+
# Active Record implements aggregation through a macro-like class method called +composed_of+ for representing attributes
# as value objects. It expresses relationships like "Account [is] composed of Money [among other things]" or "Person [is]
# composed of [an] address". Each call to the macro adds a description of how the value objects are created from the
@@ -1304,6 +1304,7 @@ def toggle!(attribute)
# Reloads the attributes of this object from the database.
def reload
+ clear_aggregation_cache
clear_association_cache
@attributes.update(self.class.find(self.id).instance_variable_get('@attributes'))
self
@@ -44,4 +44,23 @@ def test_inferred_mapping
assert_equal "39", customers(:david).gps_location.latitude
assert_equal "-110", customers(:david).gps_location.longitude
end
+
+ def test_reloaded_instance_refreshes_aggregations
+ assert_equal "35.544623640962634", customers(:david).gps_location.latitude
+ assert_equal "-105.9309951055148", customers(:david).gps_location.longitude
+
+ Customer.update_all("gps_location = '24x113'")
+ customers(:david).reload
+ assert_equal '24x113', customers(:david)['gps_location']
+
+ assert_equal GpsLocation.new('24x113'), customers(:david).gps_location
+ end
+
+ def test_gps_equality
+ assert GpsLocation.new('39x110') == GpsLocation.new('39x110')
+ end
+
+ def test_gps_inequality
+ assert GpsLocation.new('39x110') != GpsLocation.new('39x111')
+ end
end
@@ -44,4 +44,8 @@ def latitude
def longitude
gps_location.split("x").last
end
+
+ def ==(other)
+ self.latitude == other.latitude && self.longitude == other.longitude
+ end
end

0 comments on commit bd9b27d

Please sign in to comment.