|
| 1 | +module DataMapper |
| 2 | + # CounterCacheable allows you to transparently maintain counts on collection association of this model on the parent model. |
| 3 | + # You can also specify a custom counter cache column by providing a column name instead of a true/false value |
| 4 | + # to this option (e.g., :counter_cache => :my_custom_counter.) |
| 5 | + module CounterCacheable |
| 6 | + |
| 7 | + def self.included(klass) |
| 8 | + DataMapper::Associations::ManyToOne.module_eval { |
| 9 | + extend DataMapper::CounterCacheable::ClassMethods |
| 10 | + (class << self; self; end).class_eval do |
| 11 | + alias_method :setup_without_counter_caching, :setup |
| 12 | + alias_method :setup, :setup_with_counter_caching |
| 13 | + end |
| 14 | + } |
| 15 | + end |
| 16 | + |
| 17 | + module ClassMethods |
| 18 | + |
| 19 | + def setup_with_counter_caching(name, model, options = {}) |
| 20 | + counter_cache_attribute = options.delete(:counter_cache) |
| 21 | + relationship = setup_without_counter_caching(name, model, options) |
| 22 | + |
| 23 | + if counter_cache_attribute |
| 24 | + case counter_cache_attribute |
| 25 | + when String, Symbol |
| 26 | + counter_cache_attribute = counter_cache_attribute.intern |
| 27 | + else |
| 28 | + counter_cache_attribute = "#{model.storage_name}_count".intern |
| 29 | + end |
| 30 | + |
| 31 | + relationship.parent_model.class_eval <<-EOS, __FILE__, __LINE__ |
| 32 | + property :#{counter_cache_attribute}, Integer, :default => 0, :lazy => false |
| 33 | + EOS |
| 34 | + |
| 35 | + parent_name = relationship.parent_model.name.downcase |
| 36 | + |
| 37 | + model.class_eval <<-EOS, __FILE__, __LINE__ |
| 38 | + after :create, :increment_counter_cache_for_#{parent_name} |
| 39 | + after :destroy, :decrement_counter_cache_for_#{parent_name} |
| 40 | + |
| 41 | + def increment_counter_cache_for_#{parent_name} |
| 42 | + self.#{parent_name}.update_attributes(:#{counter_cache_attribute} => self.#{parent_name}.#{counter_cache_attribute}.succ) |
| 43 | + end |
| 44 | +
|
| 45 | + def decrement_counter_cache_for_#{parent_name} |
| 46 | + self.#{parent_name}.update_attributes(:#{counter_cache_attribute} => self.#{parent_name}.#{counter_cache_attribute} - 1) |
| 47 | + end |
| 48 | + |
| 49 | + EOS |
| 50 | + end |
| 51 | + |
| 52 | + relationship |
| 53 | + end |
| 54 | + |
| 55 | + end # ClassMethods |
| 56 | + |
| 57 | + end # CounterCacheable |
| 58 | +end # DataMapper |
| 59 | + |
| 60 | +if $0 == __FILE__ |
| 61 | + require 'rubygems' |
| 62 | + |
| 63 | + gem 'dm-core', '~>0.9.8' |
| 64 | + require 'dm-core' |
| 65 | + |
| 66 | + FileUtils.touch(File.join(Dir.pwd, "migration_test.db")) |
| 67 | + DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/migration_test.db") |
| 68 | + |
| 69 | + class Post |
| 70 | + include DataMapper::Resource |
| 71 | + |
| 72 | + property :id, Integer, :serial => true |
| 73 | + has n, :comments |
| 74 | + end |
| 75 | + Post.auto_migrate! |
| 76 | + |
| 77 | + class Comment |
| 78 | + include DataMapper::Resource |
| 79 | + include DataMapper::CounterCacheable |
| 80 | + |
| 81 | + belongs_to :post, :counter_cache => true |
| 82 | + end |
| 83 | + Comments.auto_migrate! |
| 84 | + |
| 85 | + Post.create.comments.create |
| 86 | +end |
0 commit comments