A very simple mongomapper plugin to add vote up/down functionality to your application
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.document
.gitignore
.rspec
Gemfile
Gemfile.lock
LICENSE.txt
README.rdoc
Rakefile
VERSION
mm-voteable.gemspec

README.rdoc

mm-voteable

A very simple mongomapper plugin to add vote up/down superpowers to your models.

v1.2 is a mongomapper 0.9.0 update to support the new plugin system if you are using mongomapper 0.8.x please use version 1.1 of the gem

Usage

Install the gem

gem install mm-voteable

Or add it to your Gemfile

gem 'mm-voteable'

Then add the voteable plugin to you MongoMapper::Document

class Post
  include MongoMapper::Document
  plugin MongoMapper::Plugins::Voteable

  def on_add_vote(vote)
    do something when a vote is added...
  end
end

You must implement the on_add_vote callback! - It can be an empty implementation, but I use it to create ActivityStream Items after someone votes on a NewsArticle

@post = Post.create
@voter = User.create

@post.add_vote!(1, @user)
@post.add_vote!(1, @user)

@post.votes_count #=> 2
@posts.votes #=> [<Vote>]
@posts.voters #=> [<User>]
@posts.voters.include?(@voter) #=> true

Limitations

I made this plugin for voting up and down on articles (kind of like stack overflow) so there are currently some limitations

  • the vote value can only be one of 1 or -1

  • the voteable model has a 'user' belongs_to association

  • a user can only vote once on a voteable item

  • the owner of a voteable item, cannot vote

Feel free to code any of these limitations out and send me a patch, this is just how I have used it so far

Contributing to mm-voteable

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet

  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright © 2010 Luke Cunningham. See LICENSE.txt for further details.