Skip to content

Instantly share code, notes, and snippets.

@azuby
Last active December 15, 2015 14:39
Show Gist options
  • Save azuby/5275645 to your computer and use it in GitHub Desktop.
Save azuby/5275645 to your computer and use it in GitHub Desktop.
def calculate_cached_attributes
# TODO OPTIMIZE
clocks.sort_by!(&:datetime)
self.started_at = clocks.first.datetime
# assign finished_at if shift is finished and check that there isn't one finish clock
if clocks.last.activity == :finish && clocks.first != clocks.last
self.finished_at = clocks.last.datetime
else
self.finished_at = nil
end
following_clock = nil
shift_duration = 0
clocks.reverse.each do |clock|
if following_clock
clock.duration = following_clock.datetime - clock.datetime
else
clock.duration = 0
end
shift_duration += clock.duration unless clock.activity == :break
following_clock = clock
end
self.duration = shift_duration
end
Employee starts "work" at "Job A" at "7am" (creates new shift and a work clock)
Employee starts "travel" from "Job A" at "8am" (adds a travel clock to shift)
Employee starts "work" at "Job B" at "9am" (adds a work clock to shift)
Employee finishes "work" at "Job B" at "3pm" (adds a finish clock to shift)
=> Shift(id: integer, job_id: integer, duration: integer, created_at: datetime, updated_at: datetime, employee_id: integer, started_at: datetime, finished_at: datetime)
=> Clock(id: integer, shift_id: integer, activity: string, datetime: datetime, status: string, created_at: datetime, updated_at: datetime, origin: string, duration: integer, job_id: integer)
=> Job(id: integer, company_id: integer, name: string, customer: string, created_at: datetime, updated_at: datetime, active: boolean)
=> Employee(id: integer, user_id: integer, company_id: integer, role_id: integer, number: integer, created_at: datetime, updated_at: datetime, active: boolean)
@zeroeth
Copy link

zeroeth commented Mar 30, 2013

WAT IS YOUR FAVORITE COLOR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment