Module: Mongo::Voteable::InstanceMethods

Defined in:
lib/voteable_mongo/voteable.rb

Instance Method Summary collapse

Instance Method Details

#down_voter_idsObject

Array of down voter ids



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

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

#down_voters(klass) ⇒ Object

Get down voters



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

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

#down_votes_countObject

Get the number of down votes



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

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

#up_voter_idsObject

Array of up voter ids



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

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

#up_voters(klass) ⇒ Object

Get up voters



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

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

#up_votes_countObject

Get the number of up votes



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

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)



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/voteable_mongo/voteable.rb', line 127

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



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

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)


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

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

#voter_idsObject

Array of voter ids



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

def voter_ids
  up_voter_ids + down_voter_ids
end

#voters(klass) ⇒ Object

Get voters



200
201
202
# File 'lib/voteable_mongo/voteable.rb', line 200

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

#votes_countObject

Get the number of votes



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

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

#votes_pointObject

Get the votes point



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

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