Created
June 19, 2011 07:52
-
-
Save ZhangHanDong/1033906 to your computer and use it in GitHub Desktop.
MongoId Counter Cahce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# lib/mongoid/counter_cache.rb | |
# ruby | |
# | |
# Created by Zhang Alex on 2011-06-17. | |
# Copyright 2011 __ZhangHanDong__. All rights reserved. | |
# | |
# =================================== | |
# class Forum | |
# references_many :topics | |
# references_many :posts | |
# end | |
# | |
# | |
# class Topic | |
# referenced_in :forum | |
# include Mongoid::CounterCache | |
# counter_cache name: :forum, inverse_of: :posts | |
# end | |
# =================================== | |
module Mongoid | |
module CounterCache | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def counter_cache(metadata) | |
counter_name = "#{metadata[:inverse_of]}_count" | |
set_callback(:create, :after) do |document| | |
relation = document.send(metadata[:name]) | |
if relation | |
relation.inc(counter_name.to_sym, 1) if relation.class.fields.keys.include?(counter_name) | |
end | |
end | |
set_callback(:destroy, :after) do |document| | |
relation = document.send(metadata[:name]) | |
if relation && relation.class.fields.keys.include?(counter_name) | |
relation.inc(counter_name.to_sym, -1) | |
end | |
end | |
end | |
end #ClassMethods | |
end #CounterCache | |
end #Mongoid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment