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.25.0.rc4 lib/decidim/gamification/badge_status.rb
decidim-core-0.25.0.rc3 lib/decidim/gamification/badge_status.rb
decidim-core-0.25.0.rc2 lib/decidim/gamification/badge_status.rb
decidim-core-0.25.0.rc1 lib/decidim/gamification/badge_status.rb
decidim-core-0.24.3 lib/decidim/gamification/badge_status.rb
decidim-core-0.23.6 lib/decidim/gamification/badge_status.rb
decidim-core-0.24.2 lib/decidim/gamification/badge_status.rb
decidim-core-0.23.5 lib/decidim/gamification/badge_status.rb
decidim-core-0.24.1 lib/decidim/gamification/badge_status.rb
decidim-core-0.24.0 lib/decidim/gamification/badge_status.rb
decidim-core-0.24.0.rc2 lib/decidim/gamification/badge_status.rb
decidim-core-0.23.4 lib/decidim/gamification/badge_status.rb
decidim-core-0.24.0.rc1 lib/decidim/gamification/badge_status.rb
decidim-core-0.23.3 lib/decidim/gamification/badge_status.rb
decidim-core-0.23.2 lib/decidim/gamification/badge_status.rb
decidim-core-0.23.1 lib/decidim/gamification/badge_status.rb
decidim-core-0.23.1.rc1 lib/decidim/gamification/badge_status.rb
decidim-core-0.23.0 lib/decidim/gamification/badge_status.rb
decidim-core-0.22.0 lib/decidim/gamification/badge_status.rb
decidim-core-0.21.0 lib/decidim/gamification/badge_status.rb