<commit_before>class TimeSlotsController < ApplicationController
  before_filter :require_department_admin
  layout 'shifts'

  def index
    @period_start = params[:date] ? Date.parse(params[:date]).previous_sunday : Date.today.previous_sunday
    #TODO:simplify this stuff:
    @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)
    @block_length = current_department.department_config.time_increment
    @blocks_per_hour = 60/@block_length.to_f
    @blocks_per_day = @hours_per_day * @blocks_per_hour
    @hidden_timeslots = [] #for timeslots that don't show up on the view
  end

  def show
    @time_slot = TimeSlot.find(params[:id])
  end

  def new
    @time_slot = TimeSlot.new
    @period_start = params[:date] ? Date.parse(params[:date]).previous_sunday.to_date : Date.today.previous_sunday.to_date
  end

  def create
    @time_slots = []
    errors = []
    parse_date_and_time_output(params[:time_slot])
#date should ideally come from the hidden date field across the application, and it can be forced through this way;
#but the way it's written, week_start_date makes more sense to keep ~Casey
    week_start_date = (params[:date] ? Date.parse(params[:date]).previous_sunday : Date.today.previous_sunday).to_time
    for location_id in params[:location_ids]
      for day in params[:days]
        time_slot = TimeSlot.new(params[:time_slot])
        time_slot.location_id = location_id
        time_slot.start_date = week_start_date + day.to_i.days
        time_slot.end_date = week_start_date + day.to_i.days
        time_slot.calendar = @department.calendars.default unless time_slot.calendar
        if !time_slot.save
          errors << "Error saving timeslot for #{WEEK_DAYS[day.to_i]}"
        else
          @time_slots << time_slot
        end
      end
    end
    respond_to do |format|
      format.html do
        if errors.empty?
          flash[:notice] = "Successfully created timeslot(s)."
        else
          flash[:error] =  "Error: "+errors*"<br/>"
        end
        redirect_to time_slots_path
      end
      format.js do
        if errors.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)
        else
          render :update do |page|
            ajax_alert(page, "<strong>error:</strong> timeslot could not be saved<br>"+errors*"<br/>")
          end
        end
      end
    end
  end

  def edit
    @time_slot = TimeSlot.find(params[:id])
  end

  def update
    @time_slot = TimeSlot.find(params[:id])

    parse_date_and_time_output(params[:time_slot])

    if @time_slot.update_attributes(params[:time_slot])
      respond_to do |format|
        format.js
        format.html do
          flash[:notice] = "Successfully updated timeslot."
          redirect_to @time_slot
        end
      end
    else
      respond_to do |format|
        format.js do
          render :update do |page|
            error_string = ""
            @time_slot.errors.each do |attr_name, message|
              error_string += "<br><br>#{attr_name}: #{message}"
            end
            ajax_alert(page, "<strong>error:</strong> updated time slot could not be saved."+error_string, 2.5 + (@time_slot.errors.size))
          end
        end
        format.html {render :action => 'edit'}
      end
    end
  end

  def destroy
    @time_slot = TimeSlot.find(params[:id])
    @time_slot.destroy
    respond_to do |format|
      format.html {flash[:notice] = "Successfully destroyed timeslot."; redirect_to time_slots_url}<commit_msg>now time_slots input is not dependent on the loop through days and locations<commit_after>class TimeSlotsController < ApplicationController
  before_filter :require_department_admin
  layout 'shifts'

  def index
    @period_start = params[:date] ? Date.parse(params[:date]).previous_sunday : Date.today.previous_sunday
    #TODO:simplify this stuff:
    @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)
    @block_length = current_department.department_config.time_increment
    @blocks_per_hour = 60/@block_length.to_f
    @blocks_per_day = @hours_per_day * @blocks_per_hour
    @hidden_timeslots = [] #for timeslots that don't show up on the view
  end

  def show
    @time_slot = TimeSlot.find(params[:id])
  end

  def new
    @time_slot = TimeSlot.new
    @period_start = params[:date] ? Date.parse(params[:date]).previous_sunday.to_date : Date.today.previous_sunday.to_date
  end

  def create
    @time_slots = []
    errors = []
    parse_date_and_time_output(params[:time_slot])
#date should ideally come from the hidden date field across the application, and it can be forced through this way;
#but the way it's written, week_start_date makes more sense to keep ~Casey
    week_start_date = (params[:date] ? Date.parse(params[:date]).previous_sunday : Date.today.previous_sunday).to_time


    if params[:location_ids] && params[:days]
    #if there is the list of days and locations
      for location_id in params[:location_ids]
        for day in params[:days]
          time_slot = TimeSlot.new(params[:time_slot])
          time_slot.location_id = location_id
          time_slot.start_date = week_start_date + day.to_i.days
          time_slot.end_date = week_start_date + day.to_i.days
          time_slot.calendar = @department.calendars.default unless time_slot.calendar
          if !time_slot.save
            errors << "Error saving timeslot for #{WEEK_DAYS[day.to_i]}"
          else
            @time_slots << time_slot
          end
        end
      end

    else
    #it's from a simple tooltip - one location and one day
      time_slot = TimeSlot.new(params[:time_slot])
      if !time_slot.save
        errors << "Error saving timeslot"
      else
        @time_slots << time_slot
      end

    end

        respond_to do |format|
          format.html do
              if errors.empty?
                flash[:notice] = "Successfully created timeslot(s)."
              else
                flash[:error] =  "Error: "+errors*"<br/>"
              end
              redirect_to time_slots_path
          end
          format.js do
              if errors.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)
              else
                render :update do |page|
                  ajax_alert(page, "<strong>error:</strong> timeslot could not be saved<br>"+errors*"<br/>")
                end
              end
          end
        end
  end

  def edit
    @time_slot = TimeSlot.find(params[:id])
  end

  def update
    @time_slot = TimeSlot.find(params[:id])
    parse_date_and_time_output(params[:time_slot])

    if @time_slot.update_attributes(params[:time_slot])
      respond_to do |format|
        format.js
        format.html do
          flash[:notice] = "Successfully updated timeslot."
          redirect_to @time_slot
        end
      end
    else
      respond_to do |format|
        format.js do
          render :update do |page|
            error_string = ""
            @time_slot.errors.each do |attr_name, message|
              error_string += "<br><br>#{attr_name}: #{message}"
            end
            ajax_alert(page, "<strong>error:</strong> updated time slot could not be saved."+error_string, 2.5 + (@time_slot.errors.size))
          end
        end
        format.html {render :action => 'edit'}
      end
    end

  end

  def destroy
    @time_slot = TimeSlot.find(params[:id])
    @time_slot.destroy
    respond_to do |format|
      format.html {flash[:notice] = "Successfully destroyed timeslot."; redirect_to time_slots_url}
{'fraction_non_alphanumeric': 0.09344422700587084, 'fraction_numerical': 0.0029354207436399216, 'mean_word_length': 2.6537086684539766, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2875740', 'n_tokens_mistral': 2603, 'n_tokens_neox': 2481, 'n_words': 573}