<commit_before>class ReservationsController < ApplicationController
  before_filter :require_login, :only => [:index, :show]
  before_filter :require_checkout_person, :only => [:check_out, :check_in]

  def index
    if current_user.can_checkout?
      if params[:show_returned]
        @reservations_set = [Reservation.overdue, Reservation.checked_out, Reservation.pending, Reservation.returned ].delete_if{|a| a.empty?} #remove empty arrays from set
      else
        @reservations_set = [Reservation.overdue, Reservation.checked_out, Reservation.pending ].delete_if{|a| a.empty?}
      end
    else
      @reservations_set = [current_user.reservations.overdue, current_user.reservations.checked_out, current_user.reservations.pending ].delete_if{|a| a.empty?}
    end
  end

  def show
    @reservation = Reservation.find(params[:id])
  end

  def show_all #Action called in _reservations_list partial view, allows checkout person to view all current reservations for one user
    @user = User.find(params[:user_id])
    @user_reservations_set = Reservation.active_user_reservations(@user)
  end

  def new
    if cart.items.empty?
      flash[:error] = "You need to add items to your cart before making a reservation."
      redirect_to catalog_path
    else
      #this is used to initialize each reservation later
      @reservation = Reservation.new(start_date: cart.start_date, due_date: cart.due_date)
    end
  end

# old method that does not split reservations by item, needs to be deleted by end of summer 12
  # def create
  #   @reservation = Reservation.new(params[:reservation])
  #   cart.items.each do |item|
  #     @reservation.equipment_models_reservations << EquipmentModelsReservation.new(:equipment_model_id => item.equipment_model.id, :quantity => item.quantity)
  #   end
  #   if @reservation.save
  #     flash[:notice] = "Successfully created reservation."
  #     session[:cart] = Cart.new
  #     redirect_to @reservation
  #   else
  #     render :action => 'new'
  #   end
  # end

  def create
    #using http://stackoverflow.com/questions/7233859/ruby-on-rails-updating-multiple-models-from-the-one-controller as inspiration
    respond_to do |format|
      Reservation.transaction do
        begin
          cart.items.each do |item|
            emodel = item.equipment_model
            item.quantity.times do |q|    # accounts for reserving multiple equipment objects of the same equipment model (mainly for admins)
              @reservation = Reservation.new(params[:reservation])
              @reservation.equipment_model =  emodel
            end
          end
          UserMailer.reservation_confirmation(@reservation).deliver
          session[:cart] = Cart.new
          format.html {redirect_to catalog_path, :flash => {:notice => "Reservation created" } }
        rescue
          format.html {redirect_to catalog_path, :flash => {:error => "Oops, something went wrong with making your reservation"} }
          raise ActiveRecord::Rollback
        end
      end
    end
  end


  def edit
    @reservation = Reservation.find(params[:id])
  end

  def update

    error_msgs = ""<commit_msg>removed sending reservations confirmation email temporarily<commit_after>class ReservationsController < ApplicationController
  before_filter :require_login, :only => [:index, :show]
  before_filter :require_checkout_person, :only => [:check_out, :check_in]

  def index
    if current_user.can_checkout?
      if params[:show_returned]
        @reservations_set = [Reservation.overdue, Reservation.checked_out, Reservation.pending, Reservation.returned ].delete_if{|a| a.empty?} #remove empty arrays from set
      else
        @reservations_set = [Reservation.overdue, Reservation.checked_out, Reservation.pending ].delete_if{|a| a.empty?}
      end
    else
      @reservations_set = [current_user.reservations.overdue, current_user.reservations.checked_out, current_user.reservations.pending ].delete_if{|a| a.empty?}
    end
  end

  def show
    @reservation = Reservation.find(params[:id])
  end

  def show_all #Action called in _reservations_list partial view, allows checkout person to view all current reservations for one user
    @user = User.find(params[:user_id])
    @user_reservations_set = Reservation.active_user_reservations(@user)
  end

  def new
    if cart.items.empty?
      flash[:error] = "You need to add items to your cart before making a reservation."
      redirect_to catalog_path
    else
      #this is used to initialize each reservation later
      @reservation = Reservation.new(start_date: cart.start_date, due_date: cart.due_date)
    end
  end

# old method that does not split reservations by item, needs to be deleted by end of summer 12
  # def create
  #   @reservation = Reservation.new(params[:reservation])
  #   cart.items.each do |item|
  #     @reservation.equipment_models_reservations << EquipmentModelsReservation.new(:equipment_model_id => item.equipment_model.id, :quantity => item.quantity)
  #   end
  #   if @reservation.save
  #     flash[:notice] = "Successfully created reservation."
  #     session[:cart] = Cart.new
  #     redirect_to @reservation
  #   else
  #     render :action => 'new'
  #   end
  # end

  def create
    #using http://stackoverflow.com/questions/7233859/ruby-on-rails-updating-multiple-models-from-the-one-controller as inspiration
    respond_to do |format|
      Reservation.transaction do
        begin
          cart.items.each do |item|
            emodel = item.equipment_model
            item.quantity.times do |q|    # accounts for reserving multiple equipment objects of the same equipment model (mainly for admins)
              @reservation = Reservation.new(params[:reservation])
              @reservation.equipment_model =  emodel
            end
          end
          #We do want an email to be sent, but this needs to be tweaked - it currently doesn't work ~Casey
          #UserMailer.reservation_confirmation(@reservation).deliver
          session[:cart] = Cart.new
          format.html {redirect_to catalog_path, :flash => {:notice => "Reservation created" } }
        rescue
          format.html {redirect_to catalog_path, :flash => {:error => "Oops, something went wrong with making your reservation"} }
          raise ActiveRecord::Rollback
        end
      end
    end
  end


  def edit
    @reservation = Reservation.find(params[:id])
  end

  def update

    error_msgs = ""
{'fraction_non_alphanumeric': 0.09042883778744562, 'fraction_numerical': 0.0027967681789931634, 'mean_word_length': 3.220983606557377, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '2967305', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1867, 'n_words': 498}