Module: FormalVote::ActsAsVoteable::InstanceMethods

Defined in:
lib/acts_as_votable.rb

Instance Method Summary collapse

Instance Method Details

#_votes_onObject

wraps the dynamic, configured, relationship name



68
69
70
# File 'lib/acts_as_votable.rb', line 68

def _votes_on
  self.send(FormalVote.configuration[:voteable_relationship_name])
end

#ci_plusminus(confidence = 0.95) ⇒ Object

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



98
99
100
101
102
103
104
105
106
107
# File 'lib/acts_as_votable.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_againstObject



84
85
86
# File 'lib/acts_as_votable.rb', line 84

def percent_against
  (votes_against.to_f * 100 / (self._votes_on.size + 0.0001)).round
end

#percent_forObject



80
81
82
# File 'lib/acts_as_votable.rb', line 80

def percent_for
  (votes_for.to_f * 100 / (self._votes_on.size + 0.0001)).round
end

#plusminusObject

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.



92
93
94
# File 'lib/acts_as_votable.rb', line 92

def plusminus
  respond_to?(:plusminus_tally) ? plusminus_tally : (votes_for - votes_against)
end

#voted_by?(voter) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
# File 'lib/acts_as_votable.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_votedObject



113
114
115
# File 'lib/acts_as_votable.rb', line 113

def voters_who_voted
  _votes_on.map(&:voter).uniq
end

#voters_who_voted_againstObject



121
122
123
# File 'lib/acts_as_votable.rb', line 121

def voters_who_voted_against
    _votes_on.where(:vote => false).map(&:voter).uniq
end

#voters_who_voted_forObject



117
118
119
# File 'lib/acts_as_votable.rb', line 117

def voters_who_voted_for
    _votes_on.where(:vote => true).map(&:voter).uniq
end

#votes_againstObject



76
77
78
# File 'lib/acts_as_votable.rb', line 76

def votes_against
  self._votes_on.where(:vote => false).count
end

#votes_countObject



109
110
111
# File 'lib/acts_as_votable.rb', line 109

def votes_count
  _votes_on.size
end

#votes_forObject



72
73
74
# File 'lib/acts_as_votable.rb', line 72

def votes_for
  self._votes_on.where(:vote => true).count
end