Sha256: cc0b67d61883bed7ccfba20cdc9626a0c8dac55c48c1a70ffb46e37c371e47da

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 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        
      
      class << self
        def condition_names_for_column
          super + ["kwords", "kw"]
        end
      end
      
      def to_conditions(value)
        strs = []
        subs = []
        
        search_parts = value.gsub(/,/, " ").split(/ /).collect { |word| word.downcase.gsub(/[^[:alnum:]]/, ''); }.uniq.select { |word| !BLACKLISTED_WORDS.include?(word.downcase) && !word.blank? }
        return if search_parts.blank?
        
        search_parts.each do |search_part|
          strs << "#{column_sql} LIKE ?"
          subs << "%#{search_part}%"
        end
        
        [strs.join(" AND "), *subs]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
searchgasm-1.5.0 lib/searchgasm/condition/keywords.rb
searchgasm-1.5.2 lib/searchgasm/condition/keywords.rb
searchgasm-1.5.1 lib/searchgasm/condition/keywords.rb