<commit_before># a note on general run workflow:
# Runs are created as part of the trip scheduling 
# process; they're associated with a vehicle and
# a driver.  At the end of the day, the driver
# must update the run with post-run data like
# odometer start/end and no-shows.  That is 
# presented by my_runs and end_of_day

class RunsController < ApplicationController
  load_and_authorize_resource
  before_filter :filter_runs, :only => :index

  def index
    @runs = @runs.for_provider(current_provider_id)
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @trips }
      format.js {
        rows = @runs.present? ? @runs.map do |r|
          render_to_string :partial => "row.html", :locals => { :run => r }
        end : [render_to_string( :partial => "no_runs.html" )]

        render :json => { :rows => rows }
      }
    end
  end

  def new
    @run = Run.new
    @run.provider_id = current_provider_id
    @drivers = Driver.where(:provider_id=>@run.provider_id)
    @vehicles = Vehicle.active.where(:provider_id=>@run.provider_id)
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @trip }
    end
  end

  def uncompleted_runs
    @runs = Run.for_provider(current_provider_id).where("complete = false").order("date desc")
    render "index"
  end

  def edit
    @drivers      = Driver.where(:provider_id=>@run.provider_id)
    @vehicles     = Vehicle.active.where(:provider_id=>@run.provider_id)
    @trip_results = TRIP_RESULT_CODES.map { |k,v| [v,k] }
  end

  def create
    run_params = params[:run]
    authorize! :manage, current_provider
    run_params[:provider_id] = current_provider_id

    @run = Run.new(run_params)

    respond_to do |format|
      if @run.save
        format.html { redirect_to(runs_path, :notice => 'Run was successfully created.') }
        format.xml  { render :xml => @run, :status => :created, :location => @run }
      else
        @drivers = Driver.where(:provider_id=>@run.provider_id)
        @vehicles = Vehicle.active.where(:provider_id=>@run.provider_id)

        format.html { render :action => "new" }
        format.xml  { render :xml => @run.errors, :status => :unprocessable_entity }
      end
    end
  end

  def update
    run_params = params[:run]
    authorize! :manage, current_provider
    run_params[:provider_id] = current_provider_id

    respond_to do |format|
      if @run.update_attributes(run_params)
        format.html { redirect_to(runs_path, :notice => 'Run was successfully updated.') }
        format.xml  { head :ok }
      else
        @drivers = Driver.where(:provider_id=>@run.provider_id)
        @vehicles = Vehicle.active.where(:provider_id=>@run.provider_id)
        @trip_results = TRIP_RESULT_CODES.map { |k,v| [v,k] }
        format.html { render :action => "edit" }
        format.xml  { render :xml => @run.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @run.destroy

    respond_to do |format|
      format.html { redirect_to(runs_path, :notice => 'Run was successfully deleted.') }
      format.xml  { head :ok }
    end
  end
  
  def for_date
    date = Date.parse params[:date]
    @runs = @runs.for_provider(current_provider_id).incomplete_on date
    render :json =>  @runs.to_json 
  end
  
  private
  
  def filter_runs
    if params[:end].present? && params[:start].present?
      @week_start = Time.at params[:start].to_i
      @week_end   = Time.at params[:end].to_i
    else
      time     = Time.now
      @week_start = time.beginning_of_week
      @week_end   = @week_start + 6.days
    end
    
    @runs = @runs.
      where("date >= '#{@week_start.to_s(:db)}'").
      where("date < '#{@week_end.to_s(:db)}'")
  end
end<commit_msg>Redirect to the correct week on runs index page when updating or deleting a run<commit_after># a note on general run workflow:
# Runs are created as part of the trip scheduling 
# process; they're associated with a vehicle and
# a driver.  At the end of the day, the driver
# must update the run with post-run data like
# odometer start/end and no-shows.  That is 
# presented by my_runs and end_of_day

class RunsController < ApplicationController
  load_and_authorize_resource
  before_filter :filter_runs, :only => :index

  def index
    @runs = @runs.for_provider(current_provider_id)
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @trips }
      format.js {
        rows = @runs.present? ? @runs.map do |r|
          render_to_string :partial => "row.html", :locals => { :run => r }
        end : [render_to_string( :partial => "no_runs.html" )]

        render :json => { :rows => rows }
      }
    end
  end

  def new
    @run = Run.new
    @run.provider_id = current_provider_id
    @drivers = Driver.where(:provider_id=>@run.provider_id)
    @vehicles = Vehicle.active.where(:provider_id=>@run.provider_id)
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @trip }
    end
  end

  def uncompleted_runs
    @runs = Run.for_provider(current_provider_id).where("complete = false").order("date desc")
    render "index"
  end

  def edit
    @drivers      = Driver.where(:provider_id=>@run.provider_id)
    @vehicles     = Vehicle.active.where(:provider_id=>@run.provider_id)
    @trip_results = TRIP_RESULT_CODES.map { |k,v| [v,k] }
  end

  def create
    run_params = params[:run]
    authorize! :manage, current_provider
    run_params[:provider_id] = current_provider_id

    @run = Run.new(run_params)

    respond_to do |format|
      if @run.save
        format.html { redirect_to(runs_path, :notice => 'Run was successfully created.') }
        format.xml  { render :xml => @run, :status => :created, :location => @run }
      else
        @drivers = Driver.where(:provider_id=>@run.provider_id)
        @vehicles = Vehicle.active.where(:provider_id=>@run.provider_id)

        format.html { render :action => "new" }
        format.xml  { render :xml => @run.errors, :status => :unprocessable_entity }
      end
    end
  end

  def update
    run_params = params[:run]
    authorize! :manage, current_provider
    run_params[:provider_id] = current_provider_id

    respond_to do |format|
      if @run.update_attributes(run_params)
        format.html { redirect_to(runs_path(date_range(@run)), :notice => 'Run was successfully updated.') }
        format.xml  { head :ok }
      else
        @drivers = Driver.where(:provider_id=>@run.provider_id)
        @vehicles = Vehicle.active.where(:provider_id=>@run.provider_id)
        @trip_results = TRIP_RESULT_CODES.map { |k,v| [v,k] }
        format.html { render :action => "edit" }
        format.xml  { render :xml => @run.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @run.destroy

    respond_to do |format|
      format.html { redirect_to(runs_path(date_range(@run)), :notice => 'Run was successfully deleted.') }
      format.xml  { head :ok }
    end
  end
  
  def for_date
    date = Date.parse params[:date]
    @runs = @runs.for_provider(current_provider_id).incomplete_on date
    render :json =>  @runs.to_json 
  end
  
  private
  
  def filter_runs
    if params[:end].present? && params[:start].present?
      @week_start = Time.at params[:start].to_i
      @week_end   = Time.at params[:end].to_i
    else
      time     = Time.now
      @week_start = time.beginning_of_week
      @week_end   = @week_start + 6.days
    end
    
    @runs = @runs.
      where("date >= '#{@week_start.to_s(:db)}'").
      where("date < '#{@week_end.to_s(:db)}'")
  end

  def date_range(run)
    if run.date
      week_start = run.date.beginning_of_week
      {:start => week_start.to_time.to_i, :end => (week_start + 6.days).to_time.to_i } 
    end    
  end
end
{'fraction_non_alphanumeric': 0.12323180833439532, 'fraction_numerical': 0.00038231171148209506, 'mean_word_length': 2.8793870489372217, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 24}, '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': '7228971', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2632, 'n_words': 606}