<commit_before>module Typus
  module Orm
    module ClassMethods
      module Search

        def build_search_conditions(key, value, conditions)
          query = ActiveRecord::Base.connection.quote_string(value.downcase)

          condition = Array.new.tap do |search|
                        typus_search_fields.each do |key, value|
                          _query = case value
                                   when "=" then query
                                   when "^" then "#{query}%"
                                   when "@" then "%#{query}%"
                                   end
                          table_key = (adapter == 'postgresql') ? "LOWER(TEXT(#{table_name}.#{key}))" : "`#{table_name}`.#{key}"
                          search << "#{table_key} LIKE '#{_query}'"
                        end
                      end.join(" OR ")

          merge_conditions(conditions, condition)
        end

        def build_boolean_conditions(key, value, conditions)
          condition = { key => (value == 'true') ? true : false }
          merge_conditions(conditions, condition)
        end

        def build_datetime_conditions(key, value, conditions)
          tomorrow = Time.zone.now.beginning_of_day.tomorrow

          interval = case value
                     when 'today'         then 0.days.ago.beginning_of_day..tomorrow
                     when 'last_few_days' then 3.days.ago.beginning_of_day..tomorrow
                     when 'last_7_days'   then 6.days.ago.beginning_of_day..tomorrow
                     when 'last_30_days'  then 30.days.ago.beginning_of_day..tomorrow
                     end

          condition = ["`#{table_name}`.#{key} BETWEEN ? AND ?", interval.first.to_s(:db), interval.last.to_s(:db)]

          merge_conditions(conditions, condition)
        end

        def build_has_and_belongs_to_many_conditions(key, value, conditions)
          condition = { key => { :id => value } }
          conditions = merge_conditions(conditions, condition)
          joins << key.to_sym
        end

        def build_date_conditions(key, value, conditions)
          field = "`#{table_name}`.#{key}"

          interval = case value
                     when 'today'         then Date.today..Date.today
                     when 'last_few_days' then 3.days.ago.to_date..Date.tomorrow
                     when 'last_7_days'   then 6.days.ago.to_date..Date.tomorrow
                     when 'last_30_days'  then 30.days.ago.to_date..Date.tomorrow
                     end

          condition = ["#{field} BETWEEN ? AND ?", interval.first, interval.last]

          merge_conditions(conditions, condition)
        end

        def build_string_conditions(key, value, conditions)
          condition = { key => value }
          merge_conditions(conditions, condition)
        end<commit_msg>Some refactor ...<commit_after>module Typus
  module Orm
    module ClassMethods
      module Search

        def build_search_conditions(key, value, conditions)
          query = ActiveRecord::Base.connection.quote_string(value.downcase)

          condition = Array.new.tap do |search|
                        typus_search_fields.each do |key, value|
                          _query = case value
                                   when "=" then query
                                   when "^" then "#{query}%"
                                   when "@" then "%#{query}%"
                                   end
                          table_key = (adapter == 'postgresql') ? "LOWER(TEXT(#{table_name}.#{key}))" : "`#{table_name}`.#{key}"
                          search << "#{table_key} LIKE '#{_query}'"
                        end
                      end.join(" OR ")

          merge_conditions(conditions, condition)
        end

        def build_boolean_conditions(key, value, conditions)
          condition = { key => (value == 'true') ? true : false }
          merge_conditions(conditions, condition)
        end

        def build_datetime_conditions(key, value, conditions)
          tomorrow = Time.zone.now.beginning_of_day.tomorrow

          interval = case value
                     when 'today'         then 0.days.ago.beginning_of_day..tomorrow
                     when 'last_few_days' then 3.days.ago.beginning_of_day..tomorrow
                     when 'last_7_days'   then 6.days.ago.beginning_of_day..tomorrow
                     when 'last_30_days'  then 30.days.ago.beginning_of_day..tomorrow
                     end

          condition = ["`#{table_name}`.#{key} BETWEEN ? AND ?", interval.first.to_s(:db), interval.last.to_s(:db)]

          merge_conditions(conditions, condition)
        end

        def build_has_and_belongs_to_many_conditions(key, value, conditions)
          condition = { key => { :id => value } }
          conditions = merge_conditions(conditions, condition)
          joins << key.to_sym
        end

        def build_date_conditions(key, value, conditions)
          tomorrow = Date.tomorrow

          interval = case value
                     when 'today'         then Date.today..Date.today
                     when 'last_few_days' then 3.days.ago.to_date..tomorrow
                     when 'last_7_days'   then 6.days.ago.to_date..tomorrow
                     when 'last_30_days'  then 30.days.ago.to_date..tomorrow
                     end

          condition = ["`#{table_name}`.#{key} BETWEEN ? AND ?", interval.first, interval.last]

          merge_conditions(conditions, condition)
        end

        def build_string_conditions(key, value, conditions)
          condition = { key => value }
          merge_conditions(conditions, condition)
        end
{'fraction_non_alphanumeric': 0.09987691225602251, 'fraction_numerical': 0.005275189027606823, 'mean_word_length': 1.6187845303867403, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4025479', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1478, 'n_words': 315}