<commit_before>class VehiclesController < ApplicationController
  load_and_authorize_resource except: [:change_initial_mileage, :inactivate, :reactivate]

  def index
    @vehicles = @vehicles.default_order.for_provider(current_provider.id)
    @vehicles = @vehicles.active if params[:show_inactive] != 'true'
  end

  def show
    @readonly = true
  end

  def new
    @vehicle.provider = current_provider
  end

  def edit; end

  def update
    new_attrs = vehicle_params
    is_garage_address_blank = check_blank_garage_address
    
    if is_garage_address_blank
      prev_garage_address = @vehicle.garage_address
      @vehicle.garage_address_id = nil
      new_attrs.except!(:garage_address_attributes)
    end

    @vehicle.assign_attributes new_attrs

    if @vehicle.garage_address.present?
      @vehicle.garage_address.the_geom = Address.compute_geom(params[:lat], params[:lon])
    end

    if !@vehicle.is_all_valid?(current_provider_id)
      render action: :edit
    else
      begin      
        Vehicle.transaction do
          @vehicle.save!
          prev_garage_address.destroy if is_garage_address_blank && prev_garage_address.present?
        end
        redirect_to @vehicle, notice: 'Vehicle was successfully updated.'
      rescue ActiveRecord::RecordInvalid => e
        Rails.logger.debug e.message
        render action: :edit
      end
    end
  end

  def create
    new_attrs = vehicle_params
    is_garage_address_blank = check_blank_garage_address
    if is_garage_address_blank
      new_attrs.except!(:garage_address_attributes)
    end

    @vehicle.attributes = new_attrs

    if is_garage_address_blank
      @vehicle.garage_address = nil
    elsif @vehicle.garage_address.present?
      @vehicle.garage_address.the_geom = Address.compute_geom(params[:lat], params[:lon])
    end

    @vehicle.provider = current_provider
    if @vehicle.is_all_valid?(current_provider_id) && @vehicle.save
      redirect_to @vehicle, notice: 'Vehicle was successfully created.'
    else
      render action: :new
    end
  end

  def destroy
    @vehicle.destroy
    redirect_to vehicles_path, notice: 'Vehicle was successfully deleted.'
  end

  def edit_initial_mileage
    @vehicle = Vehicle.find_by_id(params[:id])
    authorize! :edit, @vehicle
  end

  def update_initial_mileage
    @vehicle = Vehicle.find_by_id(params[:id])
    authorize! :edit, @vehicle

    prev_mileage = @vehicle.initial_mileage
    @vehicle.assign_attributes change_initial_mileage_params

    respond_to do |format|
      format.html {
        if @vehicle.initial_mileage_change_reason.blank?
          flash.now[:error] = "Please provide a reason."
          render action: :edit_initial_mileage
        else
          @vehicle.save(validate: false)
          TrackerActionLog.change_vehicle_initial_mileage @vehicle, current_user, prev_mileage
          redirect_to @vehicle, notice: "Initial mileage has been updated."
        end
      }
    end
  end

  def inactivate
    @vehicle = Vehicle.find_by_id(params[:id])

    authorize! :update, @vehicle
    
    prev_active_text = @vehicle.active_status_text
    prev_reason = @vehicle.active_status_changed_reason

    @vehicle.assign_attributes vehicle_inactivate_params

    if @vehicle.inactivated?
      if @vehicle.permanent_inactivated?
        @vehicle.inactivated_start_date = nil
        @vehicle.inactivated_end_date = nil
      else
        if @vehicle.inactivated_end_date.present? && !@vehicle.inactivated_start_date.present?
          @vehicle.inactivated_start_date = Date.today.in_time_zone
        end
      end
    else
      @vehicle.active_status_changed_reason = nil  
    end

    if @vehicle.changed?
      TrackerActionLog.vehicle_active_status_changed(@vehicle, current_user, prev_active_text, prev_reason)
    end

    @vehicle.save(validate: false)

    redirect_to @vehicle
  end

  def reactivate
    @vehicle = Vehicle.find(params[:id])
    authorize! :edit, @vehicle

    prev_active_text = @driver.active_status_text
    prev_reason = @driver.active_status_changed_reason

    @vehicle.reactivate!
    TrackerActionLog.vehicle_active_status_changed(@vehicle, current_user, prev_active_text, prev_reason)

    redirect_to @vehicle
  end

  private

  def vehicle_params
    params.require(:vehicle).permit(
      :name, 
      :year, 
      :make, 
      :model, <commit_msg>Fix bug related to vehicle reactivation [RPII-302]<commit_after>class VehiclesController < ApplicationController
  load_and_authorize_resource except: [:change_initial_mileage, :inactivate, :reactivate]

  def index
    @vehicles = @vehicles.default_order.for_provider(current_provider.id)
    @vehicles = @vehicles.active if params[:show_inactive] != 'true'
  end

  def show
    @readonly = true
  end

  def new
    @vehicle.provider = current_provider
  end

  def edit; end

  def update
    new_attrs = vehicle_params
    is_garage_address_blank = check_blank_garage_address
    
    if is_garage_address_blank
      prev_garage_address = @vehicle.garage_address
      @vehicle.garage_address_id = nil
      new_attrs.except!(:garage_address_attributes)
    end

    @vehicle.assign_attributes new_attrs

    if @vehicle.garage_address.present?
      @vehicle.garage_address.the_geom = Address.compute_geom(params[:lat], params[:lon])
    end

    if !@vehicle.is_all_valid?(current_provider_id)
      render action: :edit
    else
      begin      
        Vehicle.transaction do
          @vehicle.save!
          prev_garage_address.destroy if is_garage_address_blank && prev_garage_address.present?
        end
        redirect_to @vehicle, notice: 'Vehicle was successfully updated.'
      rescue ActiveRecord::RecordInvalid => e
        Rails.logger.debug e.message
        render action: :edit
      end
    end
  end

  def create
    new_attrs = vehicle_params
    is_garage_address_blank = check_blank_garage_address
    if is_garage_address_blank
      new_attrs.except!(:garage_address_attributes)
    end

    @vehicle.attributes = new_attrs

    if is_garage_address_blank
      @vehicle.garage_address = nil
    elsif @vehicle.garage_address.present?
      @vehicle.garage_address.the_geom = Address.compute_geom(params[:lat], params[:lon])
    end

    @vehicle.provider = current_provider
    if @vehicle.is_all_valid?(current_provider_id) && @vehicle.save
      redirect_to @vehicle, notice: 'Vehicle was successfully created.'
    else
      render action: :new
    end
  end

  def destroy
    @vehicle.destroy
    redirect_to vehicles_path, notice: 'Vehicle was successfully deleted.'
  end

  def edit_initial_mileage
    @vehicle = Vehicle.find_by_id(params[:id])
    authorize! :edit, @vehicle
  end

  def update_initial_mileage
    @vehicle = Vehicle.find_by_id(params[:id])
    authorize! :edit, @vehicle

    prev_mileage = @vehicle.initial_mileage
    @vehicle.assign_attributes change_initial_mileage_params

    respond_to do |format|
      format.html {
        if @vehicle.initial_mileage_change_reason.blank?
          flash.now[:error] = "Please provide a reason."
          render action: :edit_initial_mileage
        else
          @vehicle.save(validate: false)
          TrackerActionLog.change_vehicle_initial_mileage @vehicle, current_user, prev_mileage
          redirect_to @vehicle, notice: "Initial mileage has been updated."
        end
      }
    end
  end

  def inactivate
    @vehicle = Vehicle.find_by_id(params[:id])

    authorize! :update, @vehicle
    
    prev_active_text = @vehicle.active_status_text
    prev_reason = @vehicle.active_status_changed_reason

    @vehicle.assign_attributes vehicle_inactivate_params

    if @vehicle.inactivated?
      if @vehicle.permanent_inactivated?
        @vehicle.inactivated_start_date = nil
        @vehicle.inactivated_end_date = nil
      else
        if @vehicle.inactivated_end_date.present? && !@vehicle.inactivated_start_date.present?
          @vehicle.inactivated_start_date = Date.today.in_time_zone
        end
      end
    else
      @vehicle.active_status_changed_reason = nil  
    end

    if @vehicle.changed?
      TrackerActionLog.vehicle_active_status_changed(@vehicle, current_user, prev_active_text, prev_reason)
    end

    @vehicle.save(validate: false)

    redirect_to @vehicle
  end

  def reactivate
    @vehicle = Vehicle.find(params[:id])
    authorize! :edit, @vehicle

    prev_active_text = @vehicle.active_status_text
    prev_reason = @vehicle.active_status_changed_reason

    @vehicle.reactivate!
    TrackerActionLog.vehicle_active_status_changed(@vehicle, current_user, prev_active_text, prev_reason)

    redirect_to @vehicle
  end

  private

  def vehicle_params
    params.require(:vehicle).permit(
      :name, 
      :year, 
      :make, 
      :model, 
{'fraction_non_alphanumeric': 0.07627408505301563, 'fraction_numerical': 0.00034203625584311936, 'mean_word_length': 3.3042198233562314, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1994920', 'n_tokens_mistral': 2842, 'n_tokens_neox': 2640, 'n_words': 526}