Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jah2488/mongoid-magic-counter-cache
A Simple Counter Cache Gem for the Mongoid ORM
https://github.com/jah2488/mongoid-magic-counter-cache
counter-cache mongodb mongoid-plugin rails
Last synced: about 3 hours ago
JSON representation
A Simple Counter Cache Gem for the Mongoid ORM
- Host: GitHub
- URL: https://github.com/jah2488/mongoid-magic-counter-cache
- Owner: jah2488
- License: mit
- Created: 2012-02-25T06:22:06.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2015-03-02T03:26:09.000Z (over 9 years ago)
- Last Synced: 2024-04-26T06:00:49.288Z (7 months ago)
- Topics: counter-cache, mongodb, mongoid-plugin, rails
- Language: Ruby
- Homepage:
- Size: 201 KB
- Stars: 39
- Watchers: 3
- Forks: 24
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Mongoid Magic Counter Cache [![Build Status](https://secure.travis-ci.org/jah2488/mongoid-magic-counter-cache.png?branch=master)](http://travis-ci.org/jah2488/mongoid-magic-counter-cache) [![Code Climate](https://codeclimate.com/github/jah2488/mongoid-magic-counter-cache.png)](https://codeclimate.com/github/jah2488/mongoid-magic-counter-cache)
=======## DESCRIPTION
Mongoid Counter Cache is a simple mongoid extension to add basic counter cache functionality to Embedded and Referenced Mongoid Documents.
### RDOC
[http://rdoc.info/github/jah2488/mongoid-magic-counter-cache/master/frames](http://rdoc.info/github/jah2488/mongoid-magic-counter-cache/master/frames)## INSTALLATION
#### Mongoid Magic Counter Cache requires ruby `1.9.3` at a minimum
### RubyGems
````sh
$ [sudo] gem install mongoid_magic_counter_cache
````
### GemFile
````rb
gem 'mongoid_magic_counter_cache'
````
## USAGEFirst add a field to the document where you will be accessing the counter cache from.
````rb
class Library
include Mongoid::Documentfield :name
field :city
field :book_count
has_many :booksend
````
Then in the referrenced/Embedded document. Include `Mongoid::MagicCounterCache`````rb
class Book
include Mongoid::Document
include Mongoid::MagicCounterCachefield :first
field :lastbelongs_to :library
counter_cache :library
end
````````rb
$ @library.book_count
#=> 990
````
### Alternative SyntaxIf you do not wish to use the `model_count` naming convention, you can override the defaults by specifying the `:field` parameter.
````rb
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::Documentfield :article
field :comment_counthas_many :comments
end
````
Then in the referrenced/Embedded document, add condition for counter using `:if`````rb
class Comment
include Mongoid::Document
include Mongoid::MagicCounterCachebelongs_to :post
field :remark
field :is_published, type: Boolean, default: falsecounter_cache :post, :if => Proc.new { |act| (act.is_published) }
end
````comment_count will get incremented / decremented only when `:if` condition returns `true`
### Conditional Counter After Update
In conjunction with the conditional counter, if you want to maintain counter after an update to an object, then you can specify it using `:if_update`
Using same example as above, in the referrenced/Embedded document, add an additional condition for counter using `:if_update`
````rb
class Comment
include Mongoid::Document
include Mongoid::MagicCounterCachebelongs_to :post
field :remark
field :is_published, type: Boolean, default: falsecounter_cache :post, :if => Proc.new { |act| (act.is_published) }, :if_update => Proc.new { |act| act.changes['is_published'] }
end
````When a comment is saved, comment_count will get incremented / decremented if the is_published field is dirty.
## CONTRIBUTE
If you'd like to contribute, feel free to fork and merge until your heart is content