Sha256: f87ad2a9139fb586f8844a5655a145b02e4d96a8fa1ff70e7248f8b51f697b4b
Contents?: true
Size: 1.72 KB
Versions: 1
Compression:
Stored size: 1.72 KB
Contents
module MongoMapper module Plugins module BelongsToWithCounterCache extend ActiveSupport::Concern module ClassMethods # # CounterCache # belongs_to :item, counter_cashe: true # def belongs_to association_id, options={}, &extension options.must_not.include :counter_cashe if options.delete(:counter_cache) || options.delete('counter_cache') association_id = association_id.to_s association_key = "#{association_id}_id" cache_attribute = "#{self.alias.pluralize.underscore}_count" cache_class = if class_name = options[:class_name] class_name.constantize else association_id.classify.constantize end cache_class.keys.must.include cache_attribute increase_method_name = "increase_#{cache_class.alias.underscore}_#{self.alias.pluralize.underscore}_counter" decrease_method_name = "decrease_#{cache_class.alias.underscore}_#{self.alias.pluralize.underscore}_counter" define_method increase_method_name do cache_class.upsert!({id: self.send(association_key)}, :$inc => {cache_attribute => 1}) end protected increase_method_name define_method decrease_method_name do cache_class.upsert!({id: self.send(association_key)}, :$inc => {cache_attribute => -1}) end protected decrease_method_name after_create increase_method_name after_destroy decrease_method_name end super association_id, options, &extension end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mongo_mapper_ext-0.2.3 | lib/mongo_mapper_ext/plugins/belongs_to_with_counter_cache.rb |