hour.rb 1.96 KB
Newer Older
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
1
class Hour < ApplicationRecord
2
  include ImportRelation
3
  include ReviewsCommon
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
4

5
  belongs_to :volunteer, -> { with_deleted }, inverse_of: 'hours'
6

7
  belongs_to :hourable, polymorphic: true, optional: true
8

9
  belongs_to :reviewer, -> { with_deleted }, class_name: 'User', foreign_key: 'reviewer_id',
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
10
    inverse_of: 'reviewed_hours', optional: true
11

12
  belongs_to :billing_expense, -> { with_deleted }, optional: true, inverse_of: 'hours'
Tugce Nur Tas's avatar
Tugce Nur Tas committed
13
  belongs_to :certificate, optional: true
14

15
16
  belongs_to :semester_process_volunteer, optional: true

17
  validates :hours, presence: true, numericality: { greater_than: 0 }
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
18
  validates :meeting_date, presence: true
19
  validates :hourable, presence: true
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
20

21
  scope :billable, (-> { where(billing_expense: nil) })
22
  scope :billed, (-> { where.not(billing_expense: nil) })
23
  scope :semester, lambda { |date = nil|
24
25
26
27
28
29
30
31
32
    return all if date.blank?
    date = Time.zone.parse(date) unless date.is_a? Time
    return all if date.blank?
    semester_with_date(date)
  }

  scope :semester_with_date, lambda { |date|
    date_between_inclusion(:meeting_date, date.advance(days: 1),
      date.advance(months: BillingExpense::SEMESTER_LENGTH))
Zsolt Benke's avatar
Zsolt Benke committed
33
  }
34

Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
35
  scope :since_last_submitted, lambda { |submitted_at|
36
    where('hours.created_at > ?', submitted_at) if submitted_at
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
37
38
  }

39
40
  scope :assignment, (-> { where(hourable_type: 'Assignment') })
  scope :group_offer, (-> { where(hourable_type: 'GroupOffer') })
41
  scope :from_assignments, lambda { |assignment_ids|
42
    assignment.where(hourable_id: assignment_ids)
43
44
  }
  scope :from_group_offers, lambda { |group_offer_ids|
45
    group_offer.where(hourable_id: group_offer_ids)
46
47
  }

48
49
50
51
52
53
54
55
  def assignment?
    hourable_type == 'Assignment'
  end

  def group_offer?
    hourable_type == 'GroupOffer'
  end

56
  def self.total_hours
Emily Wangler's avatar
Emily Wangler committed
57
    sum(:hours)
58
  end
59
60
61
62
63
64
65
66

  def hourable_id_and_type=(id_and_type)
    self.hourable_id, self.hourable_type = id_and_type.split(',', 2)
  end

  def hourable_id_and_type
    "#{hourable_id},#{hourable_type}"
  end
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
67
end