Skip to content

Commit

Permalink
Updated READ.me with conditional counter example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sameer-Tilak committed Mar 6, 2014
1 parent 868cba8 commit 803bec0
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 803bec0

Please sign in to comment.