usefull_scopes 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.coveralls.yml +1 -0
- data/.gitignore +2 -0
- data/Gemfile +10 -0
- data/README.md +3 -8
- data/Rakefile +10 -0
- data/lib/usefull_scopes.rb +121 -14
- data/lib/usefull_scopes/version.rb +1 -1
- data/test/factories/models.rb +7 -0
- data/test/factories/sequences.rb +13 -0
- data/test/lib/usefull_scopes_test.rb +652 -0
- data/test/test_helper.rb +37 -0
- metadata +12 -4
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
repo_token: hHSEHjAw28Kly9dKy4u9dHPyDwIIWEKGI
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
@@ -2,3 +2,13 @@ source "http://rubygems.org"
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in usefull_scopes.gemspec
|
4
4
|
gemspec
|
5
|
+
|
6
|
+
gem 'rake'
|
7
|
+
gem 'sqlite3'
|
8
|
+
gem 'minitest'
|
9
|
+
gem 'tconsole'
|
10
|
+
gem 'turn'
|
11
|
+
gem 'activerecord'
|
12
|
+
gem 'factory_girl'
|
13
|
+
gem 'simplecov'
|
14
|
+
gem 'coveralls', require: false
|
data/README.md
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
# UsefullScopes
|
2
|
+
[![Build Status](https://travis-ci.org/kaize/usefull_scopes.png?branch=master)](https://travis-ci.org/kaize/usefull_scopes)
|
3
|
+
[![Coverage
|
4
|
+
status](https://coveralls.io/repos/kaize/usefull_scopes/badge.png?branch=master)](https://coveralls.io/r/kaize/usefull_scopes)
|
2
5
|
|
3
6
|
This gem provides additional scopes for your ActiveRecord models.
|
4
7
|
|
@@ -70,14 +73,6 @@ These are the scopes created for each model's attribute.
|
|
70
73
|
<td>ilike_by_attribute</td>
|
71
74
|
<td>Сase insensitive implementation of `like_by_attribute`</td>
|
72
75
|
</tr>
|
73
|
-
<tr>
|
74
|
-
<td>with_attribute</td>
|
75
|
-
<td>Returns records, where attribute's value is equal to a given value.<br/><b>Note: this scope is deprecated and will be removed in the following versions. Please, use `with` scope instead.</b></td>
|
76
|
-
</tr>
|
77
|
-
<tr>
|
78
|
-
<td>without_attribute</td>
|
79
|
-
<td>Returns records, where attribute's value is `NULL`.<br/><b>Note: this scope is deprecated and will be removed in the following versions. Please, use `without` scope instead.</b></td>
|
80
|
-
</tr>
|
81
76
|
</table>
|
82
77
|
|
83
78
|
### Example
|
data/Rakefile
CHANGED
data/lib/usefull_scopes.rb
CHANGED
@@ -4,16 +4,17 @@ module UsefullScopes
|
|
4
4
|
|
5
5
|
included do
|
6
6
|
scope :random, order("RANDOM()")
|
7
|
-
|
7
|
+
|
8
|
+
scope :exclude, ->(collection_or_object) {
|
8
9
|
collection = Array(collection_or_object)
|
9
10
|
values = collection.map do |id_or_object|
|
10
|
-
id_or_object
|
11
|
+
find_object_value_or_value(id_or_object)
|
11
12
|
end
|
12
13
|
return scoped unless values.any?
|
13
|
-
where(
|
14
|
+
where(arel_table[:id].not_in(values))
|
14
15
|
}
|
15
16
|
|
16
|
-
scope :with,
|
17
|
+
scope :with, ->(attrs_hash) {
|
17
18
|
case attrs_hash
|
18
19
|
when Hash
|
19
20
|
where(attrs_hash)
|
@@ -22,35 +23,141 @@ module UsefullScopes
|
|
22
23
|
end
|
23
24
|
}
|
24
25
|
|
25
|
-
scope :without,
|
26
|
+
scope :without, ->(*attrs) {
|
26
27
|
attrs_hash = attrs.extract_options!
|
27
28
|
query_params = []
|
28
29
|
|
29
30
|
attrs.each do |attr_name|
|
30
|
-
query_params <<
|
31
|
+
query_params << arel_table[attr_name].eq(nil)
|
31
32
|
end
|
32
33
|
|
33
34
|
attrs_hash.each do |attr_name, attr_value|
|
34
|
-
query_params <<
|
35
|
+
query_params << arel_table[attr_name].not_in(attr_value)
|
35
36
|
end
|
36
37
|
|
37
38
|
return scoped if query_params.blank?
|
38
|
-
|
39
|
+
|
40
|
+
where arel_table.create_and query_params
|
41
|
+
}
|
42
|
+
|
43
|
+
scope :desc_by, ->(attrs) {
|
44
|
+
order(collect_order_query_rules(attrs, :desc))
|
45
|
+
}
|
46
|
+
|
47
|
+
scope :asc_by, ->(attrs) {
|
48
|
+
order(collect_order_query_rules(attrs, :asc))
|
49
|
+
}
|
50
|
+
|
51
|
+
scope :more_than, ->(attrs) {
|
52
|
+
where_and(attrs, :gt)
|
53
|
+
}
|
54
|
+
|
55
|
+
scope :less_than, ->(attrs) {
|
56
|
+
where_and(attrs, :lt)
|
57
|
+
}
|
58
|
+
|
59
|
+
scope :more_or_equal, ->(attrs) {
|
60
|
+
where_and(attrs, :gteq)
|
39
61
|
}
|
40
62
|
|
63
|
+
scope :less_or_equal, ->(attrs) {
|
64
|
+
where_and(attrs, :lteq)
|
65
|
+
}
|
66
|
+
|
67
|
+
def self.collect_order_query_rules(array, rule)
|
68
|
+
query_params = []
|
69
|
+
|
70
|
+
attrs =
|
71
|
+
case array
|
72
|
+
when Array
|
73
|
+
array
|
74
|
+
when Symbol
|
75
|
+
[array]
|
76
|
+
else
|
77
|
+
raise TypeError, "Symbol or Array of symbols is expected"
|
78
|
+
end
|
79
|
+
|
80
|
+
attrs.each do |attr_name, attr_value|
|
81
|
+
query_params << arel_table[attr_name].send(rule)
|
82
|
+
end
|
83
|
+
|
84
|
+
raise ArgumentError, "Empty Array" if query_params.blank?
|
85
|
+
|
86
|
+
query_params
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.collect_query_rules(hash_or_object, rule, field = :id)
|
90
|
+
query_params = []
|
91
|
+
|
92
|
+
attrs =
|
93
|
+
case hash_or_object
|
94
|
+
when ActiveRecord::Base
|
95
|
+
{ field => hash_or_object.send(field) }
|
96
|
+
when Hash
|
97
|
+
hash_or_object
|
98
|
+
when Array
|
99
|
+
|
100
|
+
else
|
101
|
+
raise TypeError, "Hash or AR object is expected"
|
102
|
+
end
|
103
|
+
|
104
|
+
attrs.each do |attr_name, attr_value|
|
105
|
+
attr_value = attr_value.send(attr_name) if attr_value.is_a? ActiveRecord::Base
|
106
|
+
query_params << arel_table[attr_name].send(rule, attr_value)
|
107
|
+
end
|
108
|
+
|
109
|
+
raise ArgumentError, "Empty Hash" if query_params.blank?
|
110
|
+
|
111
|
+
query_params
|
112
|
+
end
|
113
|
+
|
41
114
|
attribute_names.each do |a|
|
42
|
-
|
43
|
-
scope "asc_by_#{a}", order("#{quoted_table_name}.#{a} ASC")
|
115
|
+
a = a.to_sym
|
44
116
|
|
45
|
-
scope "
|
117
|
+
scope "by_#{a}", order(arel_table[a].desc)
|
118
|
+
|
119
|
+
scope "asc_by_#{a}", order(arel_table[a].asc)
|
120
|
+
|
121
|
+
scope "like_by_#{a}", ->(term) {
|
46
122
|
quoted_term = connection.quote(term + '%')
|
47
123
|
where("lower(#{quoted_table_name}.#{a}) like #{quoted_term}")
|
48
124
|
}
|
49
|
-
|
50
|
-
|
51
|
-
|
125
|
+
|
126
|
+
scope "ilike_by_#{a}", ->(term) {
|
127
|
+
quoted_term = term + '%'
|
128
|
+
where(arel_table[a].matches(quoted_term))
|
129
|
+
}
|
130
|
+
|
131
|
+
scope "#{a}_more", ->(value_or_object) {
|
132
|
+
value = find_object_value_or_value(value_or_object, a)
|
133
|
+
where(arel_table[a].gt(value))
|
134
|
+
}
|
135
|
+
|
136
|
+
scope "#{a}_less", ->(value_or_object) {
|
137
|
+
value = find_object_value_or_value(value_or_object, a)
|
138
|
+
where(arel_table[a].lt(value))
|
139
|
+
}
|
140
|
+
|
141
|
+
scope "#{a}_more_or_equal", ->(value_or_object) {
|
142
|
+
value = find_object_value_or_value(value_or_object, a)
|
143
|
+
where(arel_table[a].gteq(value))
|
144
|
+
}
|
145
|
+
|
146
|
+
scope "#{a}_less_or_equal", ->(value_or_object) {
|
147
|
+
value = find_object_value_or_value(value_or_object, a)
|
148
|
+
where(arel_table[a].lteq(value))
|
52
149
|
}
|
53
150
|
end
|
151
|
+
|
152
|
+
def self.find_object_value_or_value(value_or_object, field = :id)
|
153
|
+
value_or_object.is_a?(ActiveRecord::Base) ? value_or_object.send(field) : value_or_object
|
154
|
+
end
|
155
|
+
|
156
|
+
def self.where_and(attrs, rule)
|
157
|
+
query_params = collect_query_rules(attrs, rule)
|
158
|
+
where arel_table.create_and query_params
|
159
|
+
end
|
160
|
+
|
54
161
|
end
|
55
162
|
end
|
56
163
|
|
@@ -0,0 +1,652 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module UsefullScopes
|
4
|
+
class ScopesTest < TestCase
|
5
|
+
def setup
|
6
|
+
3.times { create :model }
|
7
|
+
@model = Model.first
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_random_order_condition
|
11
|
+
random_request = Model.random
|
12
|
+
|
13
|
+
request_arel = random_request.arel
|
14
|
+
order = request_arel.order
|
15
|
+
order_conditions = order.orders
|
16
|
+
|
17
|
+
assert_includes order_conditions, "RANDOM()"
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_random_sql
|
21
|
+
random_request = Model.random
|
22
|
+
|
23
|
+
assert_equal "SELECT \"models\".* FROM \"models\" ORDER BY RANDOM()", random_request.to_sql
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_exclude_result
|
27
|
+
@models = Model.exclude(@model)
|
28
|
+
|
29
|
+
models_count = Model.count - 1
|
30
|
+
|
31
|
+
assert_equal models_count, @models.count
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_exclude_conditions
|
35
|
+
@models = Model.exclude(@model)
|
36
|
+
|
37
|
+
request_arel = @models.arel
|
38
|
+
condition = request_arel.where_clauses
|
39
|
+
assert_equal 1, condition.count
|
40
|
+
assert condition.first.match "NOT IN"
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_with_result
|
44
|
+
@models = Model.with({field_1: @model.field_1})
|
45
|
+
|
46
|
+
assert @models
|
47
|
+
|
48
|
+
assert_includes @models, @model
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_with_conditions
|
52
|
+
@models = Model.with({field_1: @model.field_1})
|
53
|
+
|
54
|
+
ctx = @models.arel.as_json["ctx"]
|
55
|
+
where_conditions = ctx.wheres
|
56
|
+
|
57
|
+
assert where_conditions.any?
|
58
|
+
|
59
|
+
where_conditions.each do |condition|
|
60
|
+
condition.children.each do |condition_part|
|
61
|
+
assert_kind_of Arel::Nodes::Equality, condition_part
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_with_incorrect_params
|
67
|
+
begin
|
68
|
+
@models = Model.with("field_1 = #{@model.field_1}")
|
69
|
+
rescue Exception => e
|
70
|
+
assert_equal "Hash is expected", e.message
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_without_result
|
75
|
+
@models = Model.without({field_1: @model.field_1})
|
76
|
+
|
77
|
+
assert @models
|
78
|
+
|
79
|
+
refute @models.include?(@model)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_without_conditions
|
83
|
+
@models = Model.without({field_1: @model.field_1})
|
84
|
+
|
85
|
+
ctx = @models.arel.as_json["ctx"]
|
86
|
+
where_conditions = ctx.wheres
|
87
|
+
|
88
|
+
assert where_conditions.any?
|
89
|
+
|
90
|
+
where_conditions.each do |condition|
|
91
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
92
|
+
condition.expr.children.each do |condition_part|
|
93
|
+
assert_kind_of Arel::Nodes::NotIn, condition_part
|
94
|
+
|
95
|
+
assert_kind_of Arel::Attributes::Attribute, condition_part.left
|
96
|
+
|
97
|
+
assert_equal :field_1, condition_part.left.name
|
98
|
+
assert_equal 1, condition_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
|
+
|
123
|
+
@models = Model.like_by_field_2(@model.field_2[0..3])
|
124
|
+
|
125
|
+
ctx = @models.arel.as_json["ctx"]
|
126
|
+
where_conditions = ctx.wheres
|
127
|
+
|
128
|
+
assert where_conditions.any?
|
129
|
+
|
130
|
+
where_conditions.each do |condition|
|
131
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
132
|
+
assert condition.expr.match "like"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_ilike_by_result
|
137
|
+
assert_respond_to Model, :ilike_by_field_2
|
138
|
+
|
139
|
+
# SQLite error %(
|
140
|
+
#@models = Model.ilike_by_field_2(@model.field_2[0..3])
|
141
|
+
|
142
|
+
#assert @models.any?
|
143
|
+
#assert_includes @models, @model
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_ilike_by_condition
|
147
|
+
@models = Model.ilike_by_field_2(@model.field_2[0..3])
|
148
|
+
|
149
|
+
ctx = @models.arel.as_json["ctx"]
|
150
|
+
where_conditions = ctx.wheres
|
151
|
+
|
152
|
+
assert where_conditions.any?
|
153
|
+
|
154
|
+
where_conditions.each do |condition|
|
155
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
156
|
+
assert_kind_of Arel::Nodes::Matches, condition.expr
|
157
|
+
|
158
|
+
assert_kind_of Arel::Attributes::Attribute, condition.expr.left
|
159
|
+
assert_equal :field_2, condition.expr.left.name
|
160
|
+
assert_equal "stri%", condition.expr.right
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_desc_by_result
|
165
|
+
assert_respond_to Model, :desc_by
|
166
|
+
|
167
|
+
@models = Model.desc_by(:field_1)
|
168
|
+
|
169
|
+
assert @models.any?
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_desc_by_condition_array_attrs
|
173
|
+
attrs = [:field_1, :field_2]
|
174
|
+
|
175
|
+
@models = Model.desc_by(attrs)
|
176
|
+
|
177
|
+
arel = @models.arel
|
178
|
+
orders_conditions = arel.orders
|
179
|
+
|
180
|
+
assert orders_conditions.any?
|
181
|
+
|
182
|
+
orders_conditions.each_with_index do |condition, index|
|
183
|
+
assert_kind_of Arel::Nodes::Descending, condition
|
184
|
+
assert_kind_of Arel::Attributes::Attribute, condition.expr
|
185
|
+
|
186
|
+
assert_equal attrs[index], condition.expr.name
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_desc_by_condition
|
191
|
+
@models = Model.desc_by(:field_1)
|
192
|
+
|
193
|
+
arel = @models.arel
|
194
|
+
orders_conditions = arel.orders
|
195
|
+
|
196
|
+
assert orders_conditions.any?
|
197
|
+
|
198
|
+
orders_conditions.each do |condition|
|
199
|
+
assert_kind_of Arel::Nodes::Descending, condition
|
200
|
+
assert_kind_of Arel::Attributes::Attribute, condition.expr
|
201
|
+
|
202
|
+
assert_equal :field_1, condition.expr.name
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_desc_by_incorrect_params
|
207
|
+
begin
|
208
|
+
@models = Model.desc_by("field_1")
|
209
|
+
rescue Exception => e
|
210
|
+
assert_equal "Symbol or Array of symbols is expected", e.message
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_asc_by_result
|
215
|
+
assert_respond_to Model, :asc_by
|
216
|
+
|
217
|
+
@models = Model.asc_by(:field_1)
|
218
|
+
|
219
|
+
assert @models.any?
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_asc_by_condition_array_attrs
|
223
|
+
attrs = [:field_1, :field_2]
|
224
|
+
|
225
|
+
@models = Model.asc_by(attrs)
|
226
|
+
|
227
|
+
arel = @models.arel
|
228
|
+
orders_conditions = arel.orders
|
229
|
+
|
230
|
+
assert orders_conditions.any?
|
231
|
+
|
232
|
+
orders_conditions.each_with_index do |condition, index|
|
233
|
+
assert_kind_of Arel::Nodes::Ascending, condition
|
234
|
+
assert_kind_of Arel::Attributes::Attribute, condition.expr
|
235
|
+
|
236
|
+
assert_equal attrs[index], condition.expr.name
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
def test_asc_by_condition
|
241
|
+
@models = Model.asc_by(:field_1)
|
242
|
+
|
243
|
+
arel = @models.arel
|
244
|
+
orders_conditions = arel.orders
|
245
|
+
|
246
|
+
assert orders_conditions.any?
|
247
|
+
|
248
|
+
orders_conditions.each do |condition|
|
249
|
+
assert_kind_of Arel::Nodes::Ascending, condition
|
250
|
+
assert_kind_of Arel::Attributes::Attribute, condition.expr
|
251
|
+
|
252
|
+
assert_equal :field_1, condition.expr.name
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_asc_by_incorrect_params
|
257
|
+
begin
|
258
|
+
@models = Model.asc_by("field_1")
|
259
|
+
rescue Exception => e
|
260
|
+
assert_equal "Symbol or Array of symbols is expected", e.message
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
def test_field_more_than_value_by_result
|
265
|
+
@model_less = create :model
|
266
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
267
|
+
|
268
|
+
@models = Model.field_1_more(@model_less.field_1)
|
269
|
+
|
270
|
+
assert @models.any?
|
271
|
+
assert @models.include?(@model_more)
|
272
|
+
refute @models.include?(@model_less)
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_field_more_than_object_by_result
|
276
|
+
@model_less = create :model
|
277
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
278
|
+
|
279
|
+
@models = Model.field_1_more(@model_less)
|
280
|
+
|
281
|
+
assert @models.any?
|
282
|
+
assert @models.include?(@model_more)
|
283
|
+
refute @models.include?(@model_less)
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_field_more_by_condition
|
287
|
+
@models = Model.field_1_more(@model.field_1)
|
288
|
+
|
289
|
+
ctx = @models.arel.as_json["ctx"]
|
290
|
+
where_conditions = ctx.wheres
|
291
|
+
|
292
|
+
assert where_conditions.any?
|
293
|
+
|
294
|
+
where_conditions.each do |condition|
|
295
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
296
|
+
assert_kind_of Arel::Nodes::GreaterThan, condition.expr
|
297
|
+
assert_equal @model.field_1, condition.expr.right
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
def test_field_less_than_value_by_result
|
302
|
+
@model_less = create :model
|
303
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
304
|
+
|
305
|
+
@models = Model.field_1_less(@model_more.field_1)
|
306
|
+
|
307
|
+
assert @models.any?
|
308
|
+
assert @models.include?(@model_less)
|
309
|
+
refute @models.include?(@model_more)
|
310
|
+
end
|
311
|
+
|
312
|
+
def test_field_less_than_object_by_result
|
313
|
+
@model_less = create :model
|
314
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
315
|
+
|
316
|
+
@models = Model.field_1_less(@model_more)
|
317
|
+
|
318
|
+
assert @models.any?
|
319
|
+
assert @models.include?(@model_less)
|
320
|
+
refute @models.include?(@model_more)
|
321
|
+
end
|
322
|
+
|
323
|
+
def test_field_less_by_condition
|
324
|
+
@models = Model.field_1_less(@model.field_1)
|
325
|
+
|
326
|
+
ctx = @models.arel.as_json["ctx"]
|
327
|
+
where_conditions = ctx.wheres
|
328
|
+
|
329
|
+
assert where_conditions.any?
|
330
|
+
|
331
|
+
where_conditions.each do |condition|
|
332
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
333
|
+
assert_kind_of Arel::Nodes::LessThan, condition.expr
|
334
|
+
assert_equal @model.field_1, condition.expr.right
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
def test_field_more_or_equal_than_value_by_result
|
339
|
+
@model_less = create :model
|
340
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
341
|
+
|
342
|
+
@models = Model.field_1_more_or_equal(@model_more.field_1)
|
343
|
+
|
344
|
+
assert @models.any?
|
345
|
+
assert @models.include?(@model_more)
|
346
|
+
refute @models.include?(@model_less)
|
347
|
+
end
|
348
|
+
|
349
|
+
def test_field_more_or_equal_than_object_by_result
|
350
|
+
@model_less = create :model
|
351
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
352
|
+
|
353
|
+
@models = Model.field_1_more_or_equal(@model_more)
|
354
|
+
|
355
|
+
assert @models.any?
|
356
|
+
assert @models.include?(@model_more)
|
357
|
+
refute @models.include?(@model_less)
|
358
|
+
end
|
359
|
+
|
360
|
+
def test_field_more_or_equal_by_condition
|
361
|
+
@models = Model.field_1_more_or_equal(@model.field_1)
|
362
|
+
|
363
|
+
ctx = @models.arel.as_json["ctx"]
|
364
|
+
where_conditions = ctx.wheres
|
365
|
+
|
366
|
+
assert where_conditions.any?
|
367
|
+
|
368
|
+
where_conditions.each do |condition|
|
369
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
370
|
+
assert_kind_of Arel::Nodes::GreaterThanOrEqual, condition.expr
|
371
|
+
assert_equal @model.field_1, condition.expr.right
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
def test_field_less_or_equal_than_value_by_result
|
376
|
+
@model_less = create :model
|
377
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
378
|
+
|
379
|
+
@models = Model.field_1_less_or_equal(@model_less.field_1)
|
380
|
+
|
381
|
+
assert @models.any?
|
382
|
+
assert @models.include?(@model_less)
|
383
|
+
refute @models.include?(@model_more)
|
384
|
+
end
|
385
|
+
|
386
|
+
def test_field_less_or_equal_than_object_by_result
|
387
|
+
@model_less = create :model
|
388
|
+
@model_more = create :model, field_1: @model_less.field_1 + 1
|
389
|
+
|
390
|
+
@models = Model.field_1_less_or_equal(@model_less)
|
391
|
+
|
392
|
+
assert @models.any?
|
393
|
+
assert @models.include?(@model_less)
|
394
|
+
refute @models.include?(@model_more)
|
395
|
+
end
|
396
|
+
def test_field_less_or_equal_by_condition
|
397
|
+
@models = Model.field_1_less_or_equal(@model.field_1)
|
398
|
+
|
399
|
+
ctx = @models.arel.as_json["ctx"]
|
400
|
+
where_conditions = ctx.wheres
|
401
|
+
|
402
|
+
assert where_conditions.any?
|
403
|
+
|
404
|
+
where_conditions.each do |condition|
|
405
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
406
|
+
assert_kind_of Arel::Nodes::LessThanOrEqual, condition.expr
|
407
|
+
assert_equal @model.field_1, condition.expr.right
|
408
|
+
end
|
409
|
+
end
|
410
|
+
|
411
|
+
def test_more_than_result
|
412
|
+
@model = Model.last
|
413
|
+
|
414
|
+
@models = Model.more_than({field_1: 1})
|
415
|
+
|
416
|
+
assert @models.any?
|
417
|
+
assert @models.include?(@model)
|
418
|
+
end
|
419
|
+
|
420
|
+
def test_more_than_condition_value
|
421
|
+
@models = Model.more_than({field_1: 1})
|
422
|
+
|
423
|
+
ctx = @models.arel.as_json["ctx"]
|
424
|
+
where_conditions = ctx.wheres
|
425
|
+
|
426
|
+
assert where_conditions.any?
|
427
|
+
|
428
|
+
where_conditions.each do |condition|
|
429
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
430
|
+
condition.expr.children.each do |condition_part|
|
431
|
+
assert_kind_of Arel::Nodes::GreaterThan, condition_part
|
432
|
+
|
433
|
+
assert_kind_of Arel::Attributes::Attribute, condition_part.left
|
434
|
+
|
435
|
+
assert_equal :field_1, condition_part.left.name
|
436
|
+
assert_equal 1, condition_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
|
+
ctx = @models.arel.as_json["ctx"]
|
445
|
+
where_conditions = ctx.wheres
|
446
|
+
|
447
|
+
assert where_conditions.any?
|
448
|
+
|
449
|
+
where_conditions.each do |condition|
|
450
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
451
|
+
condition.expr.children.each do |condition_part|
|
452
|
+
assert_kind_of Arel::Nodes::GreaterThan, condition_part
|
453
|
+
|
454
|
+
assert_kind_of Arel::Attributes::Attribute, condition_part.left
|
455
|
+
|
456
|
+
assert_equal :id, condition_part.left.name
|
457
|
+
assert_equal 1, condition_part.right
|
458
|
+
end
|
459
|
+
end
|
460
|
+
end
|
461
|
+
|
462
|
+
def test_more_than_incorrect_params
|
463
|
+
begin
|
464
|
+
@models = Model.more_than("field_1")
|
465
|
+
rescue Exception => e
|
466
|
+
assert_equal "Hash or AR object is expected", e.message
|
467
|
+
end
|
468
|
+
end
|
469
|
+
|
470
|
+
def test_less_than_result
|
471
|
+
@model_first = Model.first
|
472
|
+
@model_last = Model.last
|
473
|
+
|
474
|
+
@models = Model.less_than({field_1: @model_last.field_1})
|
475
|
+
|
476
|
+
assert @models.any?
|
477
|
+
assert @models.include?(@model_first)
|
478
|
+
end
|
479
|
+
|
480
|
+
def test_less_than_condition_value
|
481
|
+
@models = Model.less_than({field_1: 1})
|
482
|
+
|
483
|
+
ctx = @models.arel.as_json["ctx"]
|
484
|
+
where_conditions = ctx.wheres
|
485
|
+
|
486
|
+
assert where_conditions.any?
|
487
|
+
|
488
|
+
where_conditions.each do |condition|
|
489
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
490
|
+
condition.expr.children.each do |condition_part|
|
491
|
+
assert_kind_of Arel::Nodes::LessThan, condition_part
|
492
|
+
|
493
|
+
assert_kind_of Arel::Attributes::Attribute, condition_part.left
|
494
|
+
|
495
|
+
assert_equal :field_1, condition_part.left.name
|
496
|
+
assert_equal 1, condition_part.right
|
497
|
+
end
|
498
|
+
end
|
499
|
+
end
|
500
|
+
|
501
|
+
def test_less_than_condition_ar_object
|
502
|
+
@models = Model.less_than(@model)
|
503
|
+
|
504
|
+
ctx = @models.arel.as_json["ctx"]
|
505
|
+
where_conditions = ctx.wheres
|
506
|
+
|
507
|
+
assert where_conditions.any?
|
508
|
+
|
509
|
+
where_conditions.each do |condition|
|
510
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
511
|
+
condition.expr.children.each do |condition_part|
|
512
|
+
assert_kind_of Arel::Nodes::LessThan, condition_part
|
513
|
+
|
514
|
+
assert_kind_of Arel::Attributes::Attribute, condition_part.left
|
515
|
+
|
516
|
+
assert_equal :id, condition_part.left.name
|
517
|
+
assert_equal 1, condition_part.right
|
518
|
+
end
|
519
|
+
end
|
520
|
+
end
|
521
|
+
|
522
|
+
def test_less_than_incorrect_params
|
523
|
+
begin
|
524
|
+
@models = Model.less_than("field_1")
|
525
|
+
rescue Exception => e
|
526
|
+
assert_equal "Hash or AR object is expected", e.message
|
527
|
+
end
|
528
|
+
end
|
529
|
+
|
530
|
+
def test_more_or_equal_result
|
531
|
+
@model_first = Model.first
|
532
|
+
@model_last = Model.last
|
533
|
+
|
534
|
+
@models = Model.more_or_equal({field_1: @model_last.field_1})
|
535
|
+
|
536
|
+
assert @models.any?
|
537
|
+
assert @models.include?(@model_last)
|
538
|
+
assert_equal @models.count, 1
|
539
|
+
end
|
540
|
+
|
541
|
+
def test_more_or_equal_condition_value
|
542
|
+
@models = Model.more_or_equal({field_1: 1})
|
543
|
+
|
544
|
+
ctx = @models.arel.as_json["ctx"]
|
545
|
+
where_conditions = ctx.wheres
|
546
|
+
|
547
|
+
assert where_conditions.any?
|
548
|
+
|
549
|
+
where_conditions.each do |condition|
|
550
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
551
|
+
condition.expr.children.each do |condition_part|
|
552
|
+
assert_kind_of Arel::Nodes::GreaterThanOrEqual, condition_part
|
553
|
+
|
554
|
+
assert_kind_of Arel::Attributes::Attribute, condition_part.left
|
555
|
+
|
556
|
+
assert_equal :field_1, condition_part.left.name
|
557
|
+
assert_equal 1, condition_part.right
|
558
|
+
end
|
559
|
+
end
|
560
|
+
end
|
561
|
+
|
562
|
+
def test_more_or_equal_condition_ar_object
|
563
|
+
@models = Model.more_or_equal(@model)
|
564
|
+
|
565
|
+
ctx = @models.arel.as_json["ctx"]
|
566
|
+
where_conditions = ctx.wheres
|
567
|
+
|
568
|
+
assert where_conditions.any?
|
569
|
+
|
570
|
+
where_conditions.each do |condition|
|
571
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
572
|
+
condition.expr.children.each do |condition_part|
|
573
|
+
assert_kind_of Arel::Nodes::GreaterThanOrEqual, condition_part
|
574
|
+
|
575
|
+
assert_kind_of Arel::Attributes::Attribute, condition_part.left
|
576
|
+
|
577
|
+
assert_equal :id, condition_part.left.name
|
578
|
+
assert_equal 1, condition_part.right
|
579
|
+
end
|
580
|
+
end
|
581
|
+
end
|
582
|
+
|
583
|
+
def test_more_or_equal_incorrect_params
|
584
|
+
begin
|
585
|
+
@models = Model.more_or_equal("field_1")
|
586
|
+
rescue Exception => e
|
587
|
+
assert_equal "Hash or AR object is expected", e.message
|
588
|
+
end
|
589
|
+
end
|
590
|
+
|
591
|
+
def test_less_or_equal_result
|
592
|
+
@model_first = Model.first
|
593
|
+
@model_last = Model.last
|
594
|
+
|
595
|
+
@models = Model.less_or_equal({field_1: @model_first.field_1})
|
596
|
+
|
597
|
+
assert @models.any?
|
598
|
+
assert @models.include?(@model_first)
|
599
|
+
assert_equal @models.count, 1
|
600
|
+
end
|
601
|
+
|
602
|
+
def test_less_or_equal_condition_value
|
603
|
+
@models = Model.less_or_equal({field_1: 1})
|
604
|
+
|
605
|
+
ctx = @models.arel.as_json["ctx"]
|
606
|
+
where_conditions = ctx.wheres
|
607
|
+
|
608
|
+
assert where_conditions.any?
|
609
|
+
|
610
|
+
where_conditions.each do |condition|
|
611
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
612
|
+
condition.expr.children.each do |condition_part|
|
613
|
+
assert_kind_of Arel::Nodes::LessThanOrEqual, condition_part
|
614
|
+
|
615
|
+
assert_kind_of Arel::Attributes::Attribute, condition_part.left
|
616
|
+
|
617
|
+
assert_equal :field_1, condition_part.left.name
|
618
|
+
assert_equal 1, condition_part.right
|
619
|
+
end
|
620
|
+
end
|
621
|
+
end
|
622
|
+
|
623
|
+
def test_less_or_equal_condition_ar_object
|
624
|
+
@models = Model.less_or_equal(@model)
|
625
|
+
|
626
|
+
ctx = @models.arel.as_json["ctx"]
|
627
|
+
where_conditions = ctx.wheres
|
628
|
+
|
629
|
+
assert where_conditions.any?
|
630
|
+
|
631
|
+
where_conditions.each do |condition|
|
632
|
+
assert_kind_of Arel::Nodes::Grouping, condition
|
633
|
+
condition.expr.children.each do |condition_part|
|
634
|
+
assert_kind_of Arel::Nodes::LessThanOrEqual, condition_part
|
635
|
+
|
636
|
+
assert_kind_of Arel::Attributes::Attribute, condition_part.left
|
637
|
+
|
638
|
+
assert_equal :id, condition_part.left.name
|
639
|
+
assert_equal 1, condition_part.right
|
640
|
+
end
|
641
|
+
end
|
642
|
+
end
|
643
|
+
|
644
|
+
def test_less_or_equal_incorrect_params
|
645
|
+
begin
|
646
|
+
@models = Model.less_or_equal("field_1")
|
647
|
+
rescue Exception => e
|
648
|
+
assert_equal "Hash or AR object is expected", e.message
|
649
|
+
end
|
650
|
+
end
|
651
|
+
end
|
652
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'active_record'
|
3
|
+
require 'simplecov'
|
4
|
+
require 'coveralls'
|
5
|
+
Coveralls.wear!
|
6
|
+
|
7
|
+
ENV["COVERAGE"] = "true"
|
8
|
+
SimpleCov.start if ENV["COVERAGE"]
|
9
|
+
|
10
|
+
Bundler.require
|
11
|
+
|
12
|
+
MiniTest::Unit.autorun
|
13
|
+
|
14
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
15
|
+
|
16
|
+
ActiveRecord::Migration.create_table :models do |t|
|
17
|
+
t.integer :field_1
|
18
|
+
t.string :field_2
|
19
|
+
t.boolean :field_3
|
20
|
+
t.text :field_4
|
21
|
+
t.timestamps
|
22
|
+
end
|
23
|
+
|
24
|
+
class Model < ActiveRecord::Base
|
25
|
+
include UsefullScopes
|
26
|
+
end
|
27
|
+
|
28
|
+
class TestCase < MiniTest::Unit::TestCase
|
29
|
+
def load_fixture(filename)
|
30
|
+
File.read(File.dirname(__FILE__) + "/fixtures/#{filename}")
|
31
|
+
end
|
32
|
+
|
33
|
+
require 'factory_girl'
|
34
|
+
FactoryGirl.reload
|
35
|
+
|
36
|
+
include FactoryGirl::Syntax::Methods
|
37
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: usefull_scopes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-02-11 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: This gem provides additional scopes for your Active:Record models within
|
15
15
|
Ruby on Rails framework.
|
@@ -19,6 +19,7 @@ executables: []
|
|
19
19
|
extensions: []
|
20
20
|
extra_rdoc_files: []
|
21
21
|
files:
|
22
|
+
- .coveralls.yml
|
22
23
|
- .gitignore
|
23
24
|
- Gemfile
|
24
25
|
- LICENSE
|
@@ -26,6 +27,10 @@ files:
|
|
26
27
|
- Rakefile
|
27
28
|
- lib/usefull_scopes.rb
|
28
29
|
- lib/usefull_scopes/version.rb
|
30
|
+
- test/factories/models.rb
|
31
|
+
- test/factories/sequences.rb
|
32
|
+
- test/lib/usefull_scopes_test.rb
|
33
|
+
- test/test_helper.rb
|
29
34
|
- usefull_scopes.gemspec
|
30
35
|
homepage: http://github.com/kaize/usefull_scopes
|
31
36
|
licenses: []
|
@@ -51,5 +56,8 @@ rubygems_version: 1.8.24
|
|
51
56
|
signing_key:
|
52
57
|
specification_version: 3
|
53
58
|
summary: Additional scopes for Active:Record models.
|
54
|
-
test_files:
|
55
|
-
|
59
|
+
test_files:
|
60
|
+
- test/factories/models.rb
|
61
|
+
- test/factories/sequences.rb
|
62
|
+
- test/lib/usefull_scopes_test.rb
|
63
|
+
- test/test_helper.rb
|