lib/make_voteable/voter.rb in make_voteable-0.0.2 vs lib/make_voteable/voter.rb in make_voteable-0.1.0
- old
+ new
@@ -1,9 +1,13 @@
module MakeVoteable
module Voter
extend ActiveSupport::Concern
+ included do
+ has_many :votings, :class_name => "MakeVoteable::Voting", :as => :voter
+ end
+
module ClassMethods
def voter?
true
end
end
@@ -16,36 +20,36 @@
voting = fetch_voting(voteable)
if voting
if voting.up_vote
- raise MakeVoteable::Exceptions::AlreadyVotedError.new(true)
+ raise Exceptions::AlreadyVotedError.new(true)
else
voting.up_vote = true
voteable.down_votes -= 1
- self.down_votes -= 1 if self.has_attribute?(:down_votes)
+ self.down_votes -= 1 if has_attribute?(:down_votes)
end
else
voting = Voting.create(:voteable => voteable, :voter => self, :up_vote => true)
end
voteable.up_votes += 1
- self.up_votes += 1 if self.has_attribute?(:up_votes)
+ self.up_votes += 1 if has_attribute?(:up_votes)
- MakeVoteable::Voting.transaction do
- self.save
+ Voting.transaction do
+ save
voteable.save
voting.save
end
end
# Up votes the +voteable+, but doesn't raise an error if the votelable was already up voted.
# The vote is simply ignored then.
def up_vote!(voteable)
begin
up_vote(voteable)
- rescue MakeVoteable::Exceptions::AlreadyVotedError
+ rescue Exceptions::AlreadyVotedError
end
end
# Down vote a +voteable+.
# Raises an AlreadyVotedError if the voter already down voted the voteable.
@@ -55,69 +59,69 @@
voting = fetch_voting(voteable)
if voting
unless voting.up_vote
- raise MakeVoteable::Exceptions::AlreadyVotedError.new(false)
+ raise Exceptions::AlreadyVotedError.new(false)
else
voting.up_vote = false
voteable.up_votes -= 1
- self.up_votes -= 1 if self.has_attribute?(:up_votes)
+ self.up_votes -= 1 if has_attribute?(:up_votes)
end
else
voting = Voting.create(:voteable => voteable, :voter => self, :up_vote => false)
end
voteable.down_votes += 1
- self.down_votes += 1 if self.has_attribute?(:down_votes)
+ self.down_votes += 1 if has_attribute?(:down_votes)
- MakeVoteable::Voting.transaction do
- self.save
+ Voting.transaction do
+ save
voteable.save
voting.save
end
end
# Down votes the +voteable+, but doesn't raise an error if the votelable was already down voted.
# The vote is simply ignored then.
def down_vote!(voteable)
begin
down_vote(voteable)
- rescue MakeVoteable::Exceptions::AlreadyVotedError
+ rescue Exceptions::AlreadyVotedError
end
end
# Clears an already done vote on a +voteable+.
# Raises a NotVotedError if the voter didn't voted for the voteable.
def unvote(voteable)
check_voteable(voteable)
voting = fetch_voting(voteable)
- raise MakeVoteable::Exceptions::NotVotedError unless voting
+ raise Exceptions::NotVotedError unless voting
if voting.up_vote
voteable.up_votes -= 1
- self.up_votes -= 1 if self.has_attribute?(:up_votes)
+ self.up_votes -= 1 if has_attribute?(:up_votes)
else
voteable.down_votes -= 1
- self.down_votes -= 1 if self.has_attribute?(:down_votes)
+ self.down_votes -= 1 if has_attribute?(:down_votes)
end
- MakeVoteable::Voting.transaction do
- self.save
+ Voting.transaction do
+ save
voteable.save
voting.destroy
end
end
# Clears an already done vote on a +voteable+, but doesn't raise an error if
# the voteable was not voted. It ignores the unvote then.
def unvote!(voteable)
begin
unvote(voteable)
- rescue MakeVoteable::Exceptions::NotVotedError
+ rescue Exceptions::NotVotedError
end
end
# Returns true if the voter voted for the +voteable+.
def voted?(voteable)
@@ -151,9 +155,9 @@
:voter_type => self.class.to_s,
:voter_id => self.id).try(:first)
end
def check_voteable(voteable)
- raise MakeVoteable::Exceptions::InvalidVoteableError unless voteable.class.voteable?
+ raise Exceptions::InvalidVoteableError unless voteable.class.voteable?
end
end
end