{"id":18103287,"url":"https://github.com/jah2488/mongoid-magic-counter-cache","last_synced_at":"2025-10-24T01:06:12.428Z","repository":{"id":2563514,"uuid":"3542867","full_name":"jah2488/mongoid-magic-counter-cache","owner":"jah2488","description":"A Simple Counter Cache Gem for the Mongoid ORM","archived":false,"fork":false,"pushed_at":"2015-03-02T03:26:09.000Z","size":206,"stargazers_count":39,"open_issues_count":1,"forks_count":24,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-26T06:00:49.288Z","etag":null,"topics":["counter-cache","mongodb","mongoid-plugin","rails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":"Unmaintained","scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jah2488.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-02-25T06:22:06.000Z","updated_at":"2018-04-12T06:22:59.000Z","dependencies_parsed_at":"2022-08-24T13:39:34.728Z","dependency_job_id":null,"html_url":"https://github.com/jah2488/mongoid-magic-counter-cache","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jah2488%2Fmongoid-magic-counter-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jah2488%2Fmongoid-magic-counter-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jah2488%2Fmongoid-magic-counter-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jah2488%2Fmongoid-magic-counter-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jah2488","download_url":"https://codeload.github.com/jah2488/mongoid-magic-counter-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231474812,"owners_count":18382160,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["counter-cache","mongodb","mongoid-plugin","rails"],"created_at":"2024-10-31T22:11:23.933Z","updated_at":"2025-10-24T01:06:12.348Z","avatar_url":"https://github.com/jah2488.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"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)\n=======\n\n## DESCRIPTION\n\nMongoid Counter Cache is a simple mongoid extension to add basic counter cache functionality to Embedded and Referenced Mongoid Documents.\n### RDOC\n[http://rdoc.info/github/jah2488/mongoid-magic-counter-cache/master/frames](http://rdoc.info/github/jah2488/mongoid-magic-counter-cache/master/frames)\n\n## INSTALLATION\n\n#### Mongoid Magic Counter Cache requires ruby `1.9.3` at a minimum\n\n### RubyGems\n````sh\n$ [sudo] gem install mongoid_magic_counter_cache\n````\n### GemFile\n````rb\ngem 'mongoid_magic_counter_cache'\n````\n## USAGE\n\nFirst add a field to the document where you will be accessing the counter cache from.\n\n````rb\nclass Library\n  include Mongoid::Document\n\n  field :name\n  field :city\n  field :book_count\n  has_many :books\n\nend\n````\nThen in the referrenced/Embedded document. Include `Mongoid::MagicCounterCache`\n\n````rb\nclass Book\n  include Mongoid::Document\n  include Mongoid::MagicCounterCache\n\n  field :first\n  field :last\n\n  belongs_to    :library\n  counter_cache :library\nend\n````\n\n````rb\n$ @library.book_count\n#=\u003e 990\n````\n### Alternative Syntax\n\nIf you do not wish to use the `model_count` naming convention, you can override the defaults by specifying the `:field` parameter.\n\n````rb\ncounter_cache :library, :field =\u003e \"total_amount_of_books\"\n````\n\n\n### Conditional Counter\n\nIf you want to maintain counter based on certain condition, then you can specify it using `:if`\n\n````rb\nclass Post \n  include Mongoid::Document\n\n  field :article\n  field :comment_count\n\n  has_many :comments\n\nend\n````\nThen in the referrenced/Embedded document, add condition for counter using `:if`\n\n````rb\nclass Comment\n  include Mongoid::Document\n  include Mongoid::MagicCounterCache\n\n  belongs_to :post\n\n  field :remark\n  field :is_published, type: Boolean, default: false\n\n  counter_cache :post, :if =\u003e Proc.new { |act| (act.is_published)  }\nend\n````\n\ncomment_count will get incremented / decremented only when `:if` condition returns `true`\n\n### Conditional Counter After Update\n\nIn 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`\n\nUsing same example as above, in the referrenced/Embedded document, add an additional condition for counter using `:if_update`\n\n````rb\nclass Comment\n  include Mongoid::Document\n  include Mongoid::MagicCounterCache\n\n  belongs_to :post\n\n  field :remark\n  field :is_published, type: Boolean, default: false\n\n  counter_cache :post, :if =\u003e Proc.new { |act| (act.is_published)  }, :if_update =\u003e Proc.new { |act| act.changes['is_published'] }\nend\n````\n\nWhen a comment is saved, comment_count will get incremented / decremented if the is_published field is dirty.\n\n\n\n## CONTRIBUTE\n\n    If you'd like to contribute, feel free to fork and merge until your heart is content\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjah2488%2Fmongoid-magic-counter-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjah2488%2Fmongoid-magic-counter-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjah2488%2Fmongoid-magic-counter-cache/lists"}