Sha256: 8ada4d9289b3ef02cb952280ac8eaf27bc3ae232c9b1be494bd0b06df4d8b719

Contents?: true

Size: 1.06 KB

Versions: 8

Compression:

Stored size: 1.06 KB

Contents

class Activity < ActiveRecord::Base
  belongs_to :project
  belongs_to :person
  
  attr_accessor :minutes, :hours
  
  validates :project, :presence => true, :allow_blank => false
  validates :person,  :presence => true, :allow_blank => false
  validates_date :when, :allow_nil => false, :allow_blank => false
  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

8 entries across 8 versions & 1 rubygems

Version Path
bookyt_projects-0.10.0 app/models/activity.rb
bookyt_projects-0.9.0 app/models/activity.rb
bookyt_projects-0.8.0 app/models/activity.rb
bookyt_projects-0.7.0 app/models/activity.rb
bookyt_projects-0.6.6 app/models/activity.rb
bookyt_projects-0.6.5 app/models/activity.rb
bookyt_projects-0.6.4 app/models/activity.rb
bookyt_projects-0.6.3 app/models/activity.rb