hacker.rb 1.71 KB
Newer Older
mafolz's avatar
mafolz committed
1
2
3
4
5
6
7
8
9
10
11
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

12
13
  has_many :earning_cancels,    :through => :earnings,    :source => :cancel
  has_many :redemption_cancels, :through => :redemptions, :source => :cancel
mafolz's avatar
mafolz committed
14
15
16
17

  ######################
  ## Instance Methods ##
  ######################
18
19
20
21
22
23
24
25
26
27
  #
  
  def redemptions_without_cancel
    return redemptions.
      where(
        Redemption.arel_table[:id].not_in(
          self.redemption_cancels.map(&:subject_id)
        )
      )
  end
mafolz's avatar
mafolz committed
28
29
30

  # Summe aller jemals eingelösten Punkte
  def all_redeemed_points
31
    return redemptions_without_cancel.sum('points')
mafolz's avatar
mafolz committed
32
33
  end

34
35
36
37
38
39
40
  def earnings_without_cancel
    return earnings.
      where(
        Earning.arel_table[:id].not_in(
          self.earning_cancels.map(&:subject_id)
        )
      )
mafolz's avatar
mafolz committed
41
42
43
44
45
46
  end

  # Summe aller jemals verdienten Punkte, 
  # unabhängig davon, ob diese bereits 
  # eingelöst wurden oder nicht
  def all_earned_points
47
    return earnings_without_cancel.sum('points')
mafolz's avatar
mafolz committed
48
  end
49
50
51
52
53
54
55

  # Summe aller in den letzten 90 Tagen verdienten Punkte
  def recent_earned_points
    return earnings_without_cancel.
        where('created_at >= ?', Date.today - 90).sum('points')
  end

mafolz's avatar
mafolz committed
56
57
58
59
60
61
62
  
  # 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