GithubHelp home page GithubHelp logo

mongoid_atomic_votes's Introduction

MongoidAtomicVotes

Build Status Code Climate

mongoid_atomic_votes adds possibility to vote on mongoid documents. Each vote mark goes to db by one atomic query that increments vote count, sets vote_value (overall document vote score) and pulls vote mark (embedded document with additional info about voter) to db.

Installation

Add this line to your application's Gemfile:

gem 'mongoid_atomic_votes'

And then execute:

$ bundle

or install it yourself as:

$ gem install mongoid_atomic_votes

Usage

Include Mongoid::AtomicVotes into your mongoid documents for which you wish to add vote possibility:

class Article
  include Mongoid::Document
  include Mongoid::AtomicVotes
end

Then just call methods on model instances to vote/retract:

@article = Article.find(...)
@user = User.find(...)
@article.vote(1, @user) # vote
@article.retract(@user) # retract

You dont need to save your document after you called #vote or #retract.

Getting overall document vote score:

@article.vote_value

Getting overall vote count:

@article.vote_count

Check whether document has votes:

@article.has_votes?

Check whether document was voted by particular voter:

@article.voted_by? @user

Scopes

There are some useful scopes:

Article.voted               # documents that have votes
Article.not_voted           # documents without votes
Article.voted_by(@voter)    # documents, voted by @voter
Article.vote_value_in(3..5) # documents with vote_value in 3..5
Article.highest_voted(5)    # 5 highest voted documents

mongoid_atomic_votes's People

Contributors

hck avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

mahasamatman

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.