From 803bec0fdf32d3b6287a1152f21c32ed8a660d8a Mon Sep 17 00:00:00 2001 From: Sameer Tilak Date: Thu, 6 Mar 2014 16:35:02 +0530 Subject: [PATCH] Updated READ.me with conditional counter example. --- README.md | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 976e9b6..f404c03 100644 --- a/README.md +++ b/README.md @@ -61,13 +61,47 @@ If you do not wish to use the `model_count` naming convention, you can override counter_cache :library, :field => "total_amount_of_books" ```` + +### Conditional Counter + +If you want to maintain counter based on certain condition, then you can specify it using `:if` + +````rb +class Post + include Mongoid::Document + + field :article + field :comment_count + + has_many :comments + +end +```` +Then in the referrenced/Embedded document, add condition for counter using `:if` + +````rb +class Comment + include Mongoid::Document + include Mongoid::MagicCounterCache + + belongs_to :post + + field :remark + field :is_published, type: Boolean, default: false + + counter_cache :post, :if => Proc.new { |act| (act.is_published) } +end +```` + +comment_count will get incremented / decremented only when `:if` condition returns `true` + ## TODO - 1. Add additional options parameters - 2. Simplify syntax (I.E. including MagicCounterCache will add counts for all `belongs_to` associations on a document +1. Add additional options parameters +2. Simplify syntax (I.E. including MagicCounterCache will add counts for all `belongs_to` associations on a document ## CONTRIBUTE -If you'd like to contribute, feel free to fork and merge until your heart is content + If you'd like to contribute, feel free to fork and merge until your heart is content