Class: Triumph::Achievement

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/triumph/achievement.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_achievements_for(object) ⇒ Object

This method is currently long, and I plan to refactor much of it tomorrow I plan to move much of the checking logic into the CompletedAchievement class



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/triumph/achievement.rb', line 12

def self.check_achievements_for(object)

  user = object.user
  object_class = object.class.to_s.underscore.downcase
  potential_achievements = Triumph::Achievement.where("observe_class = ?", object_class)    

  potential_achievements.each do |achievement|
    total = user.send(object.class.to_s.pluralize.underscore.to_sym).count
    if total >= achievement.quantity
      condition_results = []
      achievement.achievement_conditions.each do |condition|
        condition.check(object)
      end
      user.grant_achievement(achievement) unless condition_results.include? false
    end
  end
end

Instance Method Details

#to_sObject



30
31
32
33
34
35
36
37
# File 'app/models/triumph/achievement.rb', line 30

def to_s
  condition = achievement_conditions.first

  return "Grant this achievement when a user has more than #{quantity} #{observe_class.pluralize} 
  and there are #{condition.total_operator} #{condition.total_value}
  where #{condition.comparison_attribute} is 
  #{condition.comparison_operator} #{condition.comparison_value}"
end