Clear aggregation cache after setter method calls. #6498

Closed
wants to merge 1 commit into
from
View
16 activerecord/lib/active_record/aggregations.rb
@@ -223,6 +223,22 @@ def composed_of(part_id, options = {})
reader_method(name, class_name, mapping, allow_nil, constructor)
writer_method(name, class_name, mapping, allow_nil, converter)
+ self.class_eval { define_attribute_methods }
@edtsech
edtsech added a line comment May 26, 2012

I'm not sure, can we force to define attribute methods here? @josevalim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ instance_methods = self.instance_methods
+ unless mapping.length == 1
+ mapping.map(&:first).each do |m|
+ if instance_methods.include? m.to_sym
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def #{m}_with_clear_cache= (value)
+ self.#{m}_without_clear_cache = value
+ clear_aggregation_cache
+ end
+ alias_method_chain :#{m}=, :clear_cache
+ RUBY
+ end
+ end
+ end
+
create_reflection(:composed_of, part_id, options, self)
end
View
6 activerecord/test/cases/aggregations_test.rb
@@ -137,6 +137,12 @@ def test_custom_converter
assert_equal 'Barnoit GUMBLEAU', customers(:barney).fullname.to_s
assert_kind_of Fullname, customers(:barney).fullname
end
+
+ def test_caching
+ customers(:barney).address
+ customers(:barney).address_street = "Noisy Road"
+ assert_equal "Noisy Road", customers(:barney).address.street
+ end
end
class OverridingAggregationsTest < ActiveRecord::TestCase