module ThumbsUp::ActsAsVoteable::InstanceMethods

Public Instance Methods

_votes_on() click to toggle source

wraps the dynamic, configured, relationship name

# File lib/acts_as_voteable.rb, line 68
def _votes_on
  self.send(ThumbsUp.configuration[:voteable_relationship_name])
end
ci_plusminus(confidence = 0.95) click to toggle source

The lower bound of a Wilson Score with a default confidence interval of 95%. Gives a more accurate representation of average rating (plusminus) based on the number of positive ratings and total ratings. evanmiller.org/how-not-to-sort-by-average-rating.html

# File lib/acts_as_voteable.rb, line 98
def ci_plusminus(confidence = 0.95)
  require 'statistics2'
  n = self._votes_on.size
  if n == 0
    return 0
  end
  z = Statistics2.pnormaldist(1 - (1 - confidence) / 2)
  phat = 1.0 * votes_for / n
  (phat + z * z / (2 * n) - z * Math.sqrt((phat * (1 - phat) + z * z / (4 * n)) / n)) / (1 + z * z / n)
end
percent_against() click to toggle source
# File lib/acts_as_voteable.rb, line 84
def percent_against
  (votes_against.to_f * 100 / (self._votes_on.size + 0.0001)).round
end
percent_for() click to toggle source
# File lib/acts_as_voteable.rb, line 80
def percent_for
  (votes_for.to_f * 100 / (self._votes_on.size + 0.0001)).round
end
plusminus() click to toggle source

You'll probably want to use this method to display how 'good' a particular voteable is, and/or sort based on it. If you're using this for a lot of voteables, then you'd best use the plusminus_tally method above.

# File lib/acts_as_voteable.rb, line 92
def plusminus
  respond_to?(:plusminus_tally) ? plusminus_tally : (votes_for - votes_against)
end
voted_by?(voter) click to toggle source
# File lib/acts_as_voteable.rb, line 125
def voted_by?(voter)
  0 < Vote.where(
        :voteable_id => self.id,
        :voteable_type => self.class.base_class.name,
        :voter_id => voter.id
      ).count
end
voters_who_voted() click to toggle source
# File lib/acts_as_voteable.rb, line 113
def voters_who_voted
  _votes_on.map(&:voter).uniq
end
voters_who_voted_against() click to toggle source
# File lib/acts_as_voteable.rb, line 121
def voters_who_voted_against
    _votes_on.where(:vote => false).map(&:voter).uniq
end
voters_who_voted_for() click to toggle source
# File lib/acts_as_voteable.rb, line 117
def voters_who_voted_for
    _votes_on.where(:vote => true).map(&:voter).uniq
end
votes_against() click to toggle source
# File lib/acts_as_voteable.rb, line 76
def votes_against
  self._votes_on.where(:vote => false).count
end
votes_count() click to toggle source
# File lib/acts_as_voteable.rb, line 109
def votes_count
  _votes_on.size
end
votes_for() click to toggle source
# File lib/acts_as_voteable.rb, line 72
def votes_for
  self._votes_on.where(:vote => true).count
end