class Decidim::Gamification::BadgeStatus

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

Attributes

badge[R]

Public Class Methods

new(user, badge) click to toggle source

Public: Initializes the `BadgeStatus`.

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

# File lib/decidim/gamification/badge_status.rb, line 15
def initialize(user, badge)
  @user = user
  @badge = badge
end

Public Instance Methods

level() click to toggle source

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

Returns an Integer with the level.

# File lib/decidim/gamification/badge_status.rb, line 23
def level
  @badge.level_of(score)
end
next_level_in() click to toggle source

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

Returns an Integer with the remaining score.

# File lib/decidim/gamification/badge_status.rb, line 30
def next_level_in
  return nil if level >= @badge.levels.count

  @badge.levels[level] - score
end
score() click to toggle source

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

Returns an Integer with the score.

# File lib/decidim/gamification/badge_status.rb, line 39
def score
  @score ||= BadgeScore.find_by(user: @user, badge_name: @badge.name).try(:value) || 0
end