Sha256: 0d44d582de52a2c74cd3a04e8e668387ad32bf4404d8b123a37ee46d9b43f9a0

Contents?: true

Size: 925 Bytes

Versions: 1

Compression:

Stored size: 925 Bytes

Contents

class Task < ActiveRecord::Base
  belongs_to :project
  belongs_to :person
  
  attr_accessor :minutes, :hours
  
  validates :project, :presence => true
  validates :person,  :presence => true
  validates :when,    :presence => true
  validates :from,    :presence => true, :unless => :hours_minutes
  validates :to,      :presence => true, :unless => :hours_minutes
  validates_numericality_of :hours,   :only_integer => true, :unless => :from
  validates_numericality_of :minutes, :only_integer => true, :unless => :from
  
  before_save :calculate_hours, :if => :hours_minutes
  
  def hours_minutes
    minutes || hours
  end
  
  def calculate_hours
    self.from = DateTime.now
    self.to = self.from + hours.to_i.hours + minutes.to_i.minutes
  end
  
  # The duration of the task in minutes
  def duration
    minutes = (to.to_f - from.to_f).to_i / 60
    
    minutes < 0 ? 1.day.to_i + minutes : minutes
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bookyt_projects-0.3.0 app/models/task.rb