Class: Decidim::Gamification::BadgeStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/gamification/badge_status.rb

Overview

This class is responsible to figure out the status of a user regarding a certain badge.

Instance Method Summary collapse

Constructor Details

#initialize(user, badge) ⇒ BadgeStatus

Public: Initializes the ‘BadgeStatus`.

user - The user of whom to check the status. badge - The badge for which to check the progress.



13
14
15
16
# File 'lib/decidim/gamification/badge_status.rb', line 13

def initialize(user, badge)
  @user = user
  @badge = badge
end

Instance Method Details

#levelObject

Public: Returns the current level of a user in a badge.

Returns an Integer with the level.



21
22
23
# File 'lib/decidim/gamification/badge_status.rb', line 21

def level
  @badge.level_of(score)
end

#next_level_inObject

Public: Returns the score remaining to get to the next level.

Returns an Integer with the remaining score.



28
29
30
31
# File 'lib/decidim/gamification/badge_status.rb', line 28

def next_level_in
  return nil if level >= @badge.levels.count
  @badge.levels[level] - score
end

#scoreObject

Public: Returns the score of a user on the badge.

Returns an Integer with the score.



36
37
38
# File 'lib/decidim/gamification/badge_status.rb', line 36

def score
  @score ||= BadgeScore.find_by(user: @user, badge_name: @badge.name).try(:value) || 0
end