Allows an arbitrary number of entites (including Users) to vote on models.
This plugin introduces three mixins to your recipe book:
- acts_as_voteable : Intended for content objects like Posts, Comments, etc.
- acts_as_voter : Intended for voting entities, like Users.
- has_karma : Intended for voting entities, or other objects that own the things you're voting on.
This plugin started as an adaptation / update of act_as_voteable. It has grown different from that plugin in several ways:
- You can specify the model name that initiates votes.
- You can, with a little tuning, have more than one entity type vote on more than one model type.
- Adds "acts_as_voter" behavior to the initiator of votes.
- Introduces some newer Rails features like named_scope and :polymorphic keywords
- Adds "has_karma" mixin for identifying key content contributors
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.
./script/plugin install git:https://github.com/peteonrails/vote_fu.git
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 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
class Model < ActiveRecord::Base
acts_as_voteable
end
class User < ActiveRecord::Base
acts_as_voter
end
class Robot < ActiveRecord::Base
acts_as_voter
end
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
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
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.
: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