Skip to content

Instantly share code, notes, and snippets.

@creich
Created July 20, 2012 17:45
Show Gist options
  • Save creich/3152184 to your computer and use it in GitHub Desktop.
Save creich/3152184 to your computer and use it in GitHub Desktop.
class TimeBooking < ActiveRecord::Base
unloadable
attr_accessible :started_on, :stopped_at, :time_entry_id, :time_log_id, :virtual, :project
belongs_to :project
belongs_to :time_log
belongs_to :time_entry, :dependent => :delete
has_one :virtual_comment, :dependent => :delete
validates_presence_of :time_log_id
validates :time_entry_id, :presence => true, :unless => Proc.new { |tb| tb.virtual }
validates_associated :virtual_comment, :if => Proc.new { |tb| tb.virtual }
# <SOME OTHER METHODS>
def hours_spent
((stopped_at.to_i - started_on.to_i) / 3600.0).to_f
end
end
class BookingError < StandardError
attr_reader :message
def initialize(message)
@message = message
end
end
class TimeLog < ActiveRecord::Base
unloadable
attr_accessible :user_id, :started_on, :stopped_at, :project_id, :comments, :issue_id, :spent_time
attr_accessor :issue_id, :spent_time
belongs_to :user
has_many :time_bookings, :dependent => :delete_all
has_many :time_entries, :through => :time_bookings
def initialize(arguments = nil, *args)
super(arguments)
end
# <SOME OTHER METHODS>
# returns the sum of bookable time of an time entry
# if log was not booked at all, so the whole time is bookable
def bookable_hours
time_booked = 0
time_bookings.each do |tb|
time_booked += tb.hours_spent
end
hours_spent - time_booked
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment