module TreasureHunt::Hunter::Model::InstanceMethods

Public Instance Methods

achieve!(reward, target=nil) click to toggle source
# File lib/treasure_hunt/treasure_hunter.rb, line 36
def achieve!(reward, target=nil)
  reward = ::Reward.find_by_name(reward.to_s.humanize) if reward.is_a? Symbol
  achievement = ::Achievement.create(:reward => reward, :targetable => target)
  self.achievements << achievement

  achievement
end
can_achieve?(reward) click to toggle source
# File lib/treasure_hunt/treasure_hunter.rb, line 30
def can_achieve?(reward)
  reward = ::Reward.find_by_name(reward.to_s.humanize) if reward.is_a? Symbol

  ::Achievement.new(:user => self, :reward => reward).valid?
end
has_achievement?(reward) click to toggle source
# File lib/treasure_hunt/treasure_hunter.rb, line 52
def has_achievement?(reward)
  reward = ::Reward.find_by_name(reward.to_s.humanize) if reward.is_a? Symbol
  achievements.where(:reward_id => reward.id).any?
end
rank() click to toggle source
# File lib/treasure_hunt/treasure_hunter.rb, line 22
def rank
  self.class.count(:all, :conditions => ['points > ?', self.points]) + 1
end
time_to_unlock(reward) click to toggle source
# File lib/treasure_hunt/treasure_hunter.rb, line 44
def time_to_unlock(reward)
  reward = ::Reward.find_by_name(reward.to_s.humanize) if reward.is_a? Symbol
  achievement = ::Achievement.new(:user => self, :reward => reward)
  remaining = (achievement.find_all_recently_achieved.last.updated_at rescue DateTime.new ) + reward.every.seconds - DateTime.now

  [0, remaining].max
end
update_points() click to toggle source
# File lib/treasure_hunt/treasure_hunter.rb, line 26
def update_points
  self.update_attribute(:points, self.achievements.sum(:points))
end