Module: Triumph::Achievements

Defined in:
lib/triumph/achievements.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/triumph/achievements.rb', line 4

def self.included(base)
  base.class_eval do
    #for some reason, this is not adding the proper association to the users model
    # when I do 'include Achievemnts', though everything else works as expected.
    # For now, I'm manually adding the 'has_many :completed_achievements' to the user
    # model in my sample app, but I need to figure this out.
    has_many :completed_achievements, :class_name => 'Triumph::CompletedAchievement'
  end
end

Instance Method Details

#grant_achievement(achievement) ⇒ Object



14
15
16
17
18
# File 'lib/triumph/achievements.rb', line 14

def grant_achievement(achievement)
  unless self.has_achievement?(achievement)
    CompletedAchievement.create!(:achievement_id => achievement.id, :user_id => self.id)
  end
end

#has_achievement?(achievement) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/triumph/achievements.rb', line 20

def has_achievement?(achievement)
  CompletedAchievement.where("achievement_id = ? AND user_id = ?", achievement.id, self.id).count >= 1     
end