Project

ez_votes

0.0
No commit activity in last 3 years
No release in over 3 years
Easily add basic voting capability to a model
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

This is a basic voting gem that can be added to any model that you wish to be subject to votes.

To Initialize this gem a polymorphic association is needed so in the database a votes table is required unique to and customizable to your needs. For example:

create_table "votes", force: true do |t|
		t.boolean  "vote"
    t.integer  "user_id"
    t.integer  "voteable_id"
    t.string   "voteable_type"
    t.datetime "created_at"
    t.datetime "updated_at"
end

Then a vote model is also required again customizable to your needs As an example with the above database our vote model would be:

class Vote < ActiveRecord::Base
  belongs_to :creator, class_name: 'User', foreign_key: 'user_id'
  belongs_to :voteable, polymorphic: true

  validates_uniqueness_of :creator, scope: [:voteable_id, :voteable_type]
end

Validation can be added as necessary the simple validation above allows only one vote per user.

Finally with the ez_votes gem installed all you must do is add
include Voteable in the model you wish to be subject to votes

This gem also allows a method called total_votes to be used in the 
views to to calculate the total number of votes