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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjViNTY5ZjMxOWYzYWU5ZmY3NGNlMGJkZTVkMjFkZmY5ZGUxMTEyZA==
4
+ Y2Y4YTU4MzMzZmQ0MmRmYzNhYTMwZWM5MDViNDFhOTExMDJlYWMxNg==
5
5
  data.tar.gz: !binary |-
6
- YjhiZDUzNWIwNGYxYWI3OGQ4MjJhMDRkYWFmNjVkMmZhNjBlZmY4Zg==
6
+ YzNkMDcxYjU1NjQ2ZGI5MjAwYzk2OGFlYWYzOGNmOTQyYjMzMjFiMg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MjFhMWU4YWI4YzExYzUyYmI3ZWM4Yzk5OGE3NGJlOWE0OTcyNjY4NmMwODRj
10
- ZDQ0NWViMDk4MTk3MWZmODBkZmYwZGQzMjgzNjU4M2VjN2M5ZmQ1MWEwYTZl
11
- MTBmMjhjMDIxYzJiNTJkMTAxNzVmYjM1MjUzOTBlMWE3YWU4OTA=
9
+ YjY5OTdjZGY5ODQ4MDRmMGZlMTVlMzUwMjM4NWQzOGMwZDAzODE3OWNiNGRk
10
+ MzBkMDMwOTZiMTgyZmYxZmZjZjliMmEwOTZjNTY1YjYyYzBhODk4YjE2ZGRh
11
+ NjZlYTAyNDRiNjA1NzE0MzY3MmUwZGQ5YTE0NTY0M2YxMTU5NzQ=
12
12
  data.tar.gz: !binary |-
13
- Y2E4NzQzMDJkZGIyOTBmZDJkY2IyNWM3NmUyZGQ0Y2U5YWRiMDM4MGMzOTli
14
- Mzg5MDRhZjUwNWVmZTZmMTNlZGY0MTM0N2Y2ZDM4MDI0ZjQ1MjE2ZWYzMjg3
15
- ODdjMTA4NDBiMmM2MjhkMzhjZjI2MGM0ZWI0ZWViYjcwYTIyOGU=
13
+ M2FlNTYzNzJmOTZmNmY2ZTNmNDc3MTA3ODg2NzliNjNjMTFjZDA5MDVmZThj
14
+ YTAyZDY2ZTUyNWIzOGFjMGY5M2JiMjEzNTQ4ZTIwZmY1ZTUxZDY4NzA1NzAz
15
+ MDRlYjJlZTQ3ODViMTc1MGI1YzBhYWZhMjQ3YTU3YzBhYjQxNmE=
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongoid-report (0.0.11)
4
+ mongoid-report (0.1.0)
5
5
  mongoid (> 3.0.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -89,6 +89,12 @@ Or install it yourself as:
89
89
 
90
90
  $ gem install mongoid-report
91
91
 
92
+ ## Contributors
93
+
94
+ @oivoodoo
95
+
96
+ @baltazore
97
+
92
98
  ## Contributing
93
99
 
94
100
  1. Fork it ( http://github.com/oivoodoo/mongoid-report/fork )
@@ -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] = @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
@@ -24,6 +24,13 @@ module Mongoid
24
24
 
25
25
  context.group_by(*fields, group_options)
26
26
  end
27
+
28
+ def filter(*fields)
29
+ filter_options = fields.extract_options!
30
+ filter_options.merge!(options)
31
+
32
+ context.filter(*fields, filter_options)
33
+ end
27
34
  end
28
35
 
29
36
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Report
3
- VERSION = "0.0.11"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  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
@@ -87,4 +87,5 @@ describe Mongoid::Report do
87
87
  expect(Report10.fields(Model).values).to eq(['field-name'])
88
88
  end
89
89
  end
90
+
90
91
  end
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.11
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-17 00:00:00.000000000 Z
11
+ date: 2014-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid