aub-record_filter 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -141,6 +141,7 @@ object that has methods to specify a number of different conditions and to negat
141
141
  with(:permalink, nil) # ['permalink IS NULL']
142
142
  with(:permalink).is_null # ['permalink IS NULL']
143
143
  with(:permalink, nil).not # ['permalink IS NOT NULL']
144
+ with(:permalink).is_not_null # ['permalink IS NOT NULL']
144
145
 
145
146
  The following condition types are supported through the Restriction API:
146
147
 
@@ -164,10 +165,22 @@ for more details on how to use them.
164
165
 
165
166
  with(:id).in([1, 2, 3]) # ['id in (?)', [1, 2, 3]]
166
167
 
168
+ with(:id).not_in([4, 5, 6]) # ['id NOT IN (?)', [4, 5, 6]]
169
+
167
170
  with(:content).like('%easy%') # ['content LIKE ?', '%easy%']
168
171
 
169
172
  with(:content).not.like('%hard%') # ['content NOT LIKE ?', '%hard%']
170
173
 
174
+ The comparison operators (greater_than, less_than, greater_than_or_equal_to and less_than_or_equal_to)
175
+ are aliased to their short forms (gt, lt, gte and lte).
176
+
177
+ It is also possible to specify multiple conditions on a single line using the 'and' and 'or' methods,
178
+ eliminating the need to use a conjunction block in many common cases.
179
+
180
+ with(:expired_at).gt(Time.now).or.is_null # ['expired_at > ? OR expired_at IS NULL', Time.now]
181
+
182
+ with(:id).gt(100).and.lt(1000) # ['id > ? AND id < ?', 100, 1000]
183
+
171
184
  === Boolean Operations
172
185
 
173
186
  Conditions can be combined with boolean operators using the methods all_of, any_of, none_of
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 9
4
- :patch: 5
4
+ :patch: 6
@@ -5,7 +5,7 @@ module RecordFilter
5
5
 
6
6
  def parse_column_in_table(column, table)
7
7
  while column.is_a?(Hash)
8
- table = table.join_association(column.keys[0]).right_table
8
+ table = table.find_join!(column.keys[0]).right_table
9
9
  column = column.values[0]
10
10
  end
11
11
  [column, table]
@@ -27,6 +27,7 @@ module RecordFilter
27
27
  params[:group] = group_bys.map { |group_by| group_by.to_sql } * ', ' unless group_bys.empty?
28
28
  params[:limit] = @conjunction.limit if @conjunction.limit
29
29
  params[:offset] = @conjunction.offset if @conjunction.offset
30
+ params[:readonly] = false
30
31
  params
31
32
  end
32
33
  end
@@ -41,25 +41,25 @@ module RecordFilter
41
41
 
42
42
  class LessThan < Base
43
43
  def to_positive_sql
44
- "#{@column_name} < ?"
44
+ "#{@column_name} < #{@value.nil? ? 'NULL' : '?'}"
45
45
  end
46
46
  end
47
47
 
48
48
  class LessThanOrEqualTo < Base
49
49
  def to_positive_sql
50
- "#{@column_name} <= ?"
50
+ "#{@column_name} <= #{@value.nil? ? 'NULL' : '?'}"
51
51
  end
52
52
  end
53
53
 
54
54
  class GreaterThan < Base
55
55
  def to_positive_sql
56
- "#{@column_name} > ?"
56
+ "#{@column_name} > #{@value.nil? ? 'NULL' : '?'}"
57
57
  end
58
58
  end
59
59
 
60
60
  class GreaterThanOrEqualTo < Base
61
61
  def to_positive_sql
62
- "#{@column_name} >= ?"
62
+ "#{@column_name} >= #{@value.nil? ? 'NULL' : '?'}"
63
63
  end
64
64
  end
65
65
 
@@ -56,6 +56,16 @@ module RecordFilter
56
56
  end
57
57
  end
58
58
 
59
+ def find_join(join_key)
60
+ @joins_cache[join_key]
61
+ end
62
+
63
+ def find_join!(join_key)
64
+ join = find_join(join_key)
65
+ raise InvalidJoinException.new("The join #{join_key} was not found.") unless join
66
+ join
67
+ end
68
+
59
69
  def all_joins
60
70
  @joins + @joins.inject([]) do |child_joins, join|
61
71
  child_joins.concat(join.right_table.all_joins)
@@ -124,7 +134,7 @@ module RecordFilter
124
134
  def alias_for_association(association)
125
135
  @alias_cache ||= {}
126
136
  @alias_cache[association.name] ||=
127
- "#{@aliased ? @table_alias.to_s : @model_class.table_name}__#{association.name.to_s.downcase}"
137
+ "#{@aliased ? @table_alias.to_s : @model_class.table_name}__#{association.name.to_s.underscore}"
128
138
  end
129
139
 
130
140
  alias_method :alias_for_class, :alias_for_association
data/lib/record_filter.rb CHANGED
@@ -26,7 +26,8 @@ module RecordFilter
26
26
  # already exists in the class it is created on or one of its superclasses.
27
27
  class InvalidFilterNameException < StandardError; end
28
28
 
29
- # An exception that is raised when no columns are privided to specify an explicit join.
29
+ # An exception that is raised when no columns are privided to specify an explicit join
30
+ # or when the join names are incorrect.
30
31
  class InvalidJoinException < StandardError; end
31
32
 
32
33
  # An exception raised in the case where a named filter is called from within a filter
@@ -55,7 +55,7 @@ describe 'raising exceptions' do
55
55
  Post.filter do
56
56
  order({ :this_is_not_there => :eh }, :asc)
57
57
  end.inspect
58
- }.should raise_error(RecordFilter::AssociationNotFoundException)
58
+ }.should raise_error(RecordFilter::InvalidJoinException)
59
59
  end
60
60
 
61
61
  it 'should raise ColumnNotFoundException for explicit joins on bad column names for the right table' do
@@ -164,6 +164,22 @@ describe 'filter qualifiers' do
164
164
  end
165
165
  end
166
166
 
167
+ describe 'with explicit joins' do
168
+ before do
169
+ Post.filter do
170
+ with(:published, false)
171
+ join(Comment, :inner) do
172
+ on(:id => :post_id)
173
+ end
174
+ order(Comment => :id)
175
+ end.inspect
176
+ end
177
+
178
+ it 'should create the correct order params' do
179
+ Post.last_find[:order].should == %q(posts__comment.id ASC)
180
+ end
181
+ end
182
+
167
183
  describe 'with the order supplied as a string' do
168
184
  before do
169
185
  Post.filter do
@@ -258,7 +258,7 @@ describe 'named filters' do
258
258
  it 'should have an empty conditions hash' do
259
259
  blog = Class.new(Blog)
260
260
  blog.named_filter(:ordered_by_id) { order(:id, :desc) }
261
- blog.ordered_by_id.proxy_options.should == { :order => %q("blogs".id DESC) }
261
+ blog.ordered_by_id.proxy_options.should == { :readonly => false, :order => %q("blogs".id DESC) }
262
262
  end
263
263
  end
264
264
  end
@@ -9,7 +9,7 @@ describe 'RecordFilter restrictions' do
9
9
  Post.filter do
10
10
  with :permalink, 'blog-post'
11
11
  end.inspect
12
- Post.last_find.should == { :conditions => [%q{"posts".permalink = ?}, 'blog-post'] }
12
+ Post.last_find.should == { :readonly => false, :conditions => [%q{"posts".permalink = ?}, 'blog-post'] }
13
13
  end
14
14
 
15
15
  it 'should filter for equality with multiple conditions' do
@@ -17,7 +17,7 @@ describe 'RecordFilter restrictions' do
17
17
  with :permalink, 'blog-post'
18
18
  with :blog_id, 3
19
19
  end.inspect
20
- Post.last_find.should == { :conditions => [%q{("posts".permalink = ?) AND ("posts".blog_id = ?)}, 'blog-post', 3] }
20
+ Post.last_find.should == { :readonly => false, :conditions => [%q{("posts".permalink = ?) AND ("posts".blog_id = ?)}, 'blog-post', 3] }
21
21
  end
22
22
 
23
23
  it 'should filter by comparison operators' do
@@ -26,17 +26,22 @@ describe 'RecordFilter restrictions' do
26
26
  Post.filter do
27
27
  with(:created_at).send(set[0], Time.parse('2009-01-03 23:02:00'))
28
28
  end.inspect
29
- Post.last_find.should == { :conditions => ["\"posts\".created_at #{set[2]} ?", Time.parse('2009-01-03 23:02:00')] }
29
+ Post.last_find.should == { :readonly => false, :conditions => ["\"posts\".created_at #{set[2]} ?", Time.parse('2009-01-03 23:02:00')] }
30
+
31
+ Post.filter do
32
+ with(:id).send(set[0], nil)
33
+ end.inspect
34
+ Post.last_find.should == { :readonly => false, :conditions => ["\"posts\".id #{set[2]} NULL"] }
30
35
 
31
36
  Post.filter do
32
37
  with(:created_at).send(set[1], Time.parse('2009-01-03 23:02:00'))
33
38
  end.inspect
34
- Post.last_find.should == { :conditions => ["\"posts\".created_at #{set[2]} ?", Time.parse('2009-01-03 23:02:00')] }
39
+ Post.last_find.should == { :readonly => false, :conditions => ["\"posts\".created_at #{set[2]} ?", Time.parse('2009-01-03 23:02:00')] }
35
40
 
36
41
  Post.filter do
37
42
  with(:created_at).not.send(set[0], Time.parse('2009-02-02 02:22:22'))
38
43
  end.inspect
39
- Post.last_find.should == { :conditions => ["NOT (\"posts\".created_at #{set[2]} ?)", Time.parse('2009-02-02 02:22:22')] }
44
+ Post.last_find.should == { :readonly => false, :conditions => ["NOT (\"posts\".created_at #{set[2]} ?)", Time.parse('2009-02-02 02:22:22')] }
40
45
  end
41
46
  end
42
47
 
@@ -44,70 +49,70 @@ describe 'RecordFilter restrictions' do
44
49
  Post.filter do
45
50
  with(:blog_id).equal_to(nil)
46
51
  end.inspect
47
- Post.last_find.should == { :conditions => [%q("posts".blog_id IS NULL)] }
52
+ Post.last_find.should == { :readonly => false, :conditions => [%q("posts".blog_id IS NULL)] }
48
53
  end
49
54
 
50
55
  it 'should filter for in' do
51
56
  Post.filter do
52
57
  with(:blog_id).in [1, 3, 5]
53
58
  end.inspect
54
- Post.last_find.should == { :conditions => [%q{"posts".blog_id IN (?)}, [1, 3, 5]] }
59
+ Post.last_find.should == { :readonly => false, :conditions => [%q{"posts".blog_id IN (?)}, [1, 3, 5]] }
55
60
  end
56
61
 
57
62
  it 'should negate IN filters correctly' do
58
63
  Post.filter do
59
64
  with(:blog_id).not.in [1, 3, 5]
60
65
  end.inspect
61
- Post.last_find.should == { :conditions => [%q{"posts".blog_id NOT IN (?)}, [1, 3, 5]] }
66
+ Post.last_find.should == { :readonly => false, :conditions => [%q{"posts".blog_id NOT IN (?)}, [1, 3, 5]] }
62
67
  end
63
68
 
64
69
  it 'should work correctly for NOT IN' do
65
70
  Post.filter do
66
71
  with(:blog_id).not_in [1, 3, 5]
67
72
  end.inspect
68
- Post.last_find.should == { :conditions => [%q{"posts".blog_id NOT IN (?)}, [1, 3, 5]] }
73
+ Post.last_find.should == { :readonly => false, :conditions => [%q{"posts".blog_id NOT IN (?)}, [1, 3, 5]] }
69
74
  end
70
75
 
71
76
  it 'should do the right thing for IN filters with empty arrays' do
72
77
  Post.filter do
73
78
  with(:blog_id).in([])
74
79
  end.inspect
75
- Post.last_find.should == { :conditions => [%q("posts".blog_id IN (?)), []] }
80
+ Post.last_find.should == { :readonly => false, :conditions => [%q("posts".blog_id IN (?)), []] }
76
81
  end
77
82
 
78
83
  it 'should do the right thing for IN filters with single values' do
79
84
  Post.filter do
80
85
  with(:blog_id).in(1)
81
86
  end.inspect
82
- Post.last_find.should == { :conditions => [%q("posts".blog_id IN (?)), 1] }
87
+ Post.last_find.should == { :readonly => false, :conditions => [%q("posts".blog_id IN (?)), 1] }
83
88
  end
84
89
 
85
90
  it 'should do the right thing for IN filters with nil' do
86
91
  Post.filter do
87
92
  with(:blog_id).in(nil)
88
93
  end.inspect
89
- Post.last_find.should == { :conditions => [%q("posts".blog_id IN (?)), nil] }
94
+ Post.last_find.should == { :readonly => false, :conditions => [%q("posts".blog_id IN (?)), nil] }
90
95
  end
91
96
 
92
97
  it 'should do the right thing for IN filters with a range' do
93
98
  Post.filter do
94
99
  with(:blog_id).in(1..5)
95
100
  end.inspect
96
- Post.last_find.should == { :conditions => [%q("posts".blog_id IN (?)), 1..5] }
101
+ Post.last_find.should == { :readonly => false, :conditions => [%q("posts".blog_id IN (?)), 1..5] }
97
102
  end
98
103
 
99
104
  it 'should negate is_not_null conditions correctly' do
100
105
  Post.filter do
101
106
  with(:blog_id).is_not_null.not
102
107
  end.inspect
103
- Post.last_find.should == { :conditions => [%q("posts".blog_id IS NULL)] }
108
+ Post.last_find.should == { :readonly => false, :conditions => [%q("posts".blog_id IS NULL)] }
104
109
  end
105
110
 
106
111
  it 'should double-negate correctly' do
107
112
  Post.filter do
108
113
  with(:blog_id, 3).not.not
109
114
  end.inspect
110
- Post.last_find.should == { :conditions => [%q("posts".blog_id = ?), 3] }
115
+ Post.last_find.should == { :readonly => false, :conditions => [%q("posts".blog_id = ?), 3] }
111
116
  end
112
117
 
113
118
  it 'should filter for between' do
@@ -116,21 +121,21 @@ describe 'RecordFilter restrictions' do
116
121
  Post.filter do
117
122
  with(:created_at).between time1..time2
118
123
  end.inspect
119
- Post.last_find.should == { :conditions => [%q{"posts".created_at BETWEEN ? AND ?}, time1, time2] }
124
+ Post.last_find.should == { :readonly => false, :conditions => [%q{"posts".created_at BETWEEN ? AND ?}, time1, time2] }
120
125
  end
121
126
 
122
127
  it 'should filter for between with two arguments passed' do
123
128
  Post.filter do
124
129
  with(:id).between(1, 5)
125
130
  end.inspect
126
- Post.last_find.should == { :conditions => [%q("posts".id BETWEEN ? AND ?), 1, 5] }
131
+ Post.last_find.should == { :readonly => false, :conditions => [%q("posts".id BETWEEN ? AND ?), 1, 5] }
127
132
  end
128
133
 
129
134
  it 'should filter for between with an array passed' do
130
135
  Post.filter do
131
136
  with(:id).between([2, 6])
132
137
  end.inspect
133
- Post.last_find.should == { :conditions => [%q("posts".id BETWEEN ? AND ?), 2, 6] }
138
+ Post.last_find.should == { :readonly => false, :conditions => [%q("posts".id BETWEEN ? AND ?), 2, 6] }
134
139
  end
135
140
 
136
141
  it 'should filter by none_of' do
@@ -140,7 +145,7 @@ describe 'RecordFilter restrictions' do
140
145
  with(:permalink, 'eek')
141
146
  end
142
147
  end.inspect
143
- Post.last_find.should == { :conditions => [%q{NOT (("posts".blog_id = ?) OR ("posts".permalink = ?))}, 1, 'eek'] }
148
+ Post.last_find.should == { :readonly => false, :conditions => [%q{NOT (("posts".blog_id = ?) OR ("posts".permalink = ?))}, 1, 'eek'] }
144
149
  end
145
150
 
146
151
  it 'should filter by not_all_of' do
@@ -150,7 +155,7 @@ describe 'RecordFilter restrictions' do
150
155
  with(:permalink, 'eek')
151
156
  end
152
157
  end.inspect
153
- Post.last_find.should == { :conditions => [%q{NOT (("posts".blog_id = ?) AND ("posts".permalink = ?))}, 1, 'eek'] }
158
+ Post.last_find.should == { :readonly => false, :conditions => [%q{NOT (("posts".blog_id = ?) AND ("posts".permalink = ?))}, 1, 'eek'] }
154
159
  end
155
160
 
156
161
  it 'should filter by disjunction' do
@@ -160,7 +165,7 @@ describe 'RecordFilter restrictions' do
160
165
  with(:permalink).equal_to 'my-post'
161
166
  end
162
167
  end.inspect
163
- Post.last_find.should == { :conditions => [%q{("posts".blog_id = ?) OR ("posts".permalink = ?)}, 1, 'my-post'] }
168
+ Post.last_find.should == { :readonly => false, :conditions => [%q{("posts".blog_id = ?) OR ("posts".permalink = ?)}, 1, 'my-post'] }
164
169
  end
165
170
 
166
171
  it 'should filter by disjunction composed of conjunction' do
@@ -174,8 +179,8 @@ describe 'RecordFilter restrictions' do
174
179
  end
175
180
  end.inspect
176
181
 
177
- Post.last_find.should == { :conditions => [%q{(("posts".blog_id = ?) AND ("posts".permalink = ?)) OR ("posts".permalink = ?)},
178
- 1, 'my-post', 'another-post'] }
182
+ Post.last_find.should == { :readonly => false, :conditions => [%q{(("posts".blog_id = ?) AND ("posts".permalink = ?)) OR ("posts".permalink = ?)},
183
+ 1, 'my-post', 'another-post'] }
179
184
  end
180
185
 
181
186
  it 'should filter for nil' do
@@ -183,7 +188,7 @@ describe 'RecordFilter restrictions' do
183
188
  Post.filter do
184
189
  with(:permalink).send(method)
185
190
  end.inspect
186
- Post.last_find.should == { :conditions => [%q("posts".permalink IS NULL)] }
191
+ Post.last_find.should == { :readonly => false, :conditions => [%q("posts".permalink IS NULL)] }
187
192
  end
188
193
  end
189
194
 
@@ -191,48 +196,48 @@ describe 'RecordFilter restrictions' do
191
196
  Post.filter do
192
197
  with(:permalink).is_not_null
193
198
  end.inspect
194
- Post.last_find.should == { :conditions => [%q("posts".permalink IS NOT NULL)] }
199
+ Post.last_find.should == { :readonly => false, :conditions => [%q("posts".permalink IS NOT NULL)] }
195
200
  end
196
201
 
197
202
  it 'should support like' do
198
203
  Post.filter do
199
204
  with(:permalink).like('%piglets%')
200
205
  end.inspect
201
- Post.last_find.should == { :conditions => [%q("posts".permalink LIKE ?), '%piglets%'] }
206
+ Post.last_find.should == { :readonly => false, :conditions => [%q("posts".permalink LIKE ?), '%piglets%'] }
202
207
  end
203
208
 
204
209
  it 'should support NOT LIKE' do
205
210
  Post.filter do
206
211
  with(:permalink).not.like('%ostriches%')
207
212
  end.inspect
208
- Post.last_find.should == { :conditions => [%q("posts".permalink NOT LIKE ?), '%ostriches%'] }
213
+ Post.last_find.should == { :readonly => false, :conditions => [%q("posts".permalink NOT LIKE ?), '%ostriches%'] }
209
214
  end
210
215
 
211
216
  it 'should provide access to the filter class in the filter' do
212
217
  Post.filter do
213
218
  with(:permalink).not.equal_to(filter_class.name)
214
219
  end.inspect
215
- Post.last_find.should == { :conditions => [%q("posts".permalink <> ?), 'Post'] }
220
+ Post.last_find.should == { :readonly => false, :conditions => [%q("posts".permalink <> ?), 'Post'] }
216
221
  end
217
222
 
218
223
  it 'should work correctly for one-line ORs with is_null' do
219
224
  Post.filter do
220
225
  with(:permalink, 'abc').or.is_null
221
226
  end.inspect
222
- Post.last_find.should == { :conditions => [%q(("posts".permalink = ?) OR ("posts".permalink IS NULL)), 'abc'] }
227
+ Post.last_find.should == { :readonly => false, :conditions => [%q(("posts".permalink = ?) OR ("posts".permalink IS NULL)), 'abc'] }
223
228
  end
224
229
 
225
230
  it 'should work correctly for one-line ANDs with is_null' do
226
231
  Post.filter do
227
232
  with(:id).gt(56).and.lt(200)
228
233
  end.inspect
229
- Post.last_find.should == { :conditions => [%q(("posts".id > ?) AND ("posts".id < ?)), 56, 200] }
234
+ Post.last_find.should == { :readonly => false, :conditions => [%q(("posts".id > ?) AND ("posts".id < ?)), 56, 200] }
230
235
  end
231
236
 
232
237
  it 'should work properly with multiple one-line conjunctions' do
233
238
  Post.filter do
234
239
  with(:id).gt(56).and.lt(200).or.gt(500).or.lt(15)
235
240
  end.inspect
236
- Post.last_find.should == { :conditions => [%q(("posts".id > ?) AND (("posts".id < ?) OR (("posts".id > ?) OR ("posts".id < ?)))), 56, 200, 500, 15] }
241
+ Post.last_find.should == { :readonly => false, :conditions => [%q(("posts".id > ?) AND (("posts".id < ?) OR (("posts".id > ?) OR ("posts".id < ?)))), 56, 200, 500, 15] }
237
242
  end
238
243
  end
data/spec/test.db CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aub-record_filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aubrey Holland
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-05-11 00:00:00 -07:00
13
+ date: 2009-05-13 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies: []
16
16