Class: Decidim::Gamification::Badge

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, AttributeObject::Model
Defined in:
lib/decidim/gamification/badge.rb

Overview

This class represents an abstract badge. Instances of this class can define different badge types with different rules such as gaining new levels, etc.

Constant Summary

Constants included from AttributeObject::TypeMap

AttributeObject::TypeMap::Boolean, AttributeObject::TypeMap::Decimal

Instance Method Summary collapse

Methods included from AttributeObject::Model

#[], #[]=, #attributes, #attributes_with_values, #initialize, #to_h

Instance Method Details

#conditionsObject



56
57
58
# File 'lib/decidim/gamification/badge.rb', line 56

def conditions
  I18n.t("conditions", scope: translation_scope)
end

#description(organization_name = nil) ⇒ Object



52
53
54
# File 'lib/decidim/gamification/badge.rb', line 52

def description(organization_name = nil)
  I18n.t("description", organization_name:, scope: translation_scope)
end

#imageObject

Public: Returns an image for this badge.

Returns a String with the image.



72
73
74
# File 'lib/decidim/gamification/badge.rb', line 72

def image
  ActionController::Base.helpers.asset_pack_path("media/images/decidim_gamification_badges_#{name}.svg")
end

#level_of(score) ⇒ Object

Public: Returns the level for this badge given a score.

Returns an Integer with the level.



40
41
42
43
44
45
46
# File 'lib/decidim/gamification/badge.rb', line 40

def level_of(score)
  levels.each_with_index do |threshold, index|
    return index if threshold > score
  end

  levels.length
end

#score_descriptions(score) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/decidim/gamification/badge.rb', line 60

def score_descriptions(score)
  {
    unearned_own: I18n.t("unearned_own", scope: translation_scope),
    description_own: I18n.t("description_own", score:, scope: translation_scope),
    unearned_another: I18n.t("unearned_another", scope: translation_scope),
    description_another: I18n.t("description_another", score:, scope: translation_scope)
  }
end

#translated_nameObject



48
49
50
# File 'lib/decidim/gamification/badge.rb', line 48

def translated_name
  I18n.t "name", scope: translation_scope
end

#valid_for?(model) ⇒ Boolean

Returns:



76
77
78
# File 'lib/decidim/gamification/badge.rb', line 76

def valid_for?(model)
  valid_for.include?(model.class.name.demodulize.underscore.to_sym)
end