Class: Triumph::Achievement

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

Class 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