Class: Decidim::Gamification::BadgeStatus
- Inherits:
-
Object
- Object
- Decidim::Gamification::BadgeStatus
- 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 Attribute Summary collapse
-
#badge ⇒ Object
readonly
Returns the value of attribute badge.
Instance Method Summary collapse
-
#initialize(user, badge) ⇒ BadgeStatus
constructor
Public: Initializes the ‘BadgeStatus`.
-
#level ⇒ Object
Public: Returns the current level of a user in a badge.
-
#next_level_in ⇒ Object
Public: Returns the score remaining to get to the next level.
-
#score ⇒ Object
Public: Returns the score of a user on the badge.
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 'lib/decidim/gamification/badge_status.rb', line 15 def initialize(user, badge) @user = user @badge = badge end |
Instance Attribute Details
#badge ⇒ Object (readonly)
Returns the value of attribute badge.
8 9 10 |
# File 'lib/decidim/gamification/badge_status.rb', line 8 def badge @badge end |
Instance Method Details
#level ⇒ Object
Public: Returns the current level of a user in a badge.
Returns an Integer with the level.
23 24 25 |
# File 'lib/decidim/gamification/badge_status.rb', line 23 def level @badge.level_of(score) end |
#next_level_in ⇒ Object
Public: Returns the score remaining to get to the next level.
Returns an Integer with the remaining score.
30 31 32 33 34 |
# 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 ⇒ Object
Public: Returns the score of a user on the badge.
Returns an Integer with the score.
39 40 41 |
# 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 |