lib/scoped_search.rb in wvanbergen-scoped_search-0.2.0 vs lib/scoped_search.rb in wvanbergen-scoped_search-0.3.0
- old
+ new
@@ -1,11 +1,13 @@
module ScopedSearch
module ClassMethods
def self.extended(base)
+ require 'scoped_search/reg_tokens'
require 'scoped_search/query_language_parser'
+ require 'scoped_search/query_conditions_builder'
end
# Creates a named scope in the class it was called upon
def searchable_on(*fields)
self.cattr_accessor :scoped_search_fields
@@ -14,52 +16,22 @@
end
# Build a hash that is used for the named_scope search_for.
# This function will split the search_string into keywords, and search for all the keywords
# in the fields that were provided to searchable_on
- def build_scoped_search_conditions(search_string)
+ def build_scoped_search_conditions(search_string)
if search_string.nil? || search_string.strip.blank?
- return { :conditions => nil }
- else
- conditions = []
- query_params = {}
-
- QueryLanguageParser.parse(search_string).each_with_index do |search_condition, index|
- keyword_name = "keyword_#{index}".to_sym
- query_params[keyword_name] = "%#{search_condition.first}%"
-
- # a keyword may be found in any of the provided fields, so join the conitions with OR
- if search_condition.length == 2 && search_condition.last == :not
- keyword_conditions = self.scoped_search_fields.map do |field|
- field_name = connection.quote_table_name(table_name) + "." + connection.quote_column_name(field)
- "(#{field_name} NOT LIKE :#{keyword_name.to_s} OR #{field_name} IS NULL)"
- end
- conditions << "(#{keyword_conditions.join(' AND ')})"
- elsif search_condition.length == 2 && search_condition.last == :or
- word1, word2 = query_params[keyword_name].split(' OR ')
-
- query_params.delete(keyword_name)
- keyword_name_a = "#{keyword_name.to_s}a".to_sym
- keyword_name_b = "#{keyword_name.to_s}b".to_sym
- query_params[keyword_name_a] = word1
- query_params[keyword_name_b] = word2
-
- keyword_conditions = self.scoped_search_fields.map do |field|
- field_name = connection.quote_table_name(table_name) + "." + connection.quote_column_name(field)
- "(#{field_name} LIKE :#{keyword_name_a.to_s} OR #{field_name} LIKE :#{keyword_name_b.to_s})"
- end
- conditions << "(#{keyword_conditions.join(' OR ')})"
- else
- keyword_conditions = self.scoped_search_fields.map do |field|
- field_name = connection.quote_table_name(table_name) + "." + connection.quote_column_name(field)
- "#{field_name} LIKE :#{keyword_name.to_s}"
- end
- conditions << "(#{keyword_conditions.join(' OR ')})"
- end
+ return {:conditions => nil}
+ else
+ query_fields = {}
+ self.scoped_search_fields.each do |field|
+ field_name = connection.quote_table_name(table_name) + "." + connection.quote_column_name(field)
+ query_fields[field_name] = self.columns_hash[field.to_s].type
end
-
- # all keywords must be matched, so join the conditions with AND
- return { :conditions => [conditions.join(' AND '), query_params] }
+
+ search_conditions = QueryLanguageParser.parse(search_string)
+ conditions = QueryConditionsBuilder.build_query(search_conditions, query_fields)
+ return {:conditions => conditions}
end
end
end
end
\ No newline at end of file