Class: Decidim::Gamification::BadgeStatus

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

Overview

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

Instance Attribute Summary collapse

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.


15
16
17
18
# File 'decidim-core/lib/decidim/gamification/badge_status.rb', line 15

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

Instance Attribute Details

#badgeObject (readonly)

Returns the value of attribute badge


8
9
10
# File 'decidim-core/lib/decidim/gamification/badge_status.rb', line 8

def badge
  @badge
end

Instance Method Details

#levelObject

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

Returns an Integer with the level.


23
24
25
# File 'decidim-core/lib/decidim/gamification/badge_status.rb', line 23

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.


30
31
32
33
34
# File 'decidim-core/lib/decidim/gamification/badge_status.rb', line 30

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.


39
40
41
# File 'decidim-core/lib/decidim/gamification/badge_status.rb', line 39

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