Module: Mongo::Voteable

Extended by:
ActiveSupport::Concern
Defined in:
lib/voteable_mongo/tasks.rb,
lib/voteable_mongo/voting.rb,
lib/voteable_mongo/helpers.rb,
lib/voteable_mongo/voteable.rb,
lib/voteable_mongo/integrations/mongoid.rb

Defined Under Namespace

Modules: ClassMethods, Helpers, Integrations, Tasks, Voting

Constant Summary collapse

DEFAULT_VOTES =
{
  'up' => [],
  'down' => [],
  'up_count' => 0,
  'down_count' => 0,
  'count' => 0,
  'point' => 0
}
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

#down_voter_idsObject

Array of down voter ids



154
155
156
# File 'lib/voteable_mongo/voteable.rb', line 154

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

#down_voters(klass) ⇒ Object

Get down voters



189
190
191
# File 'lib/voteable_mongo/voteable.rb', line 189

def down_voters(klass)
  klass.where(:_id => { '$in' => down_voter_ids })
end

#down_votes_countObject

Get the number of down votes



169
170
171
# File 'lib/voteable_mongo/voteable.rb', line 169

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

#up_voter_idsObject

Array of up voter ids



149
150
151
# File 'lib/voteable_mongo/voteable.rb', line 149

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

#up_voters(klass) ⇒ Object

Get up voters



184
185
186
# File 'lib/voteable_mongo/voteable.rb', line 184

def up_voters(klass)
  klass.where(:_id => { '$in' =>  up_voter_ids })
end

#up_votes_countObject

Get the number of up votes



164
165
166
# File 'lib/voteable_mongo/voteable.rb', line 164

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

#vote(options) ⇒ Object

Make a vote on this votee

Parameters:

  • options (Hash)

    a hash containings:

    • :voter_id: the voter document id

    • :value: vote :up or vote :down

    • :revote: change from vote up to vote down

    • :unvote: unvote the vote value (:up or :down)



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/voteable_mongo/voteable.rb', line 121

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

#vote_value(voter) ⇒ Object

Get a voted value on this votee

Parameters:

  • voter

    is object or the id of the voter who made the vote



138
139
140
141
142
# File 'lib/voteable_mongo/voteable.rb', line 138

def vote_value(voter)
  voter_id = Helpers.get_mongo_id(voter)
  return :up if up_voter_ids.include?(voter_id)
  return :down if down_voter_ids.include?(voter_id)
end

#voted_by?(voter) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/voteable_mongo/voteable.rb', line 144

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

#voter_idsObject

Array of voter ids



159
160
161
# File 'lib/voteable_mongo/voteable.rb', line 159

def voter_ids
  up_voter_ids + down_voter_ids
end

#voters(klass) ⇒ Object

Get voters



194
195
196
# File 'lib/voteable_mongo/voteable.rb', line 194

def voters(klass)
  klass.where(:_id => { '$in' => voter_ids })
end

#votes_countObject

Get the number of votes



174
175
176
# File 'lib/voteable_mongo/voteable.rb', line 174

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

#votes_pointObject

Get the votes point



179
180
181
# File 'lib/voteable_mongo/voteable.rb', line 179

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