data_table 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,10 +28,48 @@ module DataTable
28
28
  joins.to_a
29
29
  end
30
30
 
31
- def _where_conditions query, search_fields
31
+ def _where_conditions query, search_fields, join_operator = "OR"
32
32
  return if query.blank?
33
33
 
34
- [search_fields.map {|field| ["UPPER(#{field}) LIKE ?"] }.join(" OR "), *(["%#{query.upcase}%"] * search_fields.size)]
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
42
+ end
43
+
44
+ ["(" + conditions.join(" #{join_operator} ") + ")", *parameters.flatten]
45
+ end
46
+
47
+ def _where_condition query, field
48
+ return [] if query.blank?
49
+
50
+ if field.is_a? Array
51
+ options = field.extract_options!
52
+
53
+ if options[:split]
54
+ conditions = []
55
+ parameters = []
56
+ split_query = query.split(options[:split])
57
+
58
+ if split_query.size == field.size
59
+ field.map do |f|
60
+ conditions << "UPPER(#{f}) LIKE ?"
61
+ parameters << "%#{split_query.shift.upcase}%"
62
+ end
63
+ ["(" + conditions.join(" AND ") + ")", *parameters]
64
+ else
65
+ []
66
+ end
67
+ else
68
+ _where_conditions(query, field, "AND")
69
+ end
70
+ else
71
+ ["UPPER(#{field}) LIKE ?", "%#{query.upcase}%"]
72
+ end
35
73
  end
36
74
 
37
75
  def _order_fields params, fields
@@ -1,3 +1,3 @@
1
1
  module DataTable
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -29,9 +29,30 @@ describe DataTable do
29
29
  end
30
30
 
31
31
  it "should return an AR array with an entry for each search field" do
32
- send(:_where_conditions, "query", %w(foo bar)).should == ["UPPER(foo) LIKE ? OR UPPER(bar) LIKE ?", "%QUERY%", "%QUERY%"]
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
36
+ it "should return an AR array with an entry for each search field" do
37
+ send(:_where_conditions, "query", [%w(foo bar)]).should == ["((UPPER(foo) LIKE ? AND UPPER(bar) LIKE ?))", "%QUERY%", "%QUERY%"]
38
+ end
39
+
40
+ it "should return an AR array with an entry for each search field with a split query" do
41
+ send(:_where_conditions, "query-two", [['foo', 'bar', {:split => '-'}]]).should == ["((UPPER(foo) LIKE ? AND UPPER(bar) LIKE ?))", "%QUERY%", "%TWO%"]
42
+ end
43
+
44
+ it "should return an AR array with an entry for each search field with ands and ors" do
45
+ send(:_where_conditions, "query", ['foz', ['foo', 'bar']]).should == ["(UPPER(foz) LIKE ? OR (UPPER(foo) LIKE ? AND UPPER(bar) LIKE ?))", "%QUERY%", "%QUERY%", "%QUERY%"]
46
+ end
47
+
48
+ it "should return an AR array with an entry for each search field with ands and ors with a split query" do
49
+ 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%"]
50
+ end
51
+
52
+ it "should ignore a split query if the query isn't the size of the split fields" do
53
+ send(:_where_conditions, "query", ['foz', ['foo', 'bar', {:split => '-'}]]).should == ["(UPPER(foz) LIKE ?)", "%QUERY%"]
54
+ end
55
+ end
35
56
  end
36
57
 
37
58
  context "#_discover_joins" do
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_table
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 2
9
- - 0
10
- version: 0.2.0
5
+ version: 0.2.1
11
6
  platform: ruby
12
7
  authors:
13
8
  - Jason Dew
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-06-02 00:00:00 -04:00
13
+ date: 2011-06-12 00:00:00 -04:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
@@ -26,11 +21,6 @@ dependencies:
26
21
  requirements:
27
22
  - - ~>
28
23
  - !ruby/object:Gem::Version
29
- hash: 7
30
- segments:
31
- - 3
32
- - 0
33
- - 0
34
24
  version: 3.0.0
35
25
  type: :runtime
36
26
  version_requirements: *id001
@@ -42,12 +32,6 @@ dependencies:
42
32
  requirements:
43
33
  - - ~>
44
34
  - !ruby/object:Gem::Version
45
- hash: 1923831917
46
- segments:
47
- - 3
48
- - 0
49
- - pre
50
- - 2
51
35
  version: 3.0.pre2
52
36
  type: :runtime
53
37
  version_requirements: *id002
@@ -59,11 +43,6 @@ dependencies:
59
43
  requirements:
60
44
  - - ~>
61
45
  - !ruby/object:Gem::Version
62
- hash: 15
63
- segments:
64
- - 2
65
- - 0
66
- - 0
67
46
  version: 2.0.0
68
47
  type: :development
69
48
  version_requirements: *id003
@@ -75,11 +54,6 @@ dependencies:
75
54
  requirements:
76
55
  - - ~>
77
56
  - !ruby/object:Gem::Version
78
- hash: 35
79
- segments:
80
- - 2
81
- - 11
82
- - 0
83
57
  version: 2.11.0
84
58
  type: :development
85
59
  version_requirements: *id004
@@ -91,11 +65,6 @@ dependencies:
91
65
  requirements:
92
66
  - - ~>
93
67
  - !ruby/object:Gem::Version
94
- hash: 23
95
- segments:
96
- - 1
97
- - 0
98
- - 0
99
68
  version: 1.0.0
100
69
  type: :development
101
70
  version_requirements: *id005
@@ -142,23 +111,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
111
  requirements:
143
112
  - - ">="
144
113
  - !ruby/object:Gem::Version
145
- hash: 3
146
- segments:
147
- - 0
148
114
  version: "0"
149
115
  required_rubygems_version: !ruby/object:Gem::Requirement
150
116
  none: false
151
117
  requirements:
152
118
  - - ">="
153
119
  - !ruby/object:Gem::Version
154
- hash: 3
155
- segments:
156
- - 0
157
120
  version: "0"
158
121
  requirements: []
159
122
 
160
123
  rubyforge_project: data_table
161
- rubygems_version: 1.4.1
124
+ rubygems_version: 1.6.2
162
125
  signing_key:
163
126
  specification_version: 3
164
127
  summary: Simple data preparation from AR/Mongoid to the jQuery DataTables plugin