app/helpers/shifts_helper.rb
def day_preprocessing(day)
    @location_rows = {}

    #for AJAX; needs cleanup if we have time
    @loc_groups = current_user.user_config.view_loc_groups.select{|l| !l.locations.empty?}
    @dept_start_hour ||= current_department.department_config.schedule_start / 60
    @dept_end_hour ||= current_department.department_config.schedule_end / 60
    @hours_per_day ||= (@dept_end_hour - @dept_start_hour)
    @time_increment ||= current_department.department_config.time_increment
    @blocks_per_hour ||= 60/@time_increment.to_f

    @visible_locations ||= current_user.user_config.view_loc_groups.collect{|l| l.locations}.flatten
    #locations = @loc_groups.map{|lg| lg.locations}.flatten
    for location in @visible_locations
      @location_rows[location] = [] #initialize rows
      @location_rows[location][0] = [] #initialize rows
    end

    # @hidden_shifts = Shift.hidden_search(day.beginning_of_day + @dept_start_hour.hours + @time_increment.minutes,
    #                                      day.beginning_of_day + @dept_end_hour.hours - @time_increment.minutes,
    #                                      day.beginning_of_day, day.end_of_day, locations.map{|l| l.id})
    # shifts = Shift.super_search(day.beginning_of_day + @dept_start_hour.hours,
    #                             day.beginning_of_day + @dept_end_hour.hours, @time_increment.minutes, locations.map{|l| l.id})

    @visible_locations ||= current_user.user_config.view_loc_groups.collect{|l| l.locations}.flatten
    #adding the option to view unscheduled shifts
    if current_department.department_config.unscheduled_shifts == true
      shifts = Shift.active.in_locations(@visible_locations).on_day(day) #TODO: .active
    else
      shifts = Shift.active.in_locations(@visible_locations).on_day(day).scheduled
    end
    shifts ||= []
    shifts = shifts.sort_by{|s| [s.location_id, s.start]}

    # TODO: FIX ME
    @hidden_shifts = Shift.hidden_search(day.beginning_of_day + @dept_start_hour.hours + @time_increment.minutes,
                                         day.beginning_of_day + @dept_end_hour.hours - @time_increment.minutes,
                                         day.beginning_of_day, day.end_of_day, @visible_locations.map{|l| l.id})

    timeslots = TimeSlot.active.in_locations(@visible_locations).on_day(day).after_now #TODO: .active

    timeslots ||= {}
    timeslots = timeslots.group_by(&:location)

    timeslots.each_key do |location|
      timeslots[location] = timeslots[location].sort_by(&:start)
    end
    @location_rows_timeslots = timeslots

    rejected = []
    location_row = 0

#much of this logic goes toward having three rows in the TTO - 'rejected' just means rejected from the current line, being placed instead on a lower line. Nothing should be permanently 'rejected' in this process.
    until shifts.empty?
      shift = shifts.shift
      if !shift.end
        shift.end = Time.now
      elsif shift.end <= shift.start + current_department.department_config.time_increment.minutes
        shift.end = shift.start + current_department.department_config.time_increment.minutes
      end
      @location_rows[shift.location][location_row] = [shift]
      (0...shifts.length).each do |i|
        if shift.location == shifts.first.location
          if shift.end
            shift_end = shift.end
          else
            shift_end = Time.now
          end
          if shift_end > shifts.first.start
            rejected << shifts.shift
          else
            shift = shifts.shift
            @location_rows[shift.location][location_row] << shift
          end
        else
          shift = shifts.shift
          @location_rows[shift.location][location_row] = [shift]
        end
      end
      location_row += 1
      shifts = rejected
    end

    rowcount = 1 #starts with the bar on top
    for location in @visible_locations
      rowcount += (@location_rows[location].length > 0 ? @location_rows[location].length : 1)
    end

    @timeslot_rows = 0 #counter

    @row_height = 48 #pixels - this could be user-configurable
    @divider_height = 3 #pixels - this could be user-configurable
    @table_height = rowcount
    @table_pixels = @row_height * rowcount + rowcount+1
  end
#WILL BE CHANGED TO SHIFTS:
WILL BE CHANGED TO SHIFTS.
WILL BE CHANGED TO SHIFTS .
def day_preprocessing(day)
    @location_rows = {}
    @loc_groups = current_user.user_config.view_loc_groups.select{|l| !l.locations.empty?}
    @dept_start_hour ||= current_department.department_config.schedule_start / 60
    @dept_end_hour ||= current_department.department_config.schedule_end / 60
    @hours_per_day ||= (@dept_end_hour - @dept_start_hour)
    @time_increment ||= current_department.department_config.time_increment
    @blocks_per_hour ||= 60/@time_increment.to_f
    @visible_locations ||= current_user.user_config.view_loc_groups.collect{|l| l.locations}.flatten
    for location in @visible_locations
      @location_rows[location] = [] 
      @location_rows[location][0] = [] 
    end
    @visible_locations ||= current_user.user_config.view_loc_groups.collect{|l| l.locations}.flatten
    if current_department.department_config.unscheduled_shifts == true
      shifts = Shift.active.in_locations(@visible_locations).on_day(day) 
    else
      shifts = Shift.active.in_locations(@visible_locations).on_day(day).scheduled
    end
    shifts ||= []
    shifts = shifts.sort_by{|s| [s.location_id, s.start]}
    @hidden_shifts = Shift.hidden_search(day.beginning_of_day + @dept_start_hour.hours + @time_increment.minutes,
                                         day.beginning_of_day + @dept_end_hour.hours - @time_increment.minutes,
                                         day.beginning_of_day, day.end_of_day, @visible_locations.map{|l| l.id})
    timeslots = TimeSlot.active.in_locations(@visible_locations).on_day(day).after_now 
    timeslots ||= {}
    timeslots = timeslots.group_by(&:location)
    timeslots.each_key do |location|
      timeslots[location] = timeslots[location].sort_by(&:start)
    end
    @location_rows_timeslots = timeslots
    rejected = []
    location_row = 0
    until shifts.empty?
      shift = shifts.shift
      if !shift.end
        shift.end = Time.now
      elsif shift.end <= shift.start + current_department.department_config.time_increment.minutes
        shift.end = shift.start + current_department.department_config.time_increment.minutes
      end
      @location_rows[shift.location][location_row] = [shift]
      (0...shifts.length).each do |i|
        if shift.location == shifts.first.location
          if shift.end
            shift_end = shift.end
          else
            shift_end = Time.now
          end
          if shift_end > shifts.first.start
            rejected << shifts.shift
          else
            shift = shifts.shift
            @location_rows[shift.location][location_row] << shift
          end
        else
          shift = shifts.shift
          @location_rows[shift.location][location_row] = [shift]
        end
      end
      location_row += 1
      shifts = rejected
    end
    rowcount = 1 
    for location in @visible_locations
      rowcount += (@location_rows[location].length > 0 ? @location_rows[location].length : 1)
    end
    @timeslot_rows = 0 
    @row_height = 48 
    @divider_height = 3 
    @table_height = rowcount
    @table_pixels = @row_height * rowcount + rowcount+1
  end
def day_preprocessing ( day ) @location_rows = { } @loc_groups = current_user . user_config . view_loc_groups . select { | l | ! l . locations . empty? } @dept_start_hour ||= current_department . department_config . schedule_start / 60 @dept_end_hour ||= current_department . department_config . schedule_end / 60 @hours_per_day ||= ( @dept_end_hour - @dept_start_hour ) @time_increment ||= current_department . department_config . time_increment @blocks_per_hour ||= 60 / @time_increment . to_f @visible_locations ||= current_user . user_config . view_loc_groups . collect { | l | l . locations } . flatten for location in @visible_locations @location_rows [ location ] = [ ] @location_rows [ location ] [ 0 ] = [ ] end @visible_locations ||= current_user . user_config . view_loc_groups . collect { | l | l . locations } . flatten if current_department . department_config . unscheduled_shifts == true shifts = Shift . active . in_locations ( @visible_locations ) . on_day ( day ) else shifts = Shift . active . in_locations ( @visible_locations ) . on_day ( day ) . scheduled end shifts ||= [ ] shifts = shifts . sort_by { | s | [ s . location_id , s . start ] } @hidden_shifts = Shift . hidden_search ( day . beginning_of_day + @dept_start_hour . hours + @time_increment . minutes , day . beginning_of_day + @dept_end_hour . hours - @time_increment . minutes , day . beginning_of_day , day . end_of_day , @visible_locations . map { | l | l . id } ) timeslots = TimeSlot . active . in_locations ( @visible_locations ) . on_day ( day ) . after_now timeslots ||= { } timeslots = timeslots . group_by ( & :location ) timeslots . each_key do | location | timeslots [ location ] = timeslots [ location ] . sort_by ( & :start ) end @location_rows_timeslots = timeslots rejected = [ ] location_row = 0 until shifts . empty? shift = shifts . shift if ! shift . end shift . end = Time . now elsif shift . end <= shift . start + current_department . department_config . time_increment . minutes shift . end = shift . start + current_department . department_config . time_increment . minutes end @location_rows [ shift . location ] [ location_row ] = [ shift ] ( 0 ... shifts . length ) . each do | i | if shift . location == shifts . first . location if shift . end shift_end = shift . end else shift_end = Time . now end if shift_end > shifts . first . start rejected << shifts . shift else shift = shifts . shift @location_rows [ shift . location ] [ location_row ] << shift end else shift = shifts . shift @location_rows [ shift . location ] [ location_row ] = [ shift ] end end location_row += 1 shifts = rejected end rowcount = 1 for location in @visible_locations rowcount += ( @location_rows [ location ] . length > 0 ? @location_rows [ location ] . length : 1 ) end @timeslot_rows = 0 @row_height = 48 @divider_height = 3 @table_height = rowcount @table_pixels = @row_height * rowcount + rowcount + 1 end
WILL BE CHANGED TO SHIFTS:
WILL BE CHANGED TO SHIFTS :
#for AJAX; needs cleanup if we have time #locations = @loc_groups.map{|lg| lg.locations}.flatten #initialize rows #initialize rows # @hidden_shifts = Shift.hidden_search(day.beginning_of_day + @dept_start_hour.hours + @time_increment.minutes, #                                      day.beginning_of_day + @dept_end_hour.hours - @time_increment.minutes, #                                      day.beginning_of_day, day.end_of_day, locations.map{|l| l.id}) # shifts = Shift.super_search(day.beginning_of_day + @dept_start_hour.hours, #                             day.beginning_of_day + @dept_end_hour.hours, @time_increment.minutes, locations.map{|l| l.id}) #adding the option to view unscheduled shifts #TODO: .active # TODO: FIX ME #TODO: .active #much of this logic goes toward having three rows in the TTO - 'rejected' just means rejected from the current line, being placed instead on a lower line. Nothing should be permanently 'rejected' in this process. #starts with the bar on top #counter #pixels - this could be user-configurable #pixels - this could be user-configurable
day