Class: AwardEmoji

Inherits:
ApplicationRecord show all
Includes:
GhostUser, Importable, Participable
Defined in:
app/models/award_emoji.rb

Constant Summary collapse

DOWNVOTE_NAME =
"thumbsdown"
UPVOTE_NAME =
"thumbsup"

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GhostUser

#ghost_user?

Methods included from Participable

#participants

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.award_counts_for_user(user, limit = 100) ⇒ Object

Returns the top 100 emoji awarded by the given user.

The returned value is a Hash mapping emoji names to the number of times they were awarded:

{ 'thumbsup' => 2, 'thumbsdown' => 1 }

user - The User to get the awards for. limt - The maximum number of emoji to return.


46
47
48
49
50
51
52
# File 'app/models/award_emoji.rb', line 46

def award_counts_for_user(user, limit = 100)
  limit(limit)
    .where(user: user)
    .group(:name)
    .order('count_all DESC, name ASC')
    .count
end

.votes_for_collection(ids, type) ⇒ Object


31
32
33
34
35
# File 'app/models/award_emoji.rb', line 31

def votes_for_collection(ids, type)
  select('name', 'awardable_id', 'COUNT(*) as count')
    .where('name IN (?) AND awardable_type = ? AND awardable_id IN (?)', [DOWNVOTE_NAME, UPVOTE_NAME], type, ids)
    .group('name', 'awardable_id')
end

Instance Method Details

#downvote?Boolean

Returns:

  • (Boolean)

55
56
57
# File 'app/models/award_emoji.rb', line 55

def downvote?
  self.name == DOWNVOTE_NAME
end

#expire_etag_cacheObject


63
64
65
# File 'app/models/award_emoji.rb', line 63

def expire_etag_cache
  awardable.try(:expire_etag_cache)
end

#upvote?Boolean

Returns:

  • (Boolean)

59
60
61
# File 'app/models/award_emoji.rb', line 59

def upvote?
  self.name == UPVOTE_NAME
end