hacker.rb 1.32 KB
Newer Older
mafolz's avatar
mafolz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class Hacker < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates :nickname, :uniqueness => true, :presence => true

  has_many :earnings,    :foreign_key => :hacker_id
  has_many :redemptions, :foreign_key => :hacker_id


  ######################
  ## Instance Methods ##
  ######################

  # Summe aller jemals eingelösten Punkte
  def all_redeemed_points
19
    return redemptions.where("id not in (select subject_id from cancels where subject_type = 'Redemption')").sum('points')
mafolz's avatar
mafolz committed
20 21 22 23
  end

  # Summe aller in den letzten 90 Tagen verdienten Punkte
  def recent_earned_points
24 25
    return earnings.where("id not in (select subject_id from cancels where subject_type = 'Earning')").
        where('created_at >= ?', Date.today - 90).sum('points')
mafolz's avatar
mafolz committed
26 27 28 29 30 31
  end

  # Summe aller jemals verdienten Punkte, 
  # unabhängig davon, ob diese bereits 
  # eingelöst wurden oder nicht
  def all_earned_points
32
    return earnings.sum('points')
mafolz's avatar
mafolz committed
33 34 35 36 37 38 39 40
  end
  
  # Verbleibendes Guthaben, d.h. alle jemals verdienten Punkte
  # abzüglich aller jemals eingelösten Punkte,
  def remaining_points
    return all_earned_points - all_redeemed_points
  end
end