Skip to content
This repository

Clear aggregation cache after setter method calls. #6498

Closed
wants to merge 1 commit into from

2 participants

Ed Tsech Rafael Mendonça França
Ed Tsech
edtsech commented

Solve this issue:

customer.address.street # => "Quiet Road"
customer.address_street = "Noisy Road"
customer.address.street # => "Quiet Road"

Where:

class Customer
  composed_of :address, :mapping => [ %w(address_street street), %w(address_city city) ]
end


class Address
  attr_reader :street, :city, :country

  def initialize(street, city, country)
    @street, @city, @country = street, city, country
  end
end
Ed Tsech edtsech Clear aggregation cache after setter method calls.
Solve this issue:
customer.address.street # => "Quiet Road"
customer.address_street = "Noisy Road"
customer.address.street # => "Quiet Road"
567c3a8
Ed Tsech edtsech commented on the diff
activerecord/lib/active_record/aggregations.rb
@@ -223,6 +223,22 @@ def composed_of(part_id, options = {})
223 223 reader_method(name, class_name, mapping, allow_nil, constructor)
224 224 writer_method(name, class_name, mapping, allow_nil, converter)
225 225
  226 + self.class_eval { define_attribute_methods }
1
Ed Tsech
edtsech added a note

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
Rafael Mendonça França
Owner

Closed by #6743

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

May 26, 2012
Ed Tsech edtsech Clear aggregation cache after setter method calls.
Solve this issue:
customer.address.street # => "Quiet Road"
customer.address_street = "Noisy Road"
customer.address.street # => "Quiet Road"
567c3a8
This page is out of date. Refresh to see the latest.
16 activerecord/lib/active_record/aggregations.rb
@@ -223,6 +223,22 @@ def composed_of(part_id, options = {})
223 223 reader_method(name, class_name, mapping, allow_nil, constructor)
224 224 writer_method(name, class_name, mapping, allow_nil, converter)
225 225
  226 + self.class_eval { define_attribute_methods }
  227 + instance_methods = self.instance_methods
  228 + unless mapping.length == 1
  229 + mapping.map(&:first).each do |m|
  230 + if instance_methods.include? m.to_sym
  231 + self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
  232 + def #{m}_with_clear_cache= (value)
  233 + self.#{m}_without_clear_cache = value
  234 + clear_aggregation_cache
  235 + end
  236 + alias_method_chain :#{m}=, :clear_cache
  237 + RUBY
  238 + end
  239 + end
  240 + end
  241 +
226 242 create_reflection(:composed_of, part_id, options, self)
227 243 end
228 244
6 activerecord/test/cases/aggregations_test.rb
@@ -137,6 +137,12 @@ def test_custom_converter
137 137 assert_equal 'Barnoit GUMBLEAU', customers(:barney).fullname.to_s
138 138 assert_kind_of Fullname, customers(:barney).fullname
139 139 end
  140 +
  141 + def test_caching
  142 + customers(:barney).address
  143 + customers(:barney).address_street = "Noisy Road"
  144 + assert_equal "Noisy Road", customers(:barney).address.street
  145 + end
140 146 end
141 147
142 148 class OverridingAggregationsTest < ActiveRecord::TestCase

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.