Sha256: 4d404f321b0097d0401e2ba9f417835031a6a75b89105526840cb2f520a64aa1

Contents?: true

Size: 1009 Bytes

Versions: 2

Compression:

Stored size: 1009 Bytes

Contents

class Activity < 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
  
  def hours_minutes
    minutes || hours
  end
  
  def calculate_hours
    unless hours.empty? or minutes.empty?
      self.from = DateTime.now
      self.to = self.from + hours.to_i.hours + minutes.to_i.minutes
    end
  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

  def to_s
    "#{duration} => #{person}"
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bookyt_projects-0.6.1 app/models/activity.rb
bookyt_projects-0.6.0 app/models/activity.rb