Sha256: 5eaa39599b24fc5cfe741b09b4792eb2767b91571078fe3131e8e7e1a3f39e2b

Contents?: true

Size: 1.05 KB

Versions: 4

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, 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
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
backlog-0.35.0 app/models/absence.rb
backlog-0.35.1 app/models/absence.rb
backlog-0.35.2 app/models/absence.rb
backlog-0.35.3 app/models/absence.rb