Module: Decidim::Gamification
- Defined in:
- lib/decidim/gamification.rb,
lib/decidim/gamification/badge.rb,
lib/decidim/gamification/base_event.rb,
lib/decidim/gamification/badge_scorer.rb,
lib/decidim/gamification/badge_status.rb,
lib/decidim/gamification/badge_registry.rb,
lib/decidim/gamification/level_up_event.rb,
app/models/decidim/gamification/badge_score.rb,
lib/decidim/gamification/badge_earned_event.rb
Defined Under Namespace
Classes: Badge, BadgeEarnedEvent, BadgeRegistry, BadgeScore, BadgeScorer, BadgeStatus, BaseEvent, LevelUpEvent
Class Method Summary collapse
-
.badge_registry ⇒ Object
Semi-private: The BadgeRegistry to register global badges to.
-
.badges ⇒ Object
Public: Returns all available badges.
-
.decrement_score(user, badge_name, amount = 1) ⇒ Object
Public: Decrement the score of a user for a badge.
-
.find_badge(name) ⇒ Object
Public: Finds a Badge given a name.
-
.increment_score(user, badge_name, amount = 1) ⇒ Object
Public: Increments the score of a user for a badge.
-
.register_badge(name, &block) ⇒ Object
Public: Registers a new Badge.
-
.reset_badges(users = nil) ⇒ Object
Public: Resets all the badge scores using each of the badges’ reset methods (if available).
-
.set_score(user, badge_name, score) ⇒ Object
Public: Sets the score of a user for a badge.
-
.status_for(user, badge_name) ⇒ Object
Public: Returns a the status of a badge given a user and a badge name.
Class Method Details
.badge_registry ⇒ Object
Semi-private: The BadgeRegistry to register global badges to.
54 55 56 |
# File 'lib/decidim/gamification.rb', line 54 def self.badge_registry @badge_registry ||= Decidim::Gamification::BadgeRegistry.new end |
.badges ⇒ Object
Public: Returns all available badges.
Returns an Array<Badge>
61 62 63 |
# File 'lib/decidim/gamification.rb', line 61 def self.badges badge_registry.all end |
.decrement_score(user, badge_name, amount = 1) ⇒ Object
Public: Decrement the score of a user for a badge.
user - A User for whom to increase the score. badge_name - The name of the badge for which to increase the score. amount - (Optional) The amount to decrease. Defaults to 1.
Returns nothing.
38 39 40 |
# File 'lib/decidim/gamification.rb', line 38 def self.decrement_score(user, badge_name, amount = 1) BadgeScorer.new(user, find_badge(badge_name)).decrement(amount) end |
.find_badge(name) ⇒ Object
Public: Finds a Badge given a name.
Returns a Badge if found, nil otherwise.
68 69 70 |
# File 'lib/decidim/gamification.rb', line 68 def self.find_badge(name) badge_registry.find(name) end |
.increment_score(user, badge_name, amount = 1) ⇒ Object
Public: Increments the score of a user for a badge.
user - A User for whom to increase the score. badge_name - The name of the badge for which to increase the score. amount - (Optional) The amount to increase. Defaults to 1.
Returns nothing.
27 28 29 |
# File 'lib/decidim/gamification.rb', line 27 def self.increment_score(user, badge_name, amount = 1) BadgeScorer.new(user, find_badge(badge_name)).increment(amount) end |
.register_badge(name, &block) ⇒ Object
Public: Registers a new Badge.
Example:
Decidim.register_badge(:foo) do |badge|
badge.levels = [1, 10, 50]
end
Returns nothing if registered successfully, raises an exception otherwise.
82 83 84 |
# File 'lib/decidim/gamification.rb', line 82 def self.register_badge(name, &block) badge_registry.register(name, &block) end |
.reset_badges(users = nil) ⇒ Object
Public: Resets all the badge scores using each of the badges’ reset methods (if available). This is useful if the badges ever get inconsistent.
users - The Array of Users to reset the score.
Returns nothing.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/decidim/gamification.rb', line 93 def self.reset_badges(users = nil) users ||= User.all badges.each do |badge| Rails.logger.info "Resetting #{badge.name}..." if badge.reset users.find_each do |user| set_score(user, badge.name, badge.reset.call(user)) end else Rails.logger.info "Badge can't be reset since it doesn't have a reset method." end end end |
.set_score(user, badge_name, score) ⇒ Object
Public: Sets the score of a user for a badge.
user - A User for whom to set the score. badge_name - The name of the badge for which to increase the score. score - The score to set.
Returns nothing.
49 50 51 |
# File 'lib/decidim/gamification.rb', line 49 def self.set_score(user, badge_name, score) BadgeScorer.new(user, find_badge(badge_name)).set(score) end |
.status_for(user, badge_name) ⇒ Object
Public: Returns a the status of a badge given a user and a badge name.
Returns a ‘BadgeStatus` instance.
16 17 18 |
# File 'lib/decidim/gamification.rb', line 16 def self.status_for(user, badge_name) BadgeStatus.new(user, find_badge(badge_name)) end |