data_table 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,17 +31,24 @@ module DataTable
31
31
  def _where_conditions query, search_fields, join_operator = "OR"
32
32
  return if query.blank?
33
33
 
34
- conditions = []
35
- parameters = []
36
-
37
- search_fields.map do |field|
38
- clause = _where_condition(query, field)
39
- next if clause.empty?
40
- conditions << clause.shift
41
- parameters += clause
34
+ all_conditions = []
35
+ all_parameters = []
36
+
37
+ query.split.each do |term|
38
+ conditions = []
39
+ parameters = []
40
+
41
+ search_fields.each do |field|
42
+ next if (clause = _where_condition(term, field.dup)).empty?
43
+ conditions << clause.shift
44
+ parameters += clause
45
+ end
46
+
47
+ all_conditions << conditions
48
+ all_parameters << parameters
42
49
  end
43
50
 
44
- ["(" + conditions.join(" #{join_operator} ") + ")", *parameters.flatten]
51
+ [all_conditions.map {|conditions| "(" + conditions.join(" #{join_operator} ") + ")" }.join(" AND "), *all_parameters.flatten]
45
52
  end
46
53
 
47
54
  def _where_condition query, field
@@ -1,3 +1,3 @@
1
1
  module DataTable
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -32,7 +32,13 @@ describe DataTable do
32
32
  send(:_where_conditions, "query", %w(foo bar)).should == ["(UPPER(foo) LIKE ? OR UPPER(bar) LIKE ?)", "%QUERY%", "%QUERY%"]
33
33
  end
34
34
 
35
- context "complex conditions" do
35
+ context "with multiple terms" do
36
+ it "should return an AR array with conditions for all combinations of terms and fields" do
37
+ send(:_where_conditions, "q1 q2", %w(f1 f2)).should == ["(UPPER(f1) LIKE ? OR UPPER(f2) LIKE ?) AND (UPPER(f1) LIKE ? OR UPPER(f2) LIKE ?)", "%Q1%", "%Q1%", "%Q2%", "%Q2%"]
38
+ end
39
+ end
40
+
41
+ context "with complex conditions" do
36
42
  it "should return an AR array with an entry for each search field" do
37
43
  send(:_where_conditions, "query", [%w(foo bar)]).should == ["((UPPER(foo) LIKE ? AND UPPER(bar) LIKE ?))", "%QUERY%", "%QUERY%"]
38
44
  end
@@ -52,6 +58,11 @@ describe DataTable do
52
58
  it "should ignore a split query if the query isn't the size of the split fields" do
53
59
  send(:_where_conditions, "query", ['foz', ['foo', 'bar', {:split => '-'}]]).should == ["(UPPER(foz) LIKE ?)", "%QUERY%"]
54
60
  end
61
+
62
+ it "should still work with multiple terms" do
63
+ send(:_where_conditions, "q1 q-2", ['F1', ['P1', 'P2', {:split => '-'}]]).should ==
64
+ ["(UPPER(F1) LIKE ?) AND (UPPER(F1) LIKE ? OR (UPPER(P1) LIKE ? AND UPPER(P2) LIKE ?))", "%Q1%", "%Q-2%", "%Q%", "%2%"]
65
+ end
55
66
  end
56
67
  end
57
68
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: data_table
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.1
5
+ version: 0.2.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jason Dew
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-12 00:00:00 -04:00
13
+ date: 2011-09-03 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency