Class: Absence

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
UserSystem
Defined in:
app/models/absence.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_week(year, week, user = current_user) ⇒ Object

Return an array of either an absence or nil for each day of the given week.



20
21
22
23
24
25
26
27
28
# File 'app/models/absence.rb', line 20

def self.find_by_week(year, week, user = current_user)
  first_date = Date.commercial(year, week, 1)
  last_date = first_date + 6
  results = self.find(:all, :conditions => ['"on" BETWEEN ? AND ? AND user_id = ?', first_date, last_date, user && user.id], :order => '"on"')
  (0..6).each do |day|
    results.insert(day, nil) if results[day] && results[day].on > first_date + day
  end
  results
end

Instance Method Details

#validateObject



10
11
12
13
14
15
16
17
# File 'app/models/absence.rb', line 10

def validate
  if Work.exists? ['user_id = ? AND (started_on = ? OR (started_on <= ? AND completed_at IS NOT NULL AND completed_at >= ?))', user_id, on, on, on]
    errors.add :on, "You have already registered work on this date."
  end
  if PublicHoliday.exists? :on => on
    errors.add :on, "You cannot mark absence on a public holiday."
  end
end