Class: Decidim::Gamification::Badge

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
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.

Instance Method Summary collapse

Instance Method Details

#conditionsObject



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

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

#description(organization_name = nil) ⇒ Object



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

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

#imageObject

Public: Returns an image for this badge.

Returns a String with the image.



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

def image
  ActionController::Base.helpers.asset_path("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.



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

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

  levels.length
end

#score_descriptions(score) ⇒ Object



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

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

#translated_nameObject



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

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

#valid_for?(model) ⇒ Boolean

Returns:

  • (Boolean)


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

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