Sha256: 4c20631dcdaf676765e02637bb44e7612af8eb8cb6ec46a485515757a764c6cc

Contents?: true

Size: 1.13 KB

Versions: 64

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module Decidim
  module Gamification
    # This class is responsible to figure out the status of a user regarding
    # a certain badge.
    class BadgeStatus
      attr_reader :badge

      # Public: Initializes the `BadgeStatus`.
      #
      # user  - The user of whom to check the status.
      # badge - The badge for which to check the progress.
      #
      def initialize(user, badge)
        @user = user
        @badge = badge
      end

      # Public: Returns the current level of a user in a badge.
      #
      # Returns an Integer with the level.
      def level
        @badge.level_of(score)
      end

      # Public: Returns the score remaining to get to the next level.
      #
      # Returns an Integer with the remaining score.
      def next_level_in
        return nil if level >= @badge.levels.count

        @badge.levels[level] - score
      end

      # Public: Returns the score of a user on the badge.
      #
      # Returns an Integer with the score.
      def score
        @score ||= BadgeScore.find_by(user: @user, badge_name: @badge.name).try(:value) || 0
      end
    end
  end
end

Version data entries

64 entries across 64 versions & 1 rubygems

Version Path
decidim-core-0.20.1 lib/decidim/gamification/badge_status.rb
decidim-core-0.20.0 lib/decidim/gamification/badge_status.rb
decidim-core-0.19.1 lib/decidim/gamification/badge_status.rb
decidim-core-0.19.0 lib/decidim/gamification/badge_status.rb