<commit_before># frozen_string_literal: true
require 'will_paginate/array'
class RentalsController < ApplicationController
  @per_page = 10

  before_action :set_rental, only: [:show, :edit, :update, :destroy, :transform, :invoice]
  before_action :set_item_types, only: [:index, :new, :create, :edit, :update, :processing]
  before_action :set_items, only: [:index, :new, :create, :edit, :update, :processing]
  before_action :set_users, only: [:index, :new, :processing, :transform, :create]
  before_action :set_incidental_types, only: [:new]
  before_action :set_financial_transactions, only: [:show, :invoice]

  # GET /rentals
  def index
    @q = Rental.all.search(params[:q])
    @rentals = @q.result(distinct: true).paginate(page: params[:page], per_page: @per_page)
    @users = User.all

    gon.reservations = Rental.to_json_reservations
  end

  # GET /rentals/1
  def show
  end

  # GET /rentals/search_users?q
  def search_users
    # prioritiezed by order queries
    query_priority = %i(email_cont spire_id_eq full_name_cont)
    @users = []

    query_priority.each do |q|
      @users += User.ransack(q => params[:user_search_query]).result
      # break if @users.present? # maybe just join all the lists
    end

    # could be a department too
    ## join users with departments
    @users += Department.ransack(name_cont: params[:user_search_query]).result.map(&:users).flatten

    @users = User.all if @users.blank? # if we couldnt find anything pass them everything

    @users = @users.distinct.paginate(page: params[:page], per_page: 8) # remove duplicates and split into pages
    render partial: 'search_users_table'
  end

  # GET /rentals/cost?end_time=time&start_time=time&item_type=1
  def cost
    start_time = params[:start_time]
    end_time = params[:end_time]
    item_type = ItemType.find(params[:item_type]) if params[:item_type]
    render json: Rental.cost(start_time, end_time, item_type)
  end

  # GET /rentals/new
  def new
    @rental = Rental.new
    @start_date = params['start_date'].try(:to_date) || Time.zone.today
    @admin_status = @current_user.has_group? Group.where(name: 'admin')
  end

  # GET /rentals/processing<commit_msg>distinct isnt a thing even though they suggest it<commit_after># frozen_string_literal: true
require 'will_paginate/array'
class RentalsController < ApplicationController
  @per_page = 10

  before_action :set_rental, only: [:show, :edit, :update, :destroy, :transform, :invoice]
  before_action :set_item_types, only: [:index, :new, :create, :edit, :update, :processing]
  before_action :set_items, only: [:index, :new, :create, :edit, :update, :processing]
  before_action :set_users, only: [:index, :new, :processing, :transform, :create]
  before_action :set_incidental_types, only: [:new]
  before_action :set_financial_transactions, only: [:show, :invoice]

  # GET /rentals
  def index
    @q = Rental.all.search(params[:q])
    @rentals = @q.result(distinct: true).paginate(page: params[:page], per_page: @per_page)
    @users = User.all

    gon.reservations = Rental.to_json_reservations
  end

  # GET /rentals/1
  def show
  end

  # GET /rentals/search_users?q
  def search_users
    # prioritiezed by order queries
    query_priority = %i(email_cont spire_id_eq full_name_cont)
    @users = []

    query_priority.each do |q|
      @users += User.ransack(q => params[:user_search_query]).result
      # break if @users.present? # maybe just join all the lists
    end

    # could be a department too
    ## join users with departments
    @users += Department.ransack(name_cont: params[:user_search_query]).result.map(&:users).flatten

    @users = User.all if @users.blank? # if we couldnt find anything pass them everything
    
    @users = @users.uniq.paginate(page: params[:page], per_page: 8) # remove duplicates and split into pages
    render partial: 'search_users_table'
  end

  # GET /rentals/cost?end_time=time&start_time=time&item_type=1
  def cost
    start_time = params[:start_time]
    end_time = params[:end_time]
    item_type = ItemType.find(params[:item_type]) if params[:item_type]
    render json: Rental.cost(start_time, end_time, item_type)
  end

  # GET /rentals/new
  def new
    @rental = Rental.new
    @start_date = params['start_date'].try(:to_date) || Time.zone.today
    @admin_status = @current_user.has_group? Group.where(name: 'admin')
  end

  # GET /rentals/processing
{'fraction_non_alphanumeric': 0.12154195011337869, 'fraction_numerical': 0.0022675736961451248, 'mean_word_length': 4.320868516284681, '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': '4922419', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1478, 'n_words': 363}