Last active
December 15, 2015 14:39
-
-
Save azuby/5275645 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=> 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WAT IS YOUR FAVORITE COLOR