<commit_before>require_relative '../../lib/southwest/errors'

class ReservationsController < ApplicationController
  before_action :authenticate_or_redirect_to_signup!, only: :new
  before_action :authenticate_user!, except: :new
  before_action :current_user_only!
  before_action :set_user, only: [:index, :create]
  before_action :set_reservation, only: [:show, :edit, :update, :destroy]
  rescue_from Southwest::RequestError, with: :southwest_request_error, only: :create

  respond_to :html

  def index
    @reservations = @user.reservations.all
    respond_with(@reservations)
  end

  def show
    respond_with(@reservation)
  end

  def new
    @reservation = Reservation.new
    respond_with(@reservation)
  end

  def edit
  end

  def create
    @reservation = @user.reservations.new(reservation_params)
    @reservation.save
    flash[:notice] = 'Your reservation has been added! You will receive an email when the passengers on this reservation have been successfully checked in.'
    respond_with(@reservation)
  end

  def update
    @reservation.update(reservation_params)
    respond_with(@reservation)
  end

  def destroy
    @reservation.destroy
    respond_with(@reservation)
  end

  private

  def authenticate_or_redirect_to_signup!
    if user_signed_in?
      return true
    else
      flash[:notice] = 'Start by creating an account to track your reservations.'
      flash[:from_reservations] = true
      redirect_to new_registration_path(User.new)
    end
  end

  def southwest_request_error
    flash[:notice] = 'Sorry! There was an error communicating with Southwest. This has been reported. Please try to add your flight later.'
    respond_with(@reservation)
  end

  def set_user
    @user = params[:user_id] ? User.find(params[:user_id]) : current_user
  end

  def set_reservation
    @reservation = Reservation.includes({ flights: [{ passenger_checkins: [:passenger] }, :departure_airport, :arrival_airport] }, :passengers).find(params[:id])
  end

  def reservation_params
    params.require(:reservation).permit(:confirmation_number, :first_name, :last_name)
  end

  def current_user_only!
    super(params[:user_id])
  end
end
<commit_msg>only add flash message if reservation is valid<commit_after>require_relative '../../lib/southwest/errors'

class ReservationsController < ApplicationController
  before_action :authenticate_or_redirect_to_signup!, only: :new
  before_action :authenticate_user!, except: :new
  before_action :current_user_only!
  before_action :set_user, only: [:index, :create]
  before_action :set_reservation, only: [:show, :edit, :update, :destroy]
  rescue_from Southwest::RequestError, with: :southwest_request_error, only: :create

  respond_to :html

  def index
    @reservations = @user.reservations.all
    respond_with(@reservations)
  end

  def show
    respond_with(@reservation)
  end

  def new
    @reservation = Reservation.new
    respond_with(@reservation)
  end

  def edit
  end

  def create
    @reservation = @user.reservations.new(reservation_params)
    @reservation.save
    if @reservation.valid?
      flash[:notice] = 'Your reservation has been added! You will receive an email when the passengers on this reservation have been successfully checked in.'
    end
    respond_with(@reservation)
  end

  def update
    @reservation.update(reservation_params)
    respond_with(@reservation)
  end

  def destroy
    @reservation.destroy
    respond_with(@reservation)
  end

  private

  def authenticate_or_redirect_to_signup!
    if user_signed_in?
      return true
    else
      flash[:notice] = 'Start by creating an account to track your reservations.'
      flash[:from_reservations] = true
      redirect_to new_registration_path(User.new)
    end
  end

  def southwest_request_error
    flash[:notice] = 'Sorry! There was an error communicating with Southwest. This has been reported. Please try to add your flight later.'
    respond_with(@reservation)
  end

  def set_user
    @user = params[:user_id] ? User.find(params[:user_id]) : current_user
  end

  def set_reservation
    @reservation = Reservation.includes({ flights: [{ passenger_checkins: [:passenger] }, :departure_airport, :arrival_airport] }, :passengers).find(params[:id])
  end

  def reservation_params
    params.require(:reservation).permit(:confirmation_number, :first_name, :last_name)
  end

  def current_user_only!
    super(params[:user_id])
  end
end

{'fraction_non_alphanumeric': 0.08724075743913436, 'fraction_numerical': 0.0, 'mean_word_length': 4.294749403341289, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5096234', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1331, 'n_words': 350}