Sha256: bc3a241a10c8edc699cba9fee8a9c81ce14853940f35c9efa58b2ccec57f2176

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

class Absence < ActiveRecord::Base
  extend UserSystem
   
  validates_presence_of :user_id
  validates_presence_of :on
  validates_presence_of :reason

  validates_uniqueness_of :on, :scope => :user_id
  
  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
  
  # Return an array of either an absence or nil for each day of the given week.
  def self.find_by_week(year, week)
    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, current_user && current_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
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
backlog-0.30.0 app/models/absence.rb
backlog-0.31.0 app/models/absence.rb
backlog-0.31.1 app/models/absence.rb