Idea for TimeClock Need Advice
You could calculate the total and cache it. That would make for editing to be easier. For totalling, that might make it easier where the total defaults to 0 until the clock_out time is added so the sums are always correct.
I don't see any real downsides to that off the top of my head. Do you?
Well the more I think about it, editing the total is sort of a bad idea. Reason being, if you aren't editing the clock in/out fields you have a chance of skewing the total by editing it manually. Then you have all these time punches that don't jive with the total value.
I pretty much have it working the way I like it with calculating total on the fly with to_i
and then parsing in the view with my TimeFormatter class. My main issue right now is when displaying the total hours it shows that screwy negative number when calculating the sum. I need to figure out a way to hide it if there's an incomplete punch (see my pictures above) or somehow calculate all of the punches except the current punch cycle since it's not complete.
Any thoughts on that? I've made great progress with this, but now that it's functional, it's the little things that I need to get a handle on.
You can just filter them out with a scope that says where clocked_out IS NOT NULL. That should do the trick.
That's a good solution, I was thinking something along those lines but felt at first it was a bit hackish. Would still need some alert or indicator of incomplete punch cycles in the view but I think the scope really makes sense. Thanks for the tip :)
scope :incomplete, -> { where(clock_out: nil) }
scope :complete, -> { where.not(clock_out: nil) }
Note I have these two scopes to parse out incomplete and complete punches. Incomplete works but complete gives a nomethoderror
. It appears that the syntax doesn't work at least in this version of Rails. So I rewrote the scope as follows
scope :complete, -> { !where(clock_out: nil) }
So now in my controller I instantiate like so:
def index
@clock_event = current_user.clock_event
@clock_events = current_user.clock_events.complete
end
In my view I show the total hours like so:
<%= TimeFormatter.format_time(@clock_events.sum(&:total_hours)) %>
This will give a sum of all complete time punches. Works like a champ and gets rid of that screwy number in the view when the array contains an incomplete punch. :)
(Yay for small victories). Now to move on to more useful features and kill my prototype app, merge it into my main app as a feature branch.
Ok, I spoke to soon. I thought I had this all wired up and the complete
scope working. But the !where
doesn't work any longer. I had to write the scope as such:
scope :complete, -> { where("clock_out IS NOT NULL") }
Now it scopes properly. I thought I had this working this morning, I could have sworn it was but I just tested it again and it didn't work until I put in the SQL clause into the scope. Rails is not behaving today.