GithubHelp home page GithubHelp logo

sameer-tilak / mongoid-magic-counter-cache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jah2488/mongoid-magic-counter-cache

0.0 2.0 0.0 237 KB

A Simple Counter Cache Gem for the Mongoid ORM

License: MIT License

Ruby 100.00%

mongoid-magic-counter-cache's Introduction

Mongoid Magic Counter Cache Build Status Code Climate

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

INSTALLATION

Mongoid Magic Counter Cache requires ruby 1.9.3 at a minimum

RubyGems

$ [sudo] gem install mongoid_magic_counter_cache

GemFile

gem 'mongoid_magic_counter_cache'

USAGE

First add a field to the document where you will be accessing the counter cache from.

class Library
  include Mongoid::Document

  field :name
  field :city
  field :book_count
  has_many :books

end

Then in the referrenced/Embedded document. Include Mongoid::MagicCounterCache

class Book
  include Mongoid::Document
  include Mongoid::MagicCounterCache

  field :first
  field :last

  belongs_to    :library
  counter_cache :library
end
$ @library.book_count
#=> 990

Alternative Syntax

If you do not wish to use the model_count naming convention, you can override the defaults by specifying the :field parameter.

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

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

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

CONTRIBUTE

If you'd like to contribute, feel free to fork and merge until your heart is content

mongoid-magic-counter-cache's People

Contributors

jah2488 avatar niels avatar sameer-tilak avatar panaggio avatar zernie avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.