Sha256: bebd1b96ed2ff2b951751cfed651f69a3bb345e40e224097757919178c4cf2d6

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

module Searchgasm
  module Condition
    class Keywords < Base
      # Because be default it joins with AND, so padding an array just gives you more options. Joining with and is no different than combining all of the words.
      self.join_arrays_with_or = true
      
      BLACKLISTED_WORDS = ('a'..'z').to_a + ["about", "an", "are", "as", "at", "be", "by", "com", "de", "en", "for", "from", "how", "in", "is", "it", "la", "of", "on", "or", "that", "the", "the", "this", "to", "und", "was", "what", "when", "where", "who", "will", "with", "www"] # from ranks.nl        
      FOREIGN_CHARACTERS = 'àáâãäåßéèêëìíîïñòóôõöùúûüýÿ'
      
      class << self
        def condition_names_for_column
          super + ["kwords", "kw"]
        end
      end
      
      def to_conditions(value)
        strs = []
        subs = []
        
        search_parts = value.gsub(/,/, " ").split(/ /)
        replace_non_alnum_characters!(search_parts)
        search_parts.uniq!
        remove_blacklisted_words!(search_parts)
        return if search_parts.blank?
        
        search_parts.each do |search_part|
          strs << "#{column_sql} #{like_condition_name} ?"
          subs << "%#{search_part}%"
        end
        
        [strs.join(" AND "), *subs]
      end
      
      private
        def replace_non_alnum_characters!(search_parts)
          search_parts.each do |word|
            word.downcase!
            word.gsub!(/[^[:alnum:]#{FOREIGN_CHARACTERS}]/, '')
          end
        end
        
        def remove_blacklisted_words!(search_parts)
          search_parts.delete_if { |word| word.blank? || BLACKLISTED_WORDS.include?(word.downcase) }
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
searchgasm-1.5.3 lib/searchgasm/condition/keywords.rb