Ask A Question

Notifications

You’re not receiving notifications from this thread.

Idea for TimeClock Need Advice

shakycode asked in General

Anyone have any thoughts on this before I post to the dread StackOverflow? :)

Reply

Ping pong... Anyone able to help with this before I go to Stack? Chris? :)

Reply

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?

Reply

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.

Reply

You can just filter them out with a scope that says where clocked_out IS NOT NULL. That should do the trick.

Reply

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 :)

Reply
  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.

Reply

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.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,464+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.