23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/dm-counter-cache.rb', line 23
def setup_with_counter_caching(name, model, options = {})
counter_cache_attribute = options.delete(:counter_cache)
relationship = setup_without_counter_caching(name, model, options)
if counter_cache_attribute
case counter_cache_attribute
when String, Symbol
counter_cache_attribute = counter_cache_attribute.to_s
else
counter_cache_attribute = "#{model.storage_name}_count".to_s
end
model.class_eval " unless method_defined?(:increment_counter_cache_for_\#{name})\n after :create, :increment_counter_cache_for_\#{name}\n after :destroy, :decrement_counter_cache_for_\#{name}\n \n def increment_counter_cache_for_\#{name}\n return unless ::\#{relationship.parent_model}.properties.has_property?(:\#{counter_cache_attribute})\n if self.\#{name} && self.class == \#{model.name}\n self.\#{name}.update_attributes(:\#{counter_cache_attribute} => self.\#{name}.reload.\#{counter_cache_attribute}.succ)\n end\n end\n\n def decrement_counter_cache_for_\#{name}\n return unless ::\#{relationship.parent_model}.properties.has_property?(:\#{counter_cache_attribute})\n if self.\#{name} && self.class == \#{model.name}\n self.\#{name}.update_attributes(:\#{counter_cache_attribute} => self.\#{name}.reload.\#{counter_cache_attribute} - 1)\n end\n end\n end\n \n EOS\n end\n\n relationship\nend\n", __FILE__, __LINE__
|