usefull_scopes 0.2.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +13 -0
- data/CONTRIBUTING.md +33 -0
- data/Gemfile +11 -10
- data/README.md +71 -20
- data/Rakefile +0 -1
- data/lib/usefull_scopes.rb +6 -5
- data/lib/usefull_scopes/version.rb +1 -1
- data/test/lib/scopes_test.rb +658 -0
- data/test/test_helper.rb +10 -11
- data/usefull_scopes.gemspec +6 -0
- metadata +53 -20
- data/test/lib/usefull_scopes_test.rb +0 -652
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6efcab8a891866f4ef9f7dfc27efafff32d333cb
|
4
|
+
data.tar.gz: 564365193cdc25796641285b196f6930ccea9e3f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f88198052f2a67b4f9a617d76908853a7b861acce54ed5788824ebf0d0366430d962fa4b3518f3eb366e93efc60237990068efef3d9fff973858e6e49bb4c988
|
7
|
+
data.tar.gz: 662838b654160f95d8575e05d09e5455bddc91098a512258b06e9e230555ac250cb832517c3077c88f05ab3c2185c9b248d7581e076e7051f66b1ec6f1801f9e
|
data/.travis.yml
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
We love pull requests. Here's a quick guide.
|
4
|
+
|
5
|
+
Fork, then clone the repo:
|
6
|
+
|
7
|
+
git clone git@github.com:your-username/usefull_scopes.git
|
8
|
+
|
9
|
+
Make sure the tests pass:
|
10
|
+
|
11
|
+
rake
|
12
|
+
|
13
|
+
Make your change. Add tests for your change. Make the tests pass:
|
14
|
+
|
15
|
+
rake
|
16
|
+
|
17
|
+
Push to your fork and [submit a pull request][pr].
|
18
|
+
|
19
|
+
[pr]: https://github.com/kaize/usefull_scopes/compare/
|
20
|
+
|
21
|
+
At this point you're waiting on us. We like to at least comment on pull requests
|
22
|
+
within three business days (and, typically, one business day). We may suggest
|
23
|
+
some changes or improvements or alternatives.
|
24
|
+
|
25
|
+
Some things that will increase the chance that your pull request is accepted:
|
26
|
+
|
27
|
+
* Write tests.
|
28
|
+
* Follow common [style guides][style].
|
29
|
+
* Write a [good commit message][commit].
|
30
|
+
* squash your commits before sending PR.
|
31
|
+
|
32
|
+
[style]: https://github.com/thoughtbot/guides/tree/master/style
|
33
|
+
[commit]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
|
data/Gemfile
CHANGED
@@ -1,14 +1,15 @@
|
|
1
|
-
source "
|
1
|
+
source "https://rubygems.org"
|
2
2
|
|
3
|
-
# Specify your gem's dependencies in usefull_scopes.gemspec
|
4
3
|
gemspec
|
5
4
|
|
6
5
|
gem 'rake'
|
7
|
-
|
8
|
-
|
9
|
-
gem '
|
10
|
-
gem '
|
11
|
-
gem '
|
12
|
-
gem '
|
13
|
-
gem '
|
14
|
-
|
6
|
+
|
7
|
+
group :test do
|
8
|
+
gem 'minitest', '~> 5'
|
9
|
+
gem 'minitest-reporters'
|
10
|
+
gem 'factory_girl'
|
11
|
+
gem 'simplecov'
|
12
|
+
gem 'coveralls'
|
13
|
+
|
14
|
+
gem 'sqlite3'
|
15
|
+
end
|
data/README.md
CHANGED
@@ -17,12 +17,15 @@ Or install it yourself as:
|
|
17
17
|
|
18
18
|
## Usage
|
19
19
|
|
20
|
-
In order to
|
20
|
+
In order to access these scopes, include `UsefullScopes` module in your model.
|
21
|
+
|
22
|
+
For example:
|
21
23
|
|
22
24
|
class User < ActiveRecord::Base
|
23
25
|
include UsefullScopes
|
24
26
|
end
|
25
27
|
|
28
|
+
|
26
29
|
### Global scopes
|
27
30
|
|
28
31
|
<table>
|
@@ -32,25 +35,49 @@ In order to use these scopes, we need to include `UsefullScopes` module in our m
|
|
32
35
|
</tr>
|
33
36
|
<tr>
|
34
37
|
<td>random</td>
|
35
|
-
<td>
|
38
|
+
<td>Random record</td>
|
36
39
|
</tr>
|
37
40
|
<tr>
|
38
41
|
<td>exclude</td>
|
39
|
-
<td>
|
42
|
+
<td>Records who are not in a given array (you could also provide a single object as an argument)</td>
|
40
43
|
</tr>
|
41
44
|
<tr>
|
42
45
|
<td>with</td>
|
43
|
-
<td>
|
46
|
+
<td>Records, where attributes' values are corresponding to a given hash.</td>
|
44
47
|
</tr>
|
45
48
|
<tr>
|
46
49
|
<td>without</td>
|
47
|
-
<td>
|
50
|
+
<td>Records, where attributes' values are `NULL` or aren't equal to values from a given hash.</td>
|
51
|
+
</tr>
|
52
|
+
<tr>
|
53
|
+
<td>asc_by</td>
|
54
|
+
<td>Records sorted in ascending order using provided attributes.</td>
|
55
|
+
</tr>
|
56
|
+
<tr>
|
57
|
+
<td>desc_by</td>
|
58
|
+
<td>Records sorted in descending order using provided attributes.</td>
|
59
|
+
</tr>
|
60
|
+
<tr>
|
61
|
+
<td>more_than</td>
|
62
|
+
<td>FIXME</td>
|
63
|
+
</tr>
|
64
|
+
<tr>
|
65
|
+
<td>less_than</td>
|
66
|
+
<td>FIXME</td>
|
67
|
+
</tr>
|
68
|
+
<tr>
|
69
|
+
<td>more_or_equal</td>
|
70
|
+
<td>FIXME</td>
|
71
|
+
</tr>
|
72
|
+
<tr>
|
73
|
+
<td>less_or_equal</td>
|
74
|
+
<td>FIXME</td>
|
48
75
|
</tr>
|
49
76
|
</table>
|
50
77
|
|
51
78
|
### Scopes per attribute
|
52
79
|
|
53
|
-
These are the scopes created for each model
|
80
|
+
These are the scopes created for each of the model attribute.
|
54
81
|
|
55
82
|
<table>
|
56
83
|
<tr>
|
@@ -58,24 +85,40 @@ These are the scopes created for each model's attribute.
|
|
58
85
|
<th>Description</th>
|
59
86
|
</tr>
|
60
87
|
<tr>
|
61
|
-
<td>
|
62
|
-
<td>
|
88
|
+
<td>by_{attribute}</td>
|
89
|
+
<td>Records ordered by attribute in descending order.</td>
|
63
90
|
</tr>
|
64
91
|
<tr>
|
65
|
-
<td>
|
66
|
-
<td>
|
92
|
+
<td>asc_{by_attribute}</td>
|
93
|
+
<td>Records ordered by attribute in ascending order.</td>
|
67
94
|
</tr>
|
68
95
|
<tr>
|
69
|
-
<td>
|
70
|
-
<td>
|
96
|
+
<td>like_by_{attribute}</td>
|
97
|
+
<td>Records, where attribute's value LIKE a given term.</td>
|
71
98
|
</tr>
|
72
99
|
<tr>
|
73
|
-
<td>
|
74
|
-
<td>Сase insensitive implementation of `
|
100
|
+
<td>ilike_by_{attribute}</td>
|
101
|
+
<td>Сase insensitive implementation of `like_by_{attribute}`.</td>
|
102
|
+
</tr>
|
103
|
+
<tr>
|
104
|
+
<td>{attribute}_more</td>
|
105
|
+
<td>Records with attribute's value greater than a given value.</td>
|
106
|
+
</tr>
|
107
|
+
<tr>
|
108
|
+
<td>{attribute}_less</td>
|
109
|
+
<td>Records with attribute's value less than a given value.</td>
|
110
|
+
</tr>
|
111
|
+
<tr>
|
112
|
+
<td>{attribute}_more_or_equal</td>
|
113
|
+
<td>Records with attribute's value greater or equal than a given value.</td>
|
114
|
+
</tr>
|
115
|
+
<tr>
|
116
|
+
<td>{attribute}_less_or_equal</td>
|
117
|
+
<td>Records with attribute's value less or equal than a given value.</td>
|
75
118
|
</tr>
|
76
119
|
</table>
|
77
120
|
|
78
|
-
###
|
121
|
+
### Examples
|
79
122
|
|
80
123
|
Now, it is time to play with our model!
|
81
124
|
|
@@ -112,8 +155,16 @@ Now, it is time to play with our model!
|
|
112
155
|
|
113
156
|
## Contributing
|
114
157
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
158
|
+
Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details.
|
159
|
+
|
160
|
+
## Credits
|
161
|
+
|
162
|
+
Maintained by kaize.
|
163
|
+
|
164
|
+
Thank you to all our amazing [contributors](http://github.com/kaize/usefull_scopes/contributors)!
|
165
|
+
|
166
|
+
## License
|
167
|
+
|
168
|
+
usefull_scopes is Copyright © 2012-2014 kaize. It is free
|
169
|
+
software, and may be redistributed under the terms specified in the LICENSE
|
170
|
+
file.
|
data/Rakefile
CHANGED
data/lib/usefull_scopes.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
1
3
|
module UsefullScopes
|
2
|
-
autoload :Version, 'usefull_scopes/version'
|
3
4
|
extend ActiveSupport::Concern
|
4
5
|
|
5
6
|
included do
|
6
|
-
scope :random, order("RANDOM()")
|
7
|
+
scope :random, -> { order("RANDOM()") }
|
7
8
|
|
8
9
|
scope :exclude, ->(collection_or_object) {
|
9
10
|
collection = Array(collection_or_object)
|
@@ -96,7 +97,7 @@ module UsefullScopes
|
|
96
97
|
when Hash
|
97
98
|
hash_or_object
|
98
99
|
when Array
|
99
|
-
|
100
|
+
|
100
101
|
else
|
101
102
|
raise TypeError, "Hash or AR object is expected"
|
102
103
|
end
|
@@ -114,9 +115,9 @@ module UsefullScopes
|
|
114
115
|
attribute_names.each do |a|
|
115
116
|
a = a.to_sym
|
116
117
|
|
117
|
-
scope "by_#{a}", order(arel_table[a].desc)
|
118
|
+
scope "by_#{a}", -> { order(arel_table[a].desc) }
|
118
119
|
|
119
|
-
scope "asc_by_#{a}", order(arel_table[a].asc)
|
120
|
+
scope "asc_by_#{a}", -> { order(arel_table[a].asc) }
|
120
121
|
|
121
122
|
scope "like_by_#{a}", ->(term) {
|
122
123
|
quoted_term = connection.quote(term + '%')
|
@@ -0,0 +1,658 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ScopesTest < TestCase
|
4
|
+
def setup
|
5
|
+
3.times { create :model }
|
6
|
+
@model = Model.first
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_random_order_condition
|
10
|
+
random_request = Model.random
|
11
|
+
|
12
|
+
request_arel = random_request.arel
|
13
|
+
order = request_arel.order
|
14
|
+
order_conditions = order.orders
|
15
|
+
|
16
|
+
assert_includes order_conditions, "RANDOM()"
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_random_sql
|
20
|
+
random_request = Model.random
|
21
|
+
|
22
|
+
assert_equal "SELECT \"models\".* FROM \"models\" ORDER BY RANDOM()", random_request.to_sql
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_exclude_result
|
26
|
+
@models = Model.exclude(@model)
|
27
|
+
|
28
|
+
models_count = Model.count - 1
|
29
|
+
|
30
|
+
assert_equal models_count, @models.count
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_exclude_conditions
|
34
|
+
@models = Model.exclude(@model)
|
35
|
+
|
36
|
+
wheres = @models.arel.constraints
|
37
|
+
|
38
|
+
assert_equal 1, wheres.count
|
39
|
+
|
40
|
+
grouping = wheres.first.children.first
|
41
|
+
assert_kind_of Arel::Nodes::NotIn, grouping.expr
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_with_result
|
45
|
+
@models = Model.with({field_1: @model.field_1})
|
46
|
+
|
47
|
+
assert @models
|
48
|
+
|
49
|
+
assert_includes @models, @model
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_with_conditions
|
53
|
+
@models = Model.with({field_1: @model.field_1})
|
54
|
+
|
55
|
+
wheres = @models.arel.constraints
|
56
|
+
|
57
|
+
assert wheres.any?
|
58
|
+
|
59
|
+
wheres.each do |w|
|
60
|
+
equality = w.children.first
|
61
|
+
assert_kind_of Arel::Nodes::Equality, equality
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_with_incorrect_params
|
66
|
+
begin
|
67
|
+
@models = Model.with("field_1 = #{@model.field_1}")
|
68
|
+
rescue Exception => e
|
69
|
+
assert_equal "Hash is expected", e.message
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_without_result
|
74
|
+
@models = Model.without({field_1: @model.field_1})
|
75
|
+
|
76
|
+
assert @models
|
77
|
+
|
78
|
+
refute @models.include?(@model)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_without_conditions
|
82
|
+
@models = Model.without({field_1: @model.field_1})
|
83
|
+
|
84
|
+
wheres = @models.arel.constraints
|
85
|
+
|
86
|
+
assert wheres.any?
|
87
|
+
|
88
|
+
wheres.each do |w|
|
89
|
+
grouping = w.children.first
|
90
|
+
|
91
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
92
|
+
grouping.expr.children.each do |grouping_part|
|
93
|
+
assert_kind_of Arel::Nodes::NotIn, grouping_part
|
94
|
+
|
95
|
+
assert_kind_of Arel::Attributes::Attribute, grouping_part.left
|
96
|
+
|
97
|
+
assert_equal :field_1, grouping_part.left.name
|
98
|
+
assert_equal 1, grouping_part.right
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_without_incorrect_params
|
104
|
+
begin
|
105
|
+
@models = Model.without("field_1 = #{@model.field_1}")
|
106
|
+
rescue Exception => e
|
107
|
+
assert_equal "Hash is expected", e.message
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_like_by_result
|
112
|
+
|
113
|
+
assert_respond_to Model, :like_by_field_2
|
114
|
+
|
115
|
+
@models = Model.like_by_field_2(@model.field_2[0..3])
|
116
|
+
|
117
|
+
assert @models.any?
|
118
|
+
assert_includes @models, @model
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_like_by_condition
|
122
|
+
@models = Model.like_by_field_2(@model.field_2[0..3])
|
123
|
+
|
124
|
+
wheres = @models.arel.constraints
|
125
|
+
|
126
|
+
assert wheres.any?
|
127
|
+
|
128
|
+
wheres.each do |w|
|
129
|
+
grouping = w.children.first
|
130
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
131
|
+
assert grouping.expr.match "like"
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_ilike_by_result
|
136
|
+
assert_respond_to Model, :ilike_by_field_2
|
137
|
+
|
138
|
+
@models = Model.ilike_by_field_2(@model.field_2[0..3])
|
139
|
+
|
140
|
+
assert @models.any?
|
141
|
+
assert_includes @models, @model
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_ilike_by_condition
|
145
|
+
@models = Model.ilike_by_field_2(@model.field_2[0..3])
|
146
|
+
|
147
|
+
|
148
|
+
wheres = @models.arel.constraints
|
149
|
+
|
150
|
+
assert wheres.any?
|
151
|
+
|
152
|
+
wheres.each do |w|
|
153
|
+
grouping = w.children.first
|
154
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
155
|
+
assert_kind_of Arel::Nodes::Matches, grouping.expr
|
156
|
+
|
157
|
+
assert_kind_of Arel::Attributes::Attribute, grouping.expr.left
|
158
|
+
assert_equal :field_2, grouping.expr.left.name
|
159
|
+
assert_equal "stri%", grouping.expr.right
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_desc_by_result
|
164
|
+
assert_respond_to Model, :desc_by
|
165
|
+
|
166
|
+
@models = Model.desc_by(:field_1)
|
167
|
+
|
168
|
+
assert @models.any?
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_desc_by_condition_array_attrs
|
172
|
+
attrs = [:field_1, :field_2]
|
173
|
+
|
174
|
+
@models = Model.desc_by(attrs)
|
175
|
+
|
176
|
+
arel = @models.arel
|
177
|
+
orders_conditions = arel.orders
|
178
|
+
|
179
|
+
assert orders_conditions.any?
|
180
|
+
|
181
|
+
orders_conditions.each_with_index do |condition, index|
|
182
|
+
assert_kind_of Arel::Nodes::Descending, condition
|
183
|
+
assert_kind_of Arel::Attributes::Attribute, condition.expr
|
184
|
+
|
185
|
+
assert_equal attrs[index], condition.expr.name
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_desc_by_condition
|
190
|
+
@models = Model.desc_by(:field_1)
|
191
|
+
|
192
|
+
arel = @models.arel
|
193
|
+
orders_conditions = arel.orders
|
194
|
+
|
195
|
+
assert orders_conditions.any?
|
196
|
+
|
197
|
+
orders_conditions.each do |condition|
|
198
|
+
assert_kind_of Arel::Nodes::Descending, condition
|
199
|
+
assert_kind_of Arel::Attributes::Attribute, condition.expr
|
200
|
+
|
201
|
+
assert_equal :field_1, condition.expr.name
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_desc_by_incorrect_params
|
206
|
+
begin
|
207
|
+
@models = Model.desc_by("field_1")
|
208
|
+
rescue Exception => e
|
209
|
+
assert_equal "Symbol or Array of symbols is expected", e.message
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_asc_by_result
|
214
|
+
assert_respond_to Model, :asc_by
|
215
|
+
|
216
|
+
@models = Model.asc_by(:field_1)
|
217
|
+
|
218
|
+
assert @models.any?
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_asc_by_condition_array_attrs
|
222
|
+
attrs = [:field_1, :field_2]
|
223
|
+
|
224
|
+
@models = Model.asc_by(attrs)
|
225
|
+
|
226
|
+
arel = @models.arel
|
227
|
+
orders_conditions = arel.orders
|
228
|
+
|
229
|
+
assert orders_conditions.any?
|
230
|
+
|
231
|
+
orders_conditions.each_with_index do |condition, index|
|
232
|
+
assert_kind_of Arel::Nodes::Ascending, condition
|
233
|
+
assert_kind_of Arel::Attributes::Attribute, condition.expr
|
234
|
+
|
235
|
+
assert_equal attrs[index], condition.expr.name
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
def test_asc_by_condition
|
240
|
+
@models = Model.asc_by(:field_1)
|
241
|
+
|
242
|
+
arel = @models.arel
|
243
|
+
orders_conditions = arel.orders
|
244
|
+
|
245
|
+
assert orders_conditions.any?
|
246
|
+
|
247
|
+
orders_conditions.each do |condition|
|
248
|
+
assert_kind_of Arel::Nodes::Ascending, condition
|
249
|
+
assert_kind_of Arel::Attributes::Attribute, condition.expr
|
250
|
+
|
251
|
+
assert_equal :field_1, condition.expr.name
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_asc_by_incorrect_params
|
256
|
+
begin
|
257
|
+
@models = Model.asc_by("field_1")
|
258
|
+
rescue Exception => e
|
259
|
+
assert_equal "Symbol or Array of symbols is expected", e.message
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
def test_field_more_than_value_by_result
|
264
|
+
@model_less = create :model
|
265
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
266
|
+
|
267
|
+
@models = Model.field_1_more(@model_less.field_1)
|
268
|
+
|
269
|
+
assert @models.any?
|
270
|
+
assert @models.include?(@model_more)
|
271
|
+
refute @models.include?(@model_less)
|
272
|
+
end
|
273
|
+
|
274
|
+
def test_field_more_than_object_by_result
|
275
|
+
@model_less = create :model
|
276
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
277
|
+
|
278
|
+
@models = Model.field_1_more(@model_less)
|
279
|
+
|
280
|
+
assert @models.any?
|
281
|
+
assert @models.include?(@model_more)
|
282
|
+
refute @models.include?(@model_less)
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_field_more_by_condition
|
286
|
+
@models = Model.field_1_more(@model.field_1)
|
287
|
+
|
288
|
+
wheres = @models.arel.constraints
|
289
|
+
|
290
|
+
assert wheres.any?
|
291
|
+
|
292
|
+
wheres.each do |w|
|
293
|
+
grouping = w.children.first
|
294
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
295
|
+
assert_kind_of Arel::Nodes::GreaterThan, grouping.expr
|
296
|
+
assert_equal @model.field_1, grouping.expr.right
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
def test_field_less_than_value_by_result
|
301
|
+
@model_less = create :model
|
302
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
303
|
+
|
304
|
+
@models = Model.field_1_less(@model_more.field_1)
|
305
|
+
|
306
|
+
assert @models.any?
|
307
|
+
assert @models.include?(@model_less)
|
308
|
+
refute @models.include?(@model_more)
|
309
|
+
end
|
310
|
+
|
311
|
+
def test_field_less_than_object_by_result
|
312
|
+
@model_less = create :model
|
313
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
314
|
+
|
315
|
+
@models = Model.field_1_less(@model_more)
|
316
|
+
|
317
|
+
assert @models.any?
|
318
|
+
assert @models.include?(@model_less)
|
319
|
+
refute @models.include?(@model_more)
|
320
|
+
end
|
321
|
+
|
322
|
+
def test_field_less_by_condition
|
323
|
+
@models = Model.field_1_less(@model.field_1)
|
324
|
+
|
325
|
+
wheres = @models.arel.constraints
|
326
|
+
|
327
|
+
assert wheres.any?
|
328
|
+
|
329
|
+
wheres.each do |w|
|
330
|
+
grouping = w.children.first
|
331
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
332
|
+
assert_kind_of Arel::Nodes::LessThan, grouping.expr
|
333
|
+
assert_equal @model.field_1, grouping.expr.right
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
def test_field_more_or_equal_than_value_by_result
|
338
|
+
@model_less = create :model
|
339
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
340
|
+
|
341
|
+
@models = Model.field_1_more_or_equal(@model_more.field_1)
|
342
|
+
|
343
|
+
assert @models.any?
|
344
|
+
assert @models.include?(@model_more)
|
345
|
+
refute @models.include?(@model_less)
|
346
|
+
end
|
347
|
+
|
348
|
+
def test_field_more_or_equal_than_object_by_result
|
349
|
+
@model_less = create :model
|
350
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
351
|
+
|
352
|
+
@models = Model.field_1_more_or_equal(@model_more)
|
353
|
+
|
354
|
+
assert @models.any?
|
355
|
+
assert @models.include?(@model_more)
|
356
|
+
refute @models.include?(@model_less)
|
357
|
+
end
|
358
|
+
|
359
|
+
def test_field_more_or_equal_by_condition
|
360
|
+
@models = Model.field_1_more_or_equal(@model.field_1)
|
361
|
+
|
362
|
+
wheres = @models.arel.constraints
|
363
|
+
|
364
|
+
assert wheres.any?
|
365
|
+
|
366
|
+
wheres.each do |w|
|
367
|
+
grouping = w.children.first
|
368
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
369
|
+
assert_kind_of Arel::Nodes::GreaterThanOrEqual, grouping.expr
|
370
|
+
assert_equal @model.field_1, grouping.expr.right
|
371
|
+
end
|
372
|
+
end
|
373
|
+
|
374
|
+
def test_field_less_or_equal_than_value_by_result
|
375
|
+
@model_less = create :model
|
376
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
377
|
+
|
378
|
+
@models = Model.field_1_less_or_equal(@model_less.field_1)
|
379
|
+
|
380
|
+
assert @models.any?
|
381
|
+
assert @models.include?(@model_less)
|
382
|
+
refute @models.include?(@model_more)
|
383
|
+
end
|
384
|
+
|
385
|
+
def test_field_less_or_equal_than_object_by_result
|
386
|
+
@model_less = create :model
|
387
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
388
|
+
|
389
|
+
@models = Model.field_1_less_or_equal(@model_less)
|
390
|
+
|
391
|
+
assert @models.any?
|
392
|
+
assert @models.include?(@model_less)
|
393
|
+
refute @models.include?(@model_more)
|
394
|
+
end
|
395
|
+
def test_field_less_or_equal_by_condition
|
396
|
+
@models = Model.field_1_less_or_equal(@model.field_1)
|
397
|
+
|
398
|
+
wheres = @models.arel.constraints
|
399
|
+
|
400
|
+
assert wheres.any?
|
401
|
+
|
402
|
+
wheres.each do |w|
|
403
|
+
grouping = w.children.first
|
404
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
405
|
+
assert_kind_of Arel::Nodes::LessThanOrEqual, grouping.expr
|
406
|
+
assert_equal @model.field_1, grouping.expr.right
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
def test_more_than_result
|
411
|
+
@model = Model.last
|
412
|
+
|
413
|
+
@models = Model.more_than({field_1: 1})
|
414
|
+
|
415
|
+
assert @models.any?
|
416
|
+
assert @models.include?(@model)
|
417
|
+
end
|
418
|
+
|
419
|
+
def test_more_than_condition_value
|
420
|
+
@models = Model.more_than({field_1: 1})
|
421
|
+
|
422
|
+
wheres = @models.arel.constraints
|
423
|
+
|
424
|
+
assert wheres.any?
|
425
|
+
|
426
|
+
wheres.each do |w|
|
427
|
+
grouping = w.children.first
|
428
|
+
|
429
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
430
|
+
grouping.expr.children.each do |grouping_part|
|
431
|
+
assert_kind_of Arel::Nodes::GreaterThan, grouping_part
|
432
|
+
|
433
|
+
assert_kind_of Arel::Attributes::Attribute, grouping_part.left
|
434
|
+
|
435
|
+
assert_equal :field_1, grouping_part.left.name
|
436
|
+
assert_equal 1, grouping_part.right
|
437
|
+
end
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
441
|
+
def test_more_than_condition_ar_object
|
442
|
+
@models = Model.more_than(@model)
|
443
|
+
|
444
|
+
wheres = @models.arel.constraints
|
445
|
+
|
446
|
+
assert wheres.any?
|
447
|
+
|
448
|
+
wheres.each do |w|
|
449
|
+
grouping = w.children.first
|
450
|
+
|
451
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
452
|
+
grouping.expr.children.each do |grouping_part|
|
453
|
+
assert_kind_of Arel::Nodes::GreaterThan, grouping_part
|
454
|
+
|
455
|
+
assert_kind_of Arel::Attributes::Attribute, grouping_part.left
|
456
|
+
|
457
|
+
assert_equal :id, grouping_part.left.name
|
458
|
+
assert_equal 1, grouping_part.right
|
459
|
+
end
|
460
|
+
end
|
461
|
+
end
|
462
|
+
|
463
|
+
def test_more_than_incorrect_params
|
464
|
+
begin
|
465
|
+
@models = Model.more_than("field_1")
|
466
|
+
rescue Exception => e
|
467
|
+
assert_equal "Hash or AR object is expected", e.message
|
468
|
+
end
|
469
|
+
end
|
470
|
+
|
471
|
+
def test_less_than_result
|
472
|
+
@model_first = Model.first
|
473
|
+
@model_last = Model.last
|
474
|
+
|
475
|
+
@models = Model.less_than({field_1: @model_last.field_1})
|
476
|
+
|
477
|
+
assert @models.any?
|
478
|
+
assert @models.include?(@model_first)
|
479
|
+
end
|
480
|
+
|
481
|
+
def test_less_than_condition_value
|
482
|
+
@models = Model.less_than({field_1: 1})
|
483
|
+
|
484
|
+
wheres = @models.arel.constraints
|
485
|
+
|
486
|
+
assert wheres.any?
|
487
|
+
|
488
|
+
wheres.each do |w|
|
489
|
+
grouping = w.children.first
|
490
|
+
|
491
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
492
|
+
grouping.expr.children.each do |grouping_part|
|
493
|
+
assert_kind_of Arel::Nodes::LessThan, grouping_part
|
494
|
+
|
495
|
+
assert_kind_of Arel::Attributes::Attribute, grouping_part.left
|
496
|
+
|
497
|
+
assert_equal :field_1, grouping_part.left.name
|
498
|
+
assert_equal 1, grouping_part.right
|
499
|
+
end
|
500
|
+
end
|
501
|
+
end
|
502
|
+
|
503
|
+
def test_less_than_condition_ar_object
|
504
|
+
@models = Model.less_than(@model)
|
505
|
+
|
506
|
+
wheres = @models.arel.constraints
|
507
|
+
|
508
|
+
assert wheres.any?
|
509
|
+
|
510
|
+
wheres.each do |w|
|
511
|
+
grouping = w.children.first
|
512
|
+
|
513
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
514
|
+
grouping.expr.children.each do |grouping_part|
|
515
|
+
assert_kind_of Arel::Nodes::LessThan, grouping_part
|
516
|
+
|
517
|
+
assert_kind_of Arel::Attributes::Attribute, grouping_part.left
|
518
|
+
|
519
|
+
assert_equal :id, grouping_part.left.name
|
520
|
+
assert_equal 1, grouping_part.right
|
521
|
+
end
|
522
|
+
end
|
523
|
+
end
|
524
|
+
|
525
|
+
def test_less_than_incorrect_params
|
526
|
+
begin
|
527
|
+
@models = Model.less_than("field_1")
|
528
|
+
rescue Exception => e
|
529
|
+
assert_equal "Hash or AR object is expected", e.message
|
530
|
+
end
|
531
|
+
end
|
532
|
+
|
533
|
+
def test_more_or_equal_result
|
534
|
+
@model_first = Model.first
|
535
|
+
@model_last = Model.last
|
536
|
+
|
537
|
+
@models = Model.more_or_equal({field_1: @model_last.field_1})
|
538
|
+
|
539
|
+
assert @models.any?
|
540
|
+
assert @models.include?(@model_last)
|
541
|
+
assert_equal @models.count, 1
|
542
|
+
end
|
543
|
+
|
544
|
+
def test_more_or_equal_condition_value
|
545
|
+
@models = Model.more_or_equal({field_1: 1})
|
546
|
+
|
547
|
+
wheres = @models.arel.constraints
|
548
|
+
|
549
|
+
assert wheres.any?
|
550
|
+
|
551
|
+
wheres.each do |w|
|
552
|
+
grouping = w.children.first
|
553
|
+
|
554
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
555
|
+
grouping.expr.children.each do |grouping_part|
|
556
|
+
assert_kind_of Arel::Nodes::GreaterThanOrEqual, grouping_part
|
557
|
+
|
558
|
+
assert_kind_of Arel::Attributes::Attribute, grouping_part.left
|
559
|
+
|
560
|
+
assert_equal :field_1, grouping_part.left.name
|
561
|
+
assert_equal 1, grouping_part.right
|
562
|
+
end
|
563
|
+
end
|
564
|
+
end
|
565
|
+
|
566
|
+
def test_more_or_equal_condition_ar_object
|
567
|
+
@models = Model.more_or_equal(@model)
|
568
|
+
|
569
|
+
wheres = @models.arel.constraints
|
570
|
+
|
571
|
+
assert wheres.any?
|
572
|
+
|
573
|
+
wheres.each do |w|
|
574
|
+
grouping = w.children.first
|
575
|
+
|
576
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
577
|
+
grouping.expr.children.each do |grouping_part|
|
578
|
+
assert_kind_of Arel::Nodes::GreaterThanOrEqual, grouping_part
|
579
|
+
|
580
|
+
assert_kind_of Arel::Attributes::Attribute, grouping_part.left
|
581
|
+
|
582
|
+
assert_equal :id, grouping_part.left.name
|
583
|
+
assert_equal 1, grouping_part.right
|
584
|
+
end
|
585
|
+
end
|
586
|
+
end
|
587
|
+
|
588
|
+
def test_more_or_equal_incorrect_params
|
589
|
+
begin
|
590
|
+
@models = Model.more_or_equal("field_1")
|
591
|
+
rescue Exception => e
|
592
|
+
assert_equal "Hash or AR object is expected", e.message
|
593
|
+
end
|
594
|
+
end
|
595
|
+
|
596
|
+
def test_less_or_equal_result
|
597
|
+
@model_first = Model.first
|
598
|
+
@model_last = Model.last
|
599
|
+
|
600
|
+
@models = Model.less_or_equal({field_1: @model_first.field_1})
|
601
|
+
|
602
|
+
assert @models.any?
|
603
|
+
assert @models.include?(@model_first)
|
604
|
+
assert_equal @models.count, 1
|
605
|
+
end
|
606
|
+
|
607
|
+
def test_less_or_equal_condition_value
|
608
|
+
@models = Model.less_or_equal({field_1: 1})
|
609
|
+
|
610
|
+
wheres = @models.arel.constraints
|
611
|
+
|
612
|
+
assert wheres.any?
|
613
|
+
|
614
|
+
wheres.each do |w|
|
615
|
+
grouping = w.children.first
|
616
|
+
|
617
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
618
|
+
grouping.expr.children.each do |grouping_part|
|
619
|
+
assert_kind_of Arel::Nodes::LessThanOrEqual, grouping_part
|
620
|
+
|
621
|
+
assert_kind_of Arel::Attributes::Attribute, grouping_part.left
|
622
|
+
|
623
|
+
assert_equal :field_1, grouping_part.left.name
|
624
|
+
assert_equal 1, grouping_part.right
|
625
|
+
end
|
626
|
+
end
|
627
|
+
end
|
628
|
+
|
629
|
+
def test_less_or_equal_condition_ar_object
|
630
|
+
@models = Model.less_or_equal(@model)
|
631
|
+
|
632
|
+
wheres = @models.arel.constraints
|
633
|
+
|
634
|
+
assert wheres.any?
|
635
|
+
|
636
|
+
wheres.each do |w|
|
637
|
+
grouping = w.children.first
|
638
|
+
|
639
|
+
assert_kind_of Arel::Nodes::Grouping, grouping
|
640
|
+
grouping.expr.children.each do |grouping_part|
|
641
|
+
assert_kind_of Arel::Nodes::LessThanOrEqual, grouping_part
|
642
|
+
|
643
|
+
assert_kind_of Arel::Attributes::Attribute, grouping_part.left
|
644
|
+
|
645
|
+
assert_equal :id, grouping_part.left.name
|
646
|
+
assert_equal 1, grouping_part.right
|
647
|
+
end
|
648
|
+
end
|
649
|
+
end
|
650
|
+
|
651
|
+
def test_less_or_equal_incorrect_params
|
652
|
+
begin
|
653
|
+
@models = Model.less_or_equal("field_1")
|
654
|
+
rescue Exception => e
|
655
|
+
assert_equal "Hash or AR object is expected", e.message
|
656
|
+
end
|
657
|
+
end
|
658
|
+
end
|