<commit_before># -*- encoding: utf-8 -*-
class ReservesController < ApplicationController
  before_filter :store_location, :only => [:index, :new]
  load_and_authorize_resource :except => :index
  authorize_resource :only => :index
  before_filter :get_user, :only => [:index, :new]
  before_filter :store_page
  helper_method :get_manifestation
  helper_method :get_item
  after_filter :convert_charset, :only => :index

  # GET /reserves
  # GET /reserves.json
  def index
    unless current_user.has_role?('Librarian')
      if @user
        if current_user == @user
          redirect_to reserves_url(:format => params[:format])
          return
        else
          access_denied; return
        end
      end
    end

    search = Reserve.search
    query = @query = params[:query].to_s.strip
    if params[:format].to_s.downcase == 'csv'
      page = 1
      per_page = 65534
    else
      page ||= params[:page] || 1
      per_page ||= Reserve.default_per_page
    end

    if params[:mode] == 'hold' and current_user.has_role?('Librarian')
      search.build do
        with(:hold).equal_to true
      end
    else
      if @user
        user = @user
        if current_user.has_role?('Librarian')
          search.build do
            with(:username).equal_to user.username
          end
        else
          access_denied; return
        end
      else
        unless current_user.has_role?('Librarian')
          search.build do
            with(:username).equal_to current_user.username
          end
        end
      end
    end

    begin
      reserved_from = Time.zone.parse(params[:reserved_from])
      @reserved_from = params[:reserved_from].to_s.strip
    rescue
      reserved_from = nil
    end

    begin
      reserved_to = Time.zone.parse(params[:reserved_to])
      @reserved_to = params[:reserved_to].to_s.strip
    rescue
      reserved_to = nil
    end

    search.build do
      fulltext query
      if reserved_from
        with(:created_at).greater_than_or_equal_to reserved_from.beginning_of_day
      end
      if reserved_to
        with(:created_at).less_than reserved_to.tomorrow.beginning_of_day
      end
      order_by :created_at, :desc
      facet :state
      paginate :page => page.to_i, :per_page => per_page
    end

    @reserves = search.execute.results
    @reserves_facet = search.facet(:state).rows

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @reserves }
      format.rss  { render :layout => false }
      format.atom
      format.csv
    end
  end

  # GET /reserves/1
  # GET /reserves/1.json
  def show
    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @reserve }
    end
  end

  # GET /reserves/new
  # GET /reserves/new.json
  def new
    @reserve = Reserve.new(params[:reserve])

    if current_user.has_role?('Librarian')
      @reserve.user = @user
    else
      if @user.present?
        if @user != current_user
          access_denied; return
        end
      end
      @reserve.user_number = current_user.user_number
    end

    get_manifestation
    if @manifestation
      @reserve.manifestation = @manifestation
      if @reserve.user
        @reserve.expired_at = @manifestation.reservation_expired_period(@reserve.user).days.from_now.end_of_day
        if @manifestation.is_reserved_by?(@reserve.user)
          flash[:notice] = t('reserve.this_manifestation_is_already_reserved')
          redirect_to @manifestation
          return
        end
      end
    end
  end

  # GET /reserves/1/edit
  def edit
    @reserve.item_identifier = @reserve.item.try(:item_identifier)
  end

  # POST /reserves
  # POST /reserves.json
  def create
    @reserve = Reserve.new(params[:reserve])
    @reserve.user = User.where(:user_number => @reserve.user_number).first if @reserve.user_number.to_s.strip.present?

    if current_user.has_role?('Librarian')
      unless @reserve.user
        @reserve.user = @user
      end
    else
      if @reserve.user != current_user
        if @user != current_user
          access_denied; return
        end
      end
    end

    respond_to do |format|
      if @reserve.save
        @reserve.sm_request!
        @reserve.send_message

        format.html { redirect_to @reserve, :notice => t('controller.successfully_created', :model => t('activerecord.models.reserve')) }
        format.json { render :json => @reserve, :status => :created, :location => reserve_url(@reserve) }
      else
        format.html { render :action => "new" }
        format.json { render :json => @reserve.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /reserves/1
  # PUT /reserves/1.json
  def update
    if current_user.has_role?('Librarian')
      @reserve.assign_attributes(params[:reserve], :as => :admin)
    else
      @reserve.assign_attributes(params[:reserve])
    end
    @reserve.user = User.where(:user_number => @reserve.user_number).first if @reserve.user_number.to_s.strip.present?

    unless current_user.has_role?('Librarian')
      if @reserve.user != current_user
        access_denied; return
      end
    end

    if @reserve.valid?
      if params[:mode] == 'cancel'
        @reserve.sm_cancel!
      else
        unless @reserve.retained?
          @reserve.sm_retain! if @reserve.item
        end
      end
    end

    respond_to do |format|
      if @reserve.save
        if @reserve.state == 'canceled'
          flash[:notice] = t('reserve.reservation_was_canceled')
        else
          flash[:notice] = t('controller.successfully_updated', :model => t('activerecord.models.reserve'))
        end
        format.html { redirect_to @reserve }
        format.json { head :no_content }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @reserve.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /reserves/1
  # DELETE /reserves/1.json
  def destroy
    @reserve.destroy
    #flash[:notice] = t('reserve.reservation_was_canceled')

    if @reserve.manifestation.is_reserved?
      if @reserve.item
        retain = @reserve.item.retain(User.find(1)) # TODO: システムからの送信ユーザの設定
        if retain.nil?
          flash[:message] = t('reserve.this_item_is_not_reserved')
        end
      end
    end

    respond_to do |format|
      format.html { redirect_to reserves_url, :notice => t('controller.successfully_deleted', :model => t('activerecord.models.reserve')) }
      format.json { head :no_content }
    end
  end
end
<commit_msg>fixed variable name<commit_after># -*- encoding: utf-8 -*-
class ReservesController < ApplicationController
  before_filter :store_location, :only => [:index, :new]
  load_and_authorize_resource :except => :index
  authorize_resource :only => :index
  before_filter :get_user, :only => [:index, :new]
  before_filter :store_page
  helper_method :get_manifestation
  helper_method :get_item
  after_filter :convert_charset, :only => :index

  # GET /reserves
  # GET /reserves.json
  def index
    unless current_user.has_role?('Librarian')
      if @user
        if current_user == @user
          redirect_to reserves_url(:format => params[:format])
          return
        else
          access_denied; return
        end
      end
    end

    search = Reserve.search
    query = @query = params[:query].to_s.strip
    if params[:format].to_s.downcase == 'csv'
      page = 1
      per_page = 65534
    else
      page ||= params[:page] || 1
      per_page ||= Reserve.default_per_page
    end

    if params[:mode] == 'hold' and current_user.has_role?('Librarian')
      search.build do
        with(:hold).equal_to true
      end
    else
      if @user
        user = @user
        if current_user.has_role?('Librarian')
          search.build do
            with(:username).equal_to user.username
          end
        else
          access_denied; return
        end
      else
        unless current_user.has_role?('Librarian')
          search.build do
            with(:username).equal_to current_user.username
          end
        end
      end
    end

    begin
      reserved_from = Time.zone.parse(params[:reserved_from])
      @reserved_from = params[:reserved_from].to_s.strip
    rescue
      reserved_from = nil
    end

    begin
      reserved_to = Time.zone.parse(params[:reserved_to])
      @reserved_to = params[:reserved_to].to_s.strip
    rescue
      reserved_to = nil
    end

    search.build do
      fulltext query
      if reserved_from
        with(:created_at).greater_than_or_equal_to reserved_from.beginning_of_day
      end
      if reserved_to
        with(:created_at).less_than reserved_to.tomorrow.beginning_of_day
      end
      order_by :created_at, :desc
      facet :state
      paginate :page => page.to_i, :per_page => per_page
    end

    @reserves = search.execute.results
    @state_facet = search.facet(:state).rows

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @reserves }
      format.rss  { render :layout => false }
      format.atom
      format.csv
    end
  end

  # GET /reserves/1
  # GET /reserves/1.json
  def show
    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @reserve }
    end
  end

  # GET /reserves/new
  # GET /reserves/new.json
  def new
    @reserve = Reserve.new(params[:reserve])

    if current_user.has_role?('Librarian')
      @reserve.user = @user
    else
      if @user.present?
        if @user != current_user
          access_denied; return
        end
      end
      @reserve.user_number = current_user.user_number
    end

    get_manifestation
    if @manifestation
      @reserve.manifestation = @manifestation
      if @reserve.user
        @reserve.expired_at = @manifestation.reservation_expired_period(@reserve.user).days.from_now.end_of_day
        if @manifestation.is_reserved_by?(@reserve.user)
          flash[:notice] = t('reserve.this_manifestation_is_already_reserved')
          redirect_to @manifestation
          return
        end
      end
    end
  end

  # GET /reserves/1/edit
  def edit
    @reserve.item_identifier = @reserve.item.try(:item_identifier)
  end

  # POST /reserves
  # POST /reserves.json
  def create
    @reserve = Reserve.new(params[:reserve])
    @reserve.user = User.where(:user_number => @reserve.user_number).first if @reserve.user_number.to_s.strip.present?

    if current_user.has_role?('Librarian')
      unless @reserve.user
        @reserve.user = @user
      end
    else
      if @reserve.user != current_user
        if @user != current_user
          access_denied; return
        end
      end
    end

    respond_to do |format|
      if @reserve.save
        @reserve.sm_request!
        @reserve.send_message

        format.html { redirect_to @reserve, :notice => t('controller.successfully_created', :model => t('activerecord.models.reserve')) }
        format.json { render :json => @reserve, :status => :created, :location => reserve_url(@reserve) }
      else
        format.html { render :action => "new" }
        format.json { render :json => @reserve.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /reserves/1
  # PUT /reserves/1.json
  def update
    if current_user.has_role?('Librarian')
      @reserve.assign_attributes(params[:reserve], :as => :admin)
    else
      @reserve.assign_attributes(params[:reserve])
    end
    @reserve.user = User.where(:user_number => @reserve.user_number).first if @reserve.user_number.to_s.strip.present?

    unless current_user.has_role?('Librarian')
      if @reserve.user != current_user
        access_denied; return
      end
    end

    if @reserve.valid?
      if params[:mode] == 'cancel'
        @reserve.sm_cancel!
      else
        unless @reserve.retained?
          @reserve.sm_retain! if @reserve.item
        end
      end
    end

    respond_to do |format|
      if @reserve.save
        if @reserve.state == 'canceled'
          flash[:notice] = t('reserve.reservation_was_canceled')
        else
          flash[:notice] = t('controller.successfully_updated', :model => t('activerecord.models.reserve'))
        end
        format.html { redirect_to @reserve }
        format.json { head :no_content }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @reserve.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /reserves/1
  # DELETE /reserves/1.json
  def destroy
    @reserve.destroy
    #flash[:notice] = t('reserve.reservation_was_canceled')

    if @reserve.manifestation.is_reserved?
      if @reserve.item
        retain = @reserve.item.retain(User.find(1)) # TODO: システムからの送信ユーザの設定
        if retain.nil?
          flash[:message] = t('reserve.this_item_is_not_reserved')
        end
      end
    end

    respond_to do |format|
      format.html { redirect_to reserves_url, :notice => t('controller.successfully_deleted', :model => t('activerecord.models.reserve')) }
      format.json { head :no_content }
    end
  end
end

{'fraction_non_alphanumeric': 0.1056, 'fraction_numerical': 0.002438095238095238, 'mean_word_length': 2.4424337791765014, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, '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': '6255640', 'n_tokens_mistral': 4276, 'n_tokens_neox': 4173, 'n_words': 864}