Skip to content

Instantly share code, notes, and snippets.

@jparbros
Created June 12, 2012 13:36
Show Gist options
  • Select an option

  • Save jparbros/2917552 to your computer and use it in GitHub Desktop.

Select an option

Save jparbros/2917552 to your computer and use it in GitHub Desktop.
Voting system extracted a library
class Answer < ActiveRecord::Base
include Votes
act_as_votes
end
class Question < ActiveRecord::Base
include Votes
act_as_votes
end
class Vote < ActiveRecord::Base
attr_accessible :tendency, :votable_id, :votable_type
belongs_to :voteable, :polymorphic => true, :counter_cache => true
end
module Votes
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def act_as_votes
has_many :votes, :as => :voteable, :dependent => :destroy
end
def most_voted
order('votes_count desc')
end
end
def vote_up
votes.create(tendency: 1)
end
def vote_down
votes.create(tendency: -1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment