shakycode shakycode - 2 years ago 182
Ruby Question

Rails Scoping a payroll period

I've built a Rails app that logs employee time (clockin/clockout) and calculates total hours, allows exports to CSV/PDF, search timecards based on dates, etc.

What I'm really wanting to do is to implement payroll periods via a scope of some sort of a method.

The payroll period begins on a Sunday and ends on a Saturday 14 days later. What would be the best way to write a scope like this? Also is it possible to split the weeks into two for the payroll period?

I wrote these scopes but they are flawed:

scope :payroll_week_1, -> {
start = -
ending = -
where(clock_in: start..ending)
scope :payroll_week_2, -> {
start =
ending = + 6.days
where(clock_in: start..ending)

These works if you are currently in a payroll period, but once you pass the end of the week, the scopes no longer work because I'm basing my timing off of

Is there any way to actually do this? Even if I have to set some sort of static scope or value which says April 10 - 23 is payroll period 1, etc etc. I'm really not sure how to approach this problem and what might work. So far what I've written works in the current pay period but as time advances the scope drifts.

Any help would be greatly appreciated.

Answer Source

I think what you want is to create a scope, which can receive a start_day as a parameter:

scope :payroll_week_starting, -> (start_day) {
  where(clock_in: start_day..(start_day + 1.week))

Then, in the future you will be able to call your scope with the first day of your pay period:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download