mongoid-report 0.0.11 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/Gemfile.lock +1 -1
- data/README.md +6 -0
- data/lib/mongoid/report.rb +18 -1
- data/lib/mongoid/report/attach_proxy.rb +7 -0
- data/lib/mongoid/report/version.rb +1 -1
- data/spec/mongoid/report/aggregation_spec.rb +72 -0
- data/spec/mongoid/report_spec.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
Y2Y4YTU4MzMzZmQ0MmRmYzNhYTMwZWM5MDViNDFhOTExMDJlYWMxNg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YzNkMDcxYjU1NjQ2ZGI5MjAwYzk2OGFlYWYzOGNmOTQyYjMzMjFiMg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YjY5OTdjZGY5ODQ4MDRmMGZlMTVlMzUwMjM4NWQzOGMwZDAzODE3OWNiNGRk
|
10
|
+
MzBkMDMwOTZiMTgyZmYxZmZjZjliMmEwOTZjNTY1YjYyYzBhODk4YjE2ZGRh
|
11
|
+
NjZlYTAyNDRiNjA1NzE0MzY3MmUwZGQ5YTE0NTY0M2YxMTU5NzQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
M2FlNTYzNzJmOTZmNmY2ZTNmNDc3MTA3ODg2NzliNjNjMTFjZDA5MDVmZThj
|
14
|
+
YTAyZDY2ZTUyNWIzOGFjMGY5M2JiMjEzNTQ4ZTIwZmY1ZTUxZDY4NzA1NzAz
|
15
|
+
MDRlYjJlZTQ3ODViMTc1MGI1YzBhYWZhMjQ3YTU3YzBhYjQxNmE=
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
data/lib/mongoid/report.rb
CHANGED
@@ -29,7 +29,7 @@ module Mongoid
|
|
29
29
|
|
30
30
|
# Now we have access to compiled queries to run it in aggregation
|
31
31
|
# framework.
|
32
|
-
configuration[:queries]
|
32
|
+
configuration[:queries].concat(@queries)
|
33
33
|
end
|
34
34
|
end
|
35
35
|
alias :initialize :initialize_report_module
|
@@ -59,6 +59,20 @@ module Mongoid
|
|
59
59
|
proxy.instance_eval(&block)
|
60
60
|
end
|
61
61
|
|
62
|
+
def filter(*fields)
|
63
|
+
define_report_method(*fields) do |_, report_name, options|
|
64
|
+
queries = self.settings_property(report_name, :queries)
|
65
|
+
|
66
|
+
options.each do |key, value|
|
67
|
+
value = value.call if value.respond_to?(:call)
|
68
|
+
queries
|
69
|
+
.concat([{
|
70
|
+
'$match' => { key => value }
|
71
|
+
}])
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
62
76
|
def group_by(*fields)
|
63
77
|
define_report_method(*fields) do |groups, report_name, _|
|
64
78
|
settings[report_name][:group_by] = groups
|
@@ -95,10 +109,12 @@ module Mongoid
|
|
95
109
|
|
96
110
|
# We should always specify model to attach fields, groups
|
97
111
|
collection = options.fetch(:for)
|
112
|
+
options.delete(:for)
|
98
113
|
|
99
114
|
# If user didn't pass as option to name the report we are using
|
100
115
|
# collection class as key for settings.
|
101
116
|
attach_name = options.fetch(:attach_name) { collection }
|
117
|
+
options.delete(:attach_name)
|
102
118
|
|
103
119
|
# We should always have for option
|
104
120
|
initialize_settings_by(attach_name, collection)
|
@@ -114,6 +130,7 @@ module Mongoid
|
|
114
130
|
for: collection,
|
115
131
|
fields: {},
|
116
132
|
group_by: [],
|
133
|
+
queries: [],
|
117
134
|
}
|
118
135
|
end
|
119
136
|
end
|
@@ -213,4 +213,76 @@ describe Mongoid::Report do
|
|
213
213
|
end
|
214
214
|
end
|
215
215
|
|
216
|
+
class Report15
|
217
|
+
include Mongoid::Report
|
218
|
+
|
219
|
+
filter field2: 2, for: Model
|
220
|
+
aggregation_field :field1, for: Model
|
221
|
+
end
|
222
|
+
|
223
|
+
describe '.filter' do
|
224
|
+
it 'creates filter' do
|
225
|
+
klass.create(field1: 1, field2: 2)
|
226
|
+
klass.create(field1: 3, field2: 4)
|
227
|
+
|
228
|
+
example = Report15.new
|
229
|
+
scope = example.aggregate
|
230
|
+
scope = scope.all
|
231
|
+
|
232
|
+
rows = scope[Model]
|
233
|
+
expect(rows.size).to eq(1)
|
234
|
+
expect(rows[0]['field1']).to eq(1)
|
235
|
+
end
|
236
|
+
|
237
|
+
class Report16
|
238
|
+
include Mongoid::Report
|
239
|
+
|
240
|
+
report 'example' do
|
241
|
+
attach_to Model do
|
242
|
+
filter field2: 2
|
243
|
+
aggregation_field :field1
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
it 'creates filter in report scope' do
|
249
|
+
klass.create(field1: 1, field2: 2)
|
250
|
+
klass.create(field1: 3, field2: 4)
|
251
|
+
|
252
|
+
example = Report16.new
|
253
|
+
scope = example.aggregate
|
254
|
+
scope = scope.all
|
255
|
+
|
256
|
+
rows = scope['example-models']
|
257
|
+
expect(rows.size).to eq(1)
|
258
|
+
expect(rows[0]['field1']).to eq(1)
|
259
|
+
end
|
260
|
+
|
261
|
+
class Report17
|
262
|
+
include Mongoid::Report
|
263
|
+
|
264
|
+
report 'example' do
|
265
|
+
attach_to Model do
|
266
|
+
filter field2: 2,
|
267
|
+
day: -> { Date.parse("20-12-2004").mongoize }
|
268
|
+
aggregation_field :field1
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
it 'creates filter in report scope' do
|
274
|
+
klass.create(day: today , field1: 1 , field2: 2)
|
275
|
+
klass.create(day: yesterday , field1: 1 , field2: 2)
|
276
|
+
klass.create(day: today , field1: 3 , field2: 4)
|
277
|
+
|
278
|
+
example = Report17.new
|
279
|
+
scope = example.aggregate
|
280
|
+
scope = scope.all
|
281
|
+
|
282
|
+
rows = scope['example-models']
|
283
|
+
expect(rows.size).to eq(1)
|
284
|
+
expect(rows[0]['field1']).to eq(1)
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
216
288
|
end
|
data/spec/mongoid/report_spec.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-report
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexandr Korsak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|