Adds voting to Rails models to make them sortable by trending (votes weighted against time), or total number of votes
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
app
bin
config
db/migrate
lib
test
.gitignore
Gemfile
MIT-LICENSE
README.md
Rakefile
vote.gemspec

README.md

Vote

Adds voting to Rails models to make them sortable by trending (votes weighted against time), or total number of votes.

Install

gem 'vote'

Config

Vote is library, and auth system agnostic, so no further configuration is needed if:

  • Current logged in user is current_user or
  • Using Devise on User model
  • No offset for rank, which weighs votes against seconds since the Unix Epoch. Setting your own saves a few bits
  • Allowing one vote per user, per element

Define epoch

You may optionally define your own epoch. This makes rank a bit more efficient by reducing the initial baseline for its value length.

Run DateTime.now.to_i

> DateTime.now.to_i
=> 1_418_988_341

then create and add it to an initializer:

# config/initializers/vote.rb
Votable.rank_epoch = 1_418_988_341

Allow multiple votes per user, per element

Within the initializer change Votable.remove_existing_votes from its default value of true:

# config/initializers/vote.rb
Votable.remove_existing_votes = false

This will also allow guests to cast (unlimited) votes.

Assumptions

  • Each user gets a single vote per element - the latest overwrites any previous
  • Voting twice in the same direction on an element removes vote