Skip to content

jwachira/vote_fu

 
 

Repository files navigation

vote_fu

Allows an arbitrary number of entites (including Users) to vote on models.

Mixins

This plugin introduces three mixins to your recipe book:

  1. acts_as_voteable : Intended for content objects like Posts, Comments, etc.
  2. acts_as_voter : Intended for voting entities, like Users.
  3. has_karma : Intended for voting entities, or other objects that own the things you're voting on.

Inspiration

This plugin started as an adaptation / update of act_as_voteable. It has grown different from that plugin in several ways:

  1. You can specify the model name that initiates votes.
  2. You can, with a little tuning, have more than one entity type vote on more than one model type.
  3. Adds "acts_as_voter" behavior to the initiator of votes.
  4. Introduces some newer Rails features like named_scope and :polymorphic keywords
  5. Adds "has_karma" mixin for identifying key content contributors

Installation

Use either the plugin or the gem installation method depending on your preference. If you're not sure, the plugin method is simpler. Whichever you choose, create the migration afterward and run it to create the required model.

Via plugin

./script/plugin install git:https://github.com/peteonrails/vote_fu.git 

Via gem

Add the following to your application's environment.rb: config.gem "peteonrails-vote_fu", :lib => 'vote_fu', :source => 'https://gems.github.com'

Install the gem: rake gems:install

Create vote_fu migration

Create a new rails migration using your new vote_fu generator (Note: "VoteableModel" is the name of the model on which you would like votes to be cast, e.g. Comment): ./script/generate vote_fu VoteableModel

Run the migration: rake db:migrate

Usage

Getting Started

Make your ActiveRecord model act as voteable.

class Model < ActiveRecord::Base
  acts_as_voteable
end

Make your ActiveRecord model(s) that vote act as voter.

class User < ActiveRecord::Base
  acts_as_voter
end

class Robot < ActiveRecord::Base
  acts_as_voter
end

To cast a vote for a Model you can do the following:

Shorthand syntax

voter.vote_for(voteable)     # Adds a +1 vote
voter.vote_against(voteable) # Adds a -1 vote
voter.vote(voteable, t_or_f) # Adds either +1 or -1 vote true => +1, false => -1

ActsAsVoteable syntax

The old acts_as_voteable syntax is still supported:

vote = Vote.new(:vote => true)
m    = Model.find(params[:id])
m.votes    << vote
user.votes << vote

Querying votes

Tallying Votes

You can easily retrieve voteable object collections based on the properties of their votes:

@items = Item.tally(
  {  :at_least => 1, 
      :at_most => 10000,  
      :start_at => 2.weeks.ago,
      :end_at => 1.day.ago,
      :limit => 10,
      :order => "items.name desc"
  })

This will select the Items with between 1 and 10,000 votes, the votes having been cast within the last two weeks (not including today), then display the 10 last items in an alphabetical list.

Tally Options:
:start_at    - Restrict the votes to those created after a certain time
:end_at      - Restrict the votes to those created before a certain time
:conditions  - A piece of SQL conditions to add to the query
:limit       - The maximum number of voteables to return
:order       - A piece of SQL to order by. Eg 'votes.count desc' or 'voteable.created_at desc'
:at_least    - Item must have at least X votes
:at_most     - Item may not have more than X votes

Lower level queries