Class: Triumph::AchievementCondition

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

Instance Method Summary collapse

Instance Method Details

#check(object) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/triumph/achievement_condition.rb', line 8

def check(object)
  user = object.user
  object_class = object.class.to_s.underscore.downcase
  objects = user.send(object.class.to_s.pluralize.underscore.to_sym)
  
  unless objects.empty?
    case objects.first.send(comparison_attribute)
    when Integer
      comparison_value = comparison_value.to_i
    when Float
      comparison_value = comparison_value.to_f
    when TrueClass, FalseClass
      comparison_value = comparison_value == "true" ? true : false
    end
  end
      
  valid_objects = objects.select{|o| o.send(comparison_attribute).
                  send(comparison_operator, comparison_value)}
  num_valid_objects = valid_objects.count
  if !num_valid_objects.send(total_operator.to_s, total_value)
    false
  end            
  true
end