Sha256: 762e658d045bf54f1121a3bff9e25241837a93156243966d3c9933cbe8e9038b

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module BinaryLogic
  module Searchgasm
    module Search
      module ConditionTypes
        class KeywordsCondition < Condition
          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 name_for_column(column)
              return unless string_column?(column)
              super
            end
            
            def aliases_for_column(column)
              ["#{column.name}_kwords", "#{column.name}_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 << "#{quoted_table_name}.#{quoted_column_name} LIKE ?"
              subs << "%#{search_part}%"
            end
            
            [strs.join(" AND "), *subs]
          end
        end
      end
      
      Conditions.register_condition(ConditionTypes::KeywordsCondition)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
searchgasm-0.9.6 lib/searchgasm/search/condition_types/keywords_condition.rb