<commit_before>
class Search::Food < ApplicationRecord

  ATTRIBUTES = %i(
    food_name
    category_id
    sort_id
  )
  attr_accessor(*ATTRIBUTES)

  def initialize(attr)
    @food_name = attr[:search_food_name]
    @category_id = attr[:search_category_id]
    @sort_id = attr[:sort_id]
  end

  def search
    # create outer join statement
    sJoin = "
      LEFT OUTER JOIN periods AS P1
        ON P1.food_id = foods.id
        AND P1.place_id = 1
      LEFT OUTER JOIN periods AS P2
        ON P2.food_id = foods.id
        AND P2.place_id = 2
      LEFT OUTER JOIN periods AS P3
        ON P3.food_id = foods.id
        AND P3.place_id = 3"

    # search all items
    results = ::Food.joins(sJoin).all

    # search more with conditions
    results = results.where("category_id = ?", @category_id) if @category_id != '0'
    results = results.where("name LIKE ?", "%#{@food_name}%") if not @food_name.empty?
    
    # create order statement
    if @sort_id == '1'
      sOrder = "foods.name DESC"
    elsif @sort_id == '2'
      sOrder = "P1.days DESC, foods.name ASC"
    elsif @sort_id == '3'
      sOrder = "P2.days DESC, foods.name ASC"
    elsif @sort_id == '4'
      sOrder = "P3.days DESC, foods.name ASC"
    else 
      sOrder = "foods.name ASC"
    end

    # order
    results = results.order(sOrder)
  end

end<commit_msg>modify select sql<commit_after>
class Search::Food < ApplicationRecord

  ATTRIBUTES = %i(
    food_name
    category_id
    sort_id
  )
  attr_accessor(*ATTRIBUTES)

  def initialize(attr)
    @food_name = attr[:search_food_name]
    @category_id = attr[:search_category_id]
    @sort_id = attr[:sort_id]
  end

  def search
    # create outer join statement
    sJoin = "
      LEFT OUTER JOIN periods AS P1
        ON P1.food_id = foods.id
        AND P1.place_id = 1
      LEFT OUTER JOIN periods AS P2
        ON P2.food_id = foods.id
        AND P2.place_id = 2
      LEFT OUTER JOIN periods AS P3
        ON P3.food_id = foods.id
        AND P3.place_id = 3"

    # search all items
    results = ::Food.joins(sJoin).all

    # search more with conditions
    results = results.where("category_id = ?", @category_id) if @category_id != '0'
    results = results.where("LOWER(name) LIKE ?", "%#{@food_name.downcase}%") if not @food_name.empty?
    
    # create order statement
    if @sort_id == '1'
      sOrder = "foods.name DESC"
    elsif @sort_id == '2'
      sOrder = "P1.days DESC, foods.name ASC"
    elsif @sort_id == '3'
      sOrder = "P2.days DESC, foods.name ASC"
    elsif @sort_id == '4'
      sOrder = "P3.days DESC, foods.name ASC"
    else 
      sOrder = "foods.name ASC"
    end

    # order
    results = results.order(sOrder)
  end

end
{'fraction_non_alphanumeric': 0.10738007380073801, 'fraction_numerical': 0.014760147601476014, 'mean_word_length': 2.4057788944723617, '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': '3068265', 'n_tokens_mistral': 1005, 'n_tokens_neox': 952, 'n_words': 255}