Skip to content

Commit

Permalink
Merge pull request jasondew#5 from asceth/master
Browse files Browse the repository at this point in the history
Complex search queries
  • Loading branch information
jasondew committed Jun 12, 2011
2 parents 4585f04 + f145c68 commit feb0045
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
42 changes: 40 additions & 2 deletions lib/data_table/active_record.rb
Expand Up @@ -28,10 +28,48 @@ def _discover_joins fields
joins.to_a
end

def _where_conditions query, search_fields
def _where_conditions query, search_fields, join_operator = "OR"
return if query.blank?

[search_fields.map {|field| ["UPPER(#{field}) LIKE ?"] }.join(" OR "), *(["%#{query.upcase}%"] * search_fields.size)]
conditions = []
parameters = []

search_fields.map do |field|
clause = _where_condition(query, field)
next if clause.empty?
conditions << clause.shift
parameters += clause
end

["(" + conditions.join(" #{join_operator} ") + ")", *parameters.flatten]
end

def _where_condition query, field
return [] if query.blank?

if field.is_a? Array
options = field.extract_options!

if options[:split]
conditions = []
parameters = []
split_query = query.split(options[:split])

if split_query.size == field.size
field.map do |f|
conditions << "UPPER(#{f}) LIKE ?"
parameters << "%#{split_query.shift.upcase}%"
end
["(" + conditions.join(" AND ") + ")", *parameters]
else
[]
end
else
_where_conditions(query, field, "AND")
end
else
["UPPER(#{field}) LIKE ?", "%#{query.upcase}%"]
end
end

def _order_fields params, fields
Expand Down
23 changes: 22 additions & 1 deletion spec/active_record_data_table_spec.rb
Expand Up @@ -29,9 +29,30 @@
end

it "should return an AR array with an entry for each search field" do
send(:_where_conditions, "query", %w(foo bar)).should == ["UPPER(foo) LIKE ? OR UPPER(bar) LIKE ?", "%QUERY%", "%QUERY%"]
send(:_where_conditions, "query", %w(foo bar)).should == ["(UPPER(foo) LIKE ? OR UPPER(bar) LIKE ?)", "%QUERY%", "%QUERY%"]
end

context "complex conditions" do
it "should return an AR array with an entry for each search field" do
send(:_where_conditions, "query", [%w(foo bar)]).should == ["((UPPER(foo) LIKE ? AND UPPER(bar) LIKE ?))", "%QUERY%", "%QUERY%"]
end

it "should return an AR array with an entry for each search field with a split query" do
send(:_where_conditions, "query-two", [['foo', 'bar', {:split => '-'}]]).should == ["((UPPER(foo) LIKE ? AND UPPER(bar) LIKE ?))", "%QUERY%", "%TWO%"]
end

it "should return an AR array with an entry for each search field with ands and ors" do
send(:_where_conditions, "query", ['foz', ['foo', 'bar']]).should == ["(UPPER(foz) LIKE ? OR (UPPER(foo) LIKE ? AND UPPER(bar) LIKE ?))", "%QUERY%", "%QUERY%", "%QUERY%"]
end

it "should return an AR array with an entry for each search field with ands and ors with a split query" do
send(:_where_conditions, "query-two", ['foz', ['foo', 'bar', {:split => '-'}]]).should == ["(UPPER(foz) LIKE ? OR (UPPER(foo) LIKE ? AND UPPER(bar) LIKE ?))", "%QUERY-TWO%", "%QUERY%", "%TWO%"]
end

it "should ignore a split query if the query isn't the size of the split fields" do
send(:_where_conditions, "query", ['foz', ['foo', 'bar', {:split => '-'}]]).should == ["(UPPER(foz) LIKE ?)", "%QUERY%"]
end
end
end

context "#_discover_joins" do
Expand Down

0 comments on commit feb0045

Please sign in to comment.