mongoid-report 0.1.7 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDE4ZGZlZmJmOWFmMDJkNjY5Yjk3YTRmY2FlMjMwZGVlOTQzNzY4Nw==
4
+ NWZhMzhiNGQzZDgxMTdlMjlhZGU2MTUyYTE5YzRjOGJmYTRhYTFjMw==
5
5
  data.tar.gz: !binary |-
6
- MTZkNzhlMTkzNTQxZWY0ZmNlMzc4YmQzM2IxMmIyZjJlOWE1ZGEwNA==
6
+ NWEzNzVjYzY1M2IwOTgxZmNmZTc1ZDY3N2VmNjY3NTMwZjFmZWMzYw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MDU4NDgwNDc2ZmM2MjRiMmFmYzkxZmM2ODBkNWRjNzY2NDIxOTE3YjQzNjBh
10
- YmJkNDMxYjgyZWQ5NTRkNzJlNGUwZDdmYjU3MjQzZTc2ZjkzMTMwMTAyZDQw
11
- OTYwZmJiYzc3Y2FjYjAzZGNhZDNkODA0M2U0NjNhYTY4NmU4NmM=
9
+ Njc2NWE1OWIwYjE5NzJkNmJhOTYxYjM0MjNjMjMyMjkyMjMyZWM5ODVhZjc2
10
+ M2YzMDU3MmE3ZjI2MjFiNmVjMDE3MzA2ODIzMjIwOThlOThmOTViYjIxMmU4
11
+ MzY1Zjc1NjczYTk3NzViZDQyNGEzNjVhZTI2OTUzMzZlYjM1ZDU=
12
12
  data.tar.gz: !binary |-
13
- MTNiMjcyZGZmMzZkNTIzZTI4MGVjZGQ2OTY4ZDBjMTY1ODAwOWVlNDI3MjM4
14
- ODVmMmQwNTUxMDNhOWY0M2NjOWNlOWY0MTBlYTVjNTkyNWQ3YTc1NTFkOTU5
15
- NWE3OTBmNGU3MTNjOWJjYzUxYjVkNjU2OGI5ZmE4NjNiYTA0MjY=
13
+ YjgyMjcyMWFmZjU2YzVmNGE3MzRjNTQ1ZTZhODQ4M2I3Y2RmOGQ1M2NiZjg4
14
+ YmY1Y2UzM2U3ZTI3MDlmODdkMmM3ODAyODVkNzVmY2EyNjA0NzZlNjM1MWVk
15
+ ODYwM2Y1OWI5YTlhOTM5ZmY1YmMxOWE2MWFjNGMwNzMzZTE2Y2Q=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongoid-report (0.1.7)
4
+ mongoid-report (0.1.9)
5
5
  mongoid (> 3.0.1)
6
6
 
7
7
  GEM
@@ -4,30 +4,25 @@ module Mongoid
4
4
  module Report
5
5
 
6
6
  class Collection < SimpleDelegator
7
- def initialize(context, rows, fields, columns)
7
+ def initialize(context, rows, fields, columns, mapping)
8
8
  @context = context
9
9
  @rows = rows
10
10
  @fields = fields
11
11
  @columns = columns
12
- @rows = compile_rows
13
-
14
- # Collection should behave like Array using delegator method.
15
- super(@rows)
12
+ @mapping = mapping
13
+ @rows = Rows.new(compile_rows)
16
14
  end
17
15
 
18
- def compile_rows
19
- @rows.map do |row|
20
- @columns.each do |name, function|
21
- next unless @fields.include?(name)
22
- row[name] = function.call(@context, row, { summary: false })
23
- end
16
+ class Rows < SimpleDelegator ; end
24
17
 
25
- row
26
- end
18
+ attr_reader :rows
19
+
20
+ def headers
21
+ @fields
27
22
  end
28
23
 
29
24
  def summary
30
- @summary ||= reduce(Hash.new{|h, k| h[k] = 0}) do |summary, row|
25
+ @summary ||= @rows.reduce(Hash.new{|h, k| h[k] = 0}) do |summary, row|
31
26
  # Find summary for aggregated rows
32
27
  @fields.each do |field|
33
28
  # Don't apply for dynamic calculated columns lets wait until we get
@@ -40,13 +35,26 @@ module Mongoid
40
35
  # Apply dynamic columns for summarized row
41
36
  @columns.each do |name, function|
42
37
  next unless @fields.include?(name)
43
- summary[name] = function.call(@context, row, { summary: true })
38
+ summary[name] = function.call(@context, row, { mapping: @mapping, summary: true })
44
39
  end
45
40
 
46
41
  summary
47
42
  end
48
43
  end
49
44
 
45
+ private
46
+
47
+ def compile_rows
48
+ @rows.map do |row|
49
+ @columns.each do |name, function|
50
+ next unless @fields.include?(name)
51
+ row[name] = function.call(@context, row, { mapping: @mapping, summary: false })
52
+ end
53
+
54
+ row
55
+ end
56
+ end
57
+
50
58
  end
51
59
 
52
60
  end
@@ -23,7 +23,7 @@ module Mongoid
23
23
  aggregation_queries = compile_queries
24
24
  rows = klass.collection.aggregate(aggregation_queries)
25
25
 
26
- Collection.new(context, rows, fields, columns)
26
+ Collection.new(context, rows, fields, columns, mapping)
27
27
  end
28
28
 
29
29
  private
@@ -69,6 +69,10 @@ module Mongoid
69
69
  def columns
70
70
  context.report_module_settings[report_name][:columns]
71
71
  end
72
+
73
+ def mapping
74
+ context.report_module_settings[report_name][:mapping]
75
+ end
72
76
  end
73
77
 
74
78
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Report
3
- VERSION = "0.1.7"
3
+ VERSION = "0.1.9"
4
4
  end
5
5
  end
@@ -21,8 +21,9 @@ describe Mongoid::Report do
21
21
  klass.create!(field1: 1)
22
22
 
23
23
  example = Report.new
24
- rows = example.aggregate_for(klass)
25
- rows = rows.all
24
+ report = example.aggregate_for(klass)
25
+ report = report.all
26
+ rows = report.rows
26
27
 
27
28
  expect(rows.size).to eq(1)
28
29
  expect(rows[0]['field1']).to eq(3)
@@ -40,8 +41,10 @@ describe Mongoid::Report do
40
41
  end
41
42
  example = Report.new
42
43
 
43
- rows = example.aggregate_for(klass)
44
- rows = rows.all
44
+ report = example.aggregate_for(klass)
45
+ report = report.all
46
+
47
+ rows = report.rows
45
48
 
46
49
  expect(rows.size).to eq(2)
47
50
  expect(rows[0]['field1']).to eq(1)
@@ -70,7 +73,9 @@ describe Mongoid::Report do
70
73
  scope = scope.yield
71
74
  scope = scope.query('$sort' => { day: -1 })
72
75
 
73
- rows = scope.all
76
+ scope = scope.all
77
+
78
+ rows = scope.rows
74
79
 
75
80
  expect(rows.size).to eq(2)
76
81
  expect(rows[0]['field1']).to eq(2)
@@ -93,7 +98,9 @@ describe Mongoid::Report do
93
98
  scope = scope.query()
94
99
  scope = scope.query({})
95
100
 
96
- rows = scope.all
101
+ scope = scope.all
102
+
103
+ rows = scope.rows
97
104
 
98
105
  expect(rows.size).to eq(1)
99
106
  expect(rows[0]['field1']).to eq(1)
@@ -130,14 +137,14 @@ describe Mongoid::Report do
130
137
  .query('$sort' => { day: -1 })
131
138
  scope = scope.all
132
139
 
133
- rows = scope['example1']
140
+ rows = scope['example1'].rows
134
141
  expect(rows.size).to eq(2)
135
142
  expect(rows[0]['field1']).to eq(2)
136
143
  expect(rows[0]['day']).to eq(today)
137
144
  expect(rows[1]['field1']).to eq(1)
138
145
  expect(rows[1]['day']).to eq(yesterday)
139
146
 
140
- rows = scope['example2']
147
+ rows = scope['example2'].rows
141
148
  expect(rows.size).to eq(2)
142
149
  expect(rows[0]['field2']).to eq(4)
143
150
  expect(rows[0]['day']).to eq(today)
@@ -175,14 +182,14 @@ describe Mongoid::Report do
175
182
  .query('$sort' => { day: -1 })
176
183
  scope = scope.all
177
184
 
178
- rows = scope['example-model1']
185
+ rows = scope['example-model1'].rows
179
186
  expect(rows.size).to eq(2)
180
187
  expect(rows[0]['field1']).to eq(2)
181
188
  expect(rows[0]['day']).to eq(today)
182
189
  expect(rows[1]['field1']).to eq(1)
183
190
  expect(rows[1]['day']).to eq(yesterday)
184
191
 
185
- rows = scope['example-model2']
192
+ rows = scope['example-model2'].rows
186
193
  expect(rows.size).to eq(2)
187
194
  expect(rows[0]['field2']).to eq(4)
188
195
  expect(rows[0]['day']).to eq(today)
@@ -220,14 +227,14 @@ describe Mongoid::Report do
220
227
  .query('$sort' => { day: -1 })
221
228
  scope = scope.all
222
229
 
223
- rows = scope['example-model1']
230
+ rows = scope['example-model1'].rows
224
231
  expect(rows.size).to eq(2)
225
232
  expect(rows[0]['new-field1']).to eq(2)
226
233
  expect(rows[0]['day']).to eq(today)
227
234
  expect(rows[1]['new-field1']).to eq(1)
228
235
  expect(rows[1]['day']).to eq(yesterday)
229
236
 
230
- rows = scope['example-model2']
237
+ rows = scope['example-model2'].rows
231
238
  expect(rows.size).to eq(2)
232
239
  expect(rows[0]['field2']).to eq(4)
233
240
  expect(rows[0]['day']).to eq(today)
@@ -252,7 +259,7 @@ describe Mongoid::Report do
252
259
  scope = example.aggregate
253
260
  scope = scope.all
254
261
 
255
- rows = scope[Model]
262
+ rows = scope[Model].rows
256
263
  expect(rows.size).to eq(1)
257
264
  expect(rows[0]['field1']).to eq(1)
258
265
  end
@@ -276,7 +283,7 @@ describe Mongoid::Report do
276
283
  scope = example.aggregate
277
284
  scope = scope.all
278
285
 
279
- rows = scope['example-models']
286
+ rows = scope['example-models'].rows
280
287
  expect(rows.size).to eq(1)
281
288
  expect(rows[0]['field1']).to eq(1)
282
289
  end
@@ -302,7 +309,7 @@ describe Mongoid::Report do
302
309
  scope = example.aggregate
303
310
  scope = scope.all
304
311
 
305
- rows = scope['example-models']
312
+ rows = scope['example-models'].rows
306
313
  expect(rows.size).to eq(1)
307
314
  expect(rows[0]['field1']).to eq(1)
308
315
  end
@@ -333,7 +340,7 @@ describe Mongoid::Report do
333
340
  scope = example.aggregate
334
341
  scope = scope.all
335
342
 
336
- rows = scope['example-models']
343
+ rows = scope['example-models'].rows
337
344
  expect(rows.size).to eq(2)
338
345
  expect(rows[0]['field1']).to eq(1)
339
346
  expect(rows[1]['field1']).to eq(2)
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Report::Collection do
4
+ let(:klass) { Model }
5
+
6
+ describe '.rows' do
7
+ it 'use returns aggregated rows' do
8
+ Report = Class.new do
9
+ include Mongoid::Report
10
+
11
+ attach_to Model do
12
+ column :field1
13
+ end
14
+ end
15
+
16
+ 3.times { klass.create!(field1: 1) }
17
+
18
+ example = Report.new
19
+ report = example.aggregate_for(klass)
20
+ report = report.all
21
+
22
+ rows = report.rows
23
+ expect(rows.size).to eq(1)
24
+ expect(rows[0]['field1']).to eq(3)
25
+ end
26
+ end
27
+
28
+ describe '.headers' do
29
+ it 'returns columns for showing in the reports' do
30
+ Report = Class.new do
31
+ include Mongoid::Report
32
+
33
+ attach_to Model do
34
+ column :field1, :field3, :field2
35
+ end
36
+ end
37
+
38
+ report = Report.new
39
+ report = report
40
+ .aggregate_for(klass)
41
+ .all
42
+
43
+ expect(report.headers).to eq(["field1", "field3", "field2"])
44
+ end
45
+ end
46
+ end
@@ -24,11 +24,11 @@ describe Mongoid::Report do
24
24
  klass.create(day: yesterday , field1: 1)
25
25
  klass.create(day: today , field1: 1)
26
26
 
27
- example = report_klass.new
28
- scope = example.aggregate
27
+ report = report_klass.new
28
+ scope = report.aggregate
29
29
  scope = scope.all
30
30
 
31
- rows = scope['example-models']
31
+ rows = scope['example-models'].rows
32
32
  expect(rows.size).to eq(2)
33
33
  expect(rows[0]['field1']).to eq(1)
34
34
  expect(rows[0]['dynamic-field1']).to eq(10)
@@ -11,17 +11,19 @@ describe Mongoid::Report do
11
11
 
12
12
  report 'example' do
13
13
  attach_to Model do
14
- columns :'new-field' => ->(context, row, options) { row[context.mapping('example-models')['field3'].to_s] }
14
+ columns :'new-field' => ->(context, row, options) { row[options[:mapping]['field3'].to_s] }
15
15
  mapping :'field3' => :field1
16
16
 
17
17
  column :field1, :'new-field'
18
18
  end
19
19
  end
20
20
  end
21
- example = Report.new
22
21
 
23
- rows = example.aggregate_for('example-models')
24
- rows = rows.all
22
+ report = Report.new
23
+ report = report.aggregate_for('example-models')
24
+ report = report.all
25
+
26
+ rows = report.rows
25
27
 
26
28
  expect(rows.size).to eq(1)
27
29
  expect(rows[0]['field1']).to eq(2)
@@ -19,11 +19,12 @@ describe Mongoid::Report do
19
19
  end
20
20
  example = Report.new
21
21
 
22
- rows = example.aggregate_for(klass)
23
- rows = rows.all
22
+ report = example.aggregate_for(klass)
23
+ report = report.all
24
+ rows = report.rows
24
25
 
25
26
  expect(rows.count).to eq(2)
26
- expect(rows.summary['field1']).to eq(3)
27
+ expect(report.summary['field1']).to eq(3)
27
28
  end
28
29
 
29
30
  it 'should support dynamic columns as well' do
@@ -47,17 +48,18 @@ describe Mongoid::Report do
47
48
  klass.create!(field1: 1)
48
49
  klass.create!(field1: 1)
49
50
 
50
- example = Report.new
51
- rows = example.aggregate_for('example-models')
52
- rows = rows.all
51
+ report = Report.new
52
+ report = report.aggregate_for('example-models')
53
+ report = report.all
54
+ rows = report.rows
53
55
 
54
56
  expect(rows[0].keys.size).to eq(2)
55
57
  expect(rows[0]['field1']).to eq(3)
56
58
  expect(rows[0]['new-field1']).to eq(30)
57
59
 
58
- expect(rows.summary.keys.size).to eq(2)
59
- expect(rows.summary['field1']).to eq(3)
60
- expect(rows.summary['new-field1']).to eq(30)
60
+ expect(report.summary.keys.size).to eq(2)
61
+ expect(report.summary['field1']).to eq(3)
62
+ expect(report.summary['new-field1']).to eq(30)
61
63
  end
62
64
  end
63
65
 
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.1.7
4
+ version: 0.1.9
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-07-02 00:00:00.000000000 Z
11
+ date: 2014-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -82,6 +82,7 @@ files:
82
82
  - lib/mongoid/report/version.rb
83
83
  - mongoid-report.gemspec
84
84
  - spec/mongoid/report/aggregation_spec.rb
85
+ - spec/mongoid/report/collection_spec.rb
85
86
  - spec/mongoid/report/column_spec.rb
86
87
  - spec/mongoid/report/config_spec.rb
87
88
  - spec/mongoid/report/queries_builder_spec.rb
@@ -117,6 +118,7 @@ specification_version: 4
117
118
  summary: Easily build mongoid reports using aggregation framework
118
119
  test_files:
119
120
  - spec/mongoid/report/aggregation_spec.rb
121
+ - spec/mongoid/report/collection_spec.rb
120
122
  - spec/mongoid/report/column_spec.rb
121
123
  - spec/mongoid/report/config_spec.rb
122
124
  - spec/mongoid/report/queries_builder_spec.rb