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,
app/controllers/decidim/gamification/badges_controller.rb 
Defined Under Namespace
Classes: Badge, BadgeEarnedEvent, BadgeRegistry, BadgeScore, BadgeScorer, BadgeStatus, BadgesController, 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)  ⇒ 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.
      64 65 66  | 
    
      # File 'lib/decidim/gamification.rb', line 64 def self.badge_registry @badge_registry ||= Decidim::Gamification::BadgeRegistry.new end  | 
  
.badges ⇒ Object
Public: Returns all available badges.
Returns an Array<Badge>
      71 72 73  | 
    
      # File 'lib/decidim/gamification.rb', line 71 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.
      43 44 45 46 47 48  | 
    
      # File 'lib/decidim/gamification.rb', line 43 def self.decrement_score(user, badge_name, amount = 1) return unless amount.positive? return unless user.is_a?(Decidim::UserBaseEntity) 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.
      78 79 80  | 
    
      # File 'lib/decidim/gamification.rb', line 78 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.
      29 30 31 32 33 34  | 
    
      # File 'lib/decidim/gamification.rb', line 29 def self.increment_score(user, badge_name, amount = 1) return unless amount.positive? return unless user.is_a?(Decidim::UserBaseEntity) BadgeScorer.new(user, find_badge(badge_name)).increment(amount) end  | 
  
.register_badge(name) ⇒ 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.
      92 93 94  | 
    
      # File 'lib/decidim/gamification.rb', line 92 def self.register_badge(name, &) badge_registry.register(name, &) 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.
      103 104 105 106 107 108 109 110 111 112 113 114 115 116 117  | 
    
      # File 'lib/decidim/gamification.rb', line 103 def self.reset_badges(users = nil) return reset_badges(User.all) && reset_badges(UserGroup.all) unless users 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)) if badge.valid_for?(user) end else Rails.logger.info "Badge cannot be reset since it does not 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.
      57 58 59 60 61  | 
    
      # File 'lib/decidim/gamification.rb', line 57 def self.set_score(user, badge_name, score) return unless user.is_a?(Decidim::UserBaseEntity) 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 19 20  | 
    
      # File 'lib/decidim/gamification.rb', line 16 def self.status_for(user, badge_name) return unless user.is_a?(Decidim::UserBaseEntity) BadgeStatus.new(user, find_badge(badge_name)) end  |