lib/scoped_search.rb in wvanbergen-scoped_search-0.1.3 vs lib/scoped_search.rb in wvanbergen-scoped_search-0.2.0

- old
+ new

@@ -32,20 +32,34 @@ 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 + end end # all keywords must be matched, so join the conditions with AND - return { :conditions => [conditions.join(' AND '), query_params] } + return { :conditions => [conditions.join(' AND '), query_params] } end end end end \ No newline at end of file