Created
June 12, 2012 13:36
-
-
Save jparbros/2917552 to your computer and use it in GitHub Desktop.
Voting system extracted a library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Answer < ActiveRecord::Base | |
| include Votes | |
| act_as_votes | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Question < ActiveRecord::Base | |
| include Votes | |
| act_as_votes | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Vote < ActiveRecord::Base | |
| attr_accessible :tendency, :votable_id, :votable_type | |
| belongs_to :voteable, :polymorphic => true, :counter_cache => true | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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