Class: Punch

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/punchcard/punch.rb

Instance Method Summary collapse

Instance Method Details

#difference_in_minutesObject

Calculates the difference between checkin and checkout or time now when pending in minutes



33
34
35
36
# File 'lib/punchcard/punch.rb', line 33

def difference_in_minutes
  base = checked_out_at.present? ? checked_out_at.utc : Time.now.utc
  ((base - checked_in_at.utc) / 60).round
end

#minutesObject

Return database-saved minutes when already checked out, otherwise calculate based upon time now



28
29
30
# File 'lib/punchcard/punch.rb', line 28

def minutes
  checked_out_at.present? ? self["minutes"] : difference_in_minutes
end

#punch_out!Object

Punches this punch out when pending



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/punchcard/punch.rb', line 39

def punch_out!
  return false if checked_out_at.present?
  # Make sure no one stays the night...
  if self.checked_in_at.utc < Time.now.utc.beginning_of_day
    self.checked_out_at = self.checked_in_at.utc.end_of_day
  else
    self.checked_out_at = Time.now.utc
  end
  save!
  self
end

#reopen!Object



51
52
53
54
55
# File 'lib/punchcard/punch.rb', line 51

def reopen!
  self.checked_out_at = nil
  save!
  self
end