Q&A community for developers, inspired by Stack Overflow.
Users can vote on questions, answers, and comments. This feature is implemented using a polymorphic association built off votable_type
and votable_id
columns in the votes
database table. The association is kept DRY using concerns, simplifying the addition of features with complex interactions such as reputation and badges:
module Votable
extend ActiveSupport::Concern
included do
has_many :votes, as: :votable, dependent: :destroy
has_many :voters, through: :votes
end
def addVote(vote)
case vote.vote_type
when "up_vote"
self.update(score: self.score + 1)
self.author.improveReputation(10)
when "down_vote"
self.update(score: self.score - 1)
self.author.loseReputation(10)
else
return
end
end
def undoVote(vote)
case vote.vote_type
when "up_vote"
self.update(score: self.score - 1)
self.author.loseReputation(10)
when "down_vote"
self.update(score: self.score + 1)
self.author.improveReputation(10)
else
return
end
end
def award_popular_post_badge
badge_name = "Popular Post"
if self.score == 2 && !Badge.find_by(user_id: self.author.id, name: badge_name)
Badge.create!(user_id: self.author.id, badge_type: 1, name: badge_name);
end
end
end
This app uses the following open source libraries:
MIT