Module: Mongoid::Voteable

Extended by:
ActiveSupport::Concern
Defined in:
lib/voteable_mongoid/voteable.rb,
lib/voteable_mongoid/voteable/tasks.rb,
lib/voteable_mongoid/voteable/votes.rb,
lib/voteable_mongoid/voteable/voting.rb

Defined Under Namespace

Modules: ClassMethods, Tasks, Voting Classes: Votes

Constant Summary

VOTEABLE =

How many points should be assigned for each up or down vote and other options This hash should manipulated using voteable method

{}

Instance Method Summary (collapse)

Instance Method Details

- (Object) down_voter_ids

Array of down voter ids



95
96
97
# File 'lib/voteable_mongoid/voteable.rb', line 95

def down_voter_ids
  votes.try(:[], 'down') || []
end

- (Object) down_votes_count

Get the number of down votes



105
106
107
# File 'lib/voteable_mongoid/voteable.rb', line 105

def down_votes_count
  votes.try(:[], 'down_count') || 0
end

- (Object) up_voter_ids

Array of up voter ids



90
91
92
# File 'lib/voteable_mongoid/voteable.rb', line 90

def up_voter_ids
  votes.try(:[], 'up') || []
end

- (Object) up_votes_count

Get the number of up votes



100
101
102
# File 'lib/voteable_mongoid/voteable.rb', line 100

def up_votes_count
  votes.try(:[], 'up_count') || 0
end

- (Object) vote(options)

Make a vote on this votee



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/voteable_mongoid/voteable.rb', line 62

def vote(options)
  options[:votee_id] = id
  options[:votee] = self
  options[:voter_id] ||= options[:voter].id

  if options[:unvote]
    options[:value] ||= vote_value(options[:voter_id])
  else
    options[:revote] ||= vote_value(options[:voter_id]).present?
  end

  self.class.vote(options)
end

- (Object) vote_value(voter)

Get a voted value on this votee



79
80
81
82
83
# File 'lib/voteable_mongoid/voteable.rb', line 79

def vote_value(voter)
  voter_id = voter.is_a?(BSON::ObjectId) ? voter : voter.id
  return :up if up_voter_ids.include?(voter_id)
  return :down if down_voter_ids.include?(voter_id)
end

- (Boolean) voted_by?(voter)



85
86
87
# File 'lib/voteable_mongoid/voteable.rb', line 85

def voted_by?(voter)
  !!vote_value(voter)
end

- (Object) votes_count

Get the number of votes



110
111
112
# File 'lib/voteable_mongoid/voteable.rb', line 110

def votes_count
  votes.try(:[], 'count') || 0
end

- (Object) votes_point

Get the votes point



115
116
117
# File 'lib/voteable_mongoid/voteable.rb', line 115

def votes_point
  votes.try(:[], 'point') || 0
end