mongoid-report 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OWE5MWVmMDI4MjY5MmE1MjNkZDUyNzMxZDVjOWNmMDljNWFjY2U0OQ==
5
+ data.tar.gz: !binary |-
6
+ MDZmNTg3OTJlMzcwZGNhMmVmYTVjNDY1OWNhMDBiYzllMDE4MTQ4OA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NjMzOTZhMWVkYzMxNTM4NTIzZjE5NGJkMWI1NTY5NmI5NDcyZjhmYTQyNTg2
10
+ MjNmMjlkOWJjZTEwMDcyOGJhMGU3NmZhZGZjZWExMDExNjZhNDY2NDhjMzZj
11
+ Y2ExOTY3ZmMzMmE0NzgwM2YzNTA3OTgzNjAzMzhmOTRkYjExMWQ=
12
+ data.tar.gz: !binary |-
13
+ ZWU3NDBmYjlkMTZjOWRlZDE5NzI5Yjg1Nzk5N2Q2ODkxYzVkMTI0NTJkYzZk
14
+ MTg3MjhiZGI2ZTMzODZiODBiMWJlNDk2YWY3NDJjYTIzZjRmOThlNGU5OTRm
15
+ MjlkZDk1MWMzOGM4NTI0NWVkNjA4ZDUyY2VmOTIyNDRlNGIwNzQ=
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ vendor/
2
+ .bundle/
3
+ pkg/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p448
data/.vimrc ADDED
@@ -0,0 +1,4 @@
1
+ let g:turbux_command_prefix=''
2
+ let g:turbux_command_rspec = 'rspec'
3
+ let g:turbux_command_test_unit = 'ruby -Itest'
4
+ let g:turbux_command_cucumber = 'cucumber'
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rspec'
7
+ gem 'rspec-expectations'
8
+ gem 'database_cleaner'
9
+ end
10
+
11
+ group :development, :test do
12
+ gem 'pry'
13
+ gem 'pry-debugger'
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,71 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mongoid-report (0.0.5)
5
+ mongoid (> 3.0.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (3.2.18)
11
+ activesupport (= 3.2.18)
12
+ builder (~> 3.0.0)
13
+ activesupport (3.2.18)
14
+ i18n (~> 0.6, >= 0.6.4)
15
+ multi_json (~> 1.0)
16
+ builder (3.0.4)
17
+ coderay (1.1.0)
18
+ columnize (0.3.6)
19
+ database_cleaner (1.2.0)
20
+ debugger (1.6.6)
21
+ columnize (>= 0.3.1)
22
+ debugger-linecache (~> 1.2.0)
23
+ debugger-ruby_core_source (~> 1.3.2)
24
+ debugger-linecache (1.2.0)
25
+ debugger-ruby_core_source (1.3.2)
26
+ diff-lcs (1.2.5)
27
+ i18n (0.6.9)
28
+ method_source (0.8.2)
29
+ mongoid (3.1.6)
30
+ activemodel (~> 3.2)
31
+ moped (~> 1.4)
32
+ origin (~> 1.0)
33
+ tzinfo (~> 0.3.29)
34
+ moped (1.5.2)
35
+ multi_json (1.10.1)
36
+ origin (1.1.0)
37
+ pry (0.9.12.6)
38
+ coderay (~> 1.0)
39
+ method_source (~> 0.8)
40
+ slop (~> 3.4)
41
+ pry-debugger (0.2.2)
42
+ debugger (~> 1.3)
43
+ pry (~> 0.9.10)
44
+ rake (10.3.2)
45
+ rspec (3.0.0)
46
+ rspec-core (~> 3.0.0)
47
+ rspec-expectations (~> 3.0.0)
48
+ rspec-mocks (~> 3.0.0)
49
+ rspec-core (3.0.0)
50
+ rspec-support (~> 3.0.0)
51
+ rspec-expectations (3.0.0)
52
+ diff-lcs (>= 1.2.0, < 2.0)
53
+ rspec-support (~> 3.0.0)
54
+ rspec-mocks (3.0.1)
55
+ rspec-support (~> 3.0.0)
56
+ rspec-support (3.0.0)
57
+ slop (3.5.0)
58
+ tzinfo (0.3.39)
59
+
60
+ PLATFORMS
61
+ ruby
62
+
63
+ DEPENDENCIES
64
+ bundler (~> 1.5)
65
+ database_cleaner
66
+ mongoid-report!
67
+ pry
68
+ pry-debugger
69
+ rake
70
+ rspec
71
+ rspec-expectations
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alexandr Korsak
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # Mongoid Report
2
+
3
+ Library for easy building aggregation report using mongodb aggregation
4
+ framework.
5
+
6
+ ### Example
7
+
8
+ ```ruby
9
+ class Model
10
+ include Mongoid::Document
11
+
12
+ field :field1, type: Integer, default: 0
13
+
14
+ field :day, type: Date
15
+ end
16
+
17
+ class Report1
18
+ include Mongoid::Report
19
+
20
+ aggregation_field :field1, for: Model
21
+ end
22
+
23
+ class Report2
24
+ include Mongoid::Report
25
+
26
+ attach_to Model do
27
+ aggregation_field :field1
28
+ end
29
+ end
30
+
31
+ class Report3
32
+ include Mongoid::Report
33
+
34
+ group_by :day, for: Model
35
+
36
+ aggregation_field :field1, for: Model
37
+ end
38
+
39
+ class Report4
40
+ include Mongoid::Report
41
+
42
+ attach_to Model do
43
+ group_by :day
44
+
45
+ aggregation_field :field1
46
+ end
47
+ end
48
+ ```
49
+
50
+ ```ruby
51
+ example = Report4.new
52
+ result = example.aggregate_for(Model)
53
+
54
+ result.is_a?(Array) => true
55
+ result[0].is_a?(Hash) => true
56
+ ```
57
+
58
+ ## Installation
59
+
60
+ Add this line to your application's Gemfile:
61
+
62
+ gem 'mongoid-report'
63
+
64
+ And then execute:
65
+
66
+ $ bundle
67
+
68
+ Or install it yourself as:
69
+
70
+ $ gem install mongoid_report
71
+
72
+ ## Usage
73
+
74
+ TODO: Write usage instructions here
75
+
76
+ ## Contributing
77
+
78
+ 1. Fork it ( http://github.com/<my-github-username>/mongoid_report/fork )
79
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
80
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
81
+ 4. Push to the branch (`git push origin my-new-feature`)
82
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,15 @@
1
+ module Mongoid
2
+ module Report
3
+
4
+ AttachProxy = Struct.new(:context, :collection) do
5
+ def aggregation_field(*fields)
6
+ context.aggregation_field(*fields, for: collection)
7
+ end
8
+
9
+ def group_by(*fields)
10
+ context.group_by(*fields, for: collection)
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ require 'delegate'
2
+
3
+ module Mongoid
4
+ module Report
5
+
6
+ class Collection < SimpleDelegator
7
+ def initialize(rows, fields)
8
+ @rows = rows
9
+ @fields = fields
10
+ super(rows)
11
+ end
12
+
13
+ def summary
14
+ @summary ||= reduce(Hash.new{|h, k| h[k] = 0}) do |summary, row|
15
+ @fields.each do |field|
16
+ summary[field] += row[field.to_s]
17
+ end
18
+
19
+ summary
20
+ end
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,78 @@
1
+ module Mongoid
2
+ module Report
3
+
4
+ QueriesBuilder = Struct.new(:settings) do
5
+ def do
6
+ [].tap do |queries|
7
+ queries.concat([{ '$project' => query1 }])
8
+ queries.concat([{ '$group' => query2 }])
9
+ queries.concat([{ '$project' => query3 }])
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def groups
16
+ @group_by ||= begin
17
+ if settings[:group_by].size == 0
18
+ [:_id]
19
+ else
20
+ settings[:group_by]
21
+ end
22
+ end
23
+ end
24
+
25
+ def fields
26
+ @fields ||= settings[:fields]
27
+ end
28
+
29
+ def all_fields
30
+ [:_id]
31
+ .concat(fields)
32
+ .concat(groups)
33
+ end
34
+
35
+ # Example: { '$project' => { :field1 => 1 } }
36
+ def query1
37
+ all_fields.inject({}) do |hash, field|
38
+ hash.merge!(field => 1)
39
+ end
40
+ end
41
+
42
+ GROUP_TEMPLATE = "$%s"
43
+ def query2
44
+ {}.tap do |query|
45
+ query[:_id] = {}
46
+
47
+ groups.inject(query[:_id]) do |hash, group|
48
+ hash.merge!(group => GROUP_TEMPLATE % group)
49
+ end
50
+
51
+ fields.inject(query) do |hash, field|
52
+ hash.merge!(field => { '$sum' => GROUP_TEMPLATE % field })
53
+ end
54
+ end
55
+ end
56
+
57
+ PROJECT_TEMPLATE = "$_id.%s"
58
+ def query3
59
+ {}.tap do |query|
60
+ if groups == [:_id]
61
+ query[:_id] = '$_id'
62
+ else
63
+ query[:_id] = 0
64
+
65
+ groups.inject(query) do |hash, group|
66
+ hash.merge!(group => PROJECT_TEMPLATE % group)
67
+ end
68
+ end
69
+
70
+ fields.inject(query) do |hash, field|
71
+ hash.merge!(field => 1)
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Report
3
+ VERSION = "0.0.5"
4
+ end
5
+ end
@@ -0,0 +1,122 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/core_ext/class/attribute'
3
+
4
+ require_relative 'report/queries_builder'
5
+ require_relative 'report/attach_proxy'
6
+ require_relative 'report/collection'
7
+
8
+ module Mongoid
9
+ module Report
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ extend ClassMethods
14
+
15
+ class_attribute :settings
16
+
17
+ self.settings = {}
18
+
19
+ def initialize_report_module
20
+ self.class.settings.each do |klass, configuration|
21
+ builder = QueriesBuilder.new(configuration)
22
+
23
+ # Prepare group queries depends on the configuration in the included
24
+ # class.
25
+ @queries = builder.do
26
+
27
+ # Now we have access to compiled queries to run it in aggregation
28
+ # framework.
29
+ configuration[:queries] = @queries
30
+ end
31
+ end
32
+ alias :initialize :initialize_report_module
33
+
34
+ def queries(klass)
35
+ self.class.settings[klass][:queries]
36
+ end
37
+
38
+ # We should pass here mongoid document
39
+ def aggregate_for(klass)
40
+ queries = queries(klass)
41
+
42
+ yield queries if block_given?
43
+
44
+ # Lets wrap aggregation by collection structure for adding common
45
+ # methods like summary for data.
46
+ Collection.new(
47
+ klass.collection.aggregate(queries),
48
+ self.class.fields(klass),
49
+ )
50
+ end
51
+ end
52
+
53
+ module ClassMethods
54
+ def attach_to(collection, &block)
55
+ proxy = AttachProxy.new(self, collection)
56
+ proxy.instance_eval(&block)
57
+ end
58
+
59
+ def group_by(*fields)
60
+ define_report_method(*fields) do |groups, collection|
61
+ settings[collection][:group_by] = groups
62
+ end
63
+ end
64
+
65
+ def aggregation_field(*fields)
66
+ define_report_method(*fields) do |columns, collection|
67
+ columns.each do |column|
68
+ add_field(collection, column)
69
+ end
70
+ end
71
+ end
72
+
73
+ def fields(collection)
74
+ settings_property(collection, :fields)
75
+ end
76
+
77
+ def groups(collection)
78
+ settings_property(collection, :group_by)
79
+ end
80
+
81
+ private
82
+
83
+ def define_report_method(*fields)
84
+ options = fields.extract_options!
85
+
86
+ # We should always specify model to attach fields, groups
87
+ collection = options.fetch(:for)
88
+
89
+ # We should always have for option
90
+ initialize_settings_by(collection)
91
+
92
+ # Because of modifying fields(usign exract options method of
93
+ # ActiveSupport) lets pass fields to the next block with collection.
94
+ yield fields, collection
95
+ end
96
+
97
+ def initialize_settings_by(collection)
98
+ settings[collection] ||= settings.fetch(collection) do
99
+ {
100
+ fields: [],
101
+ group_by: [],
102
+ }
103
+ end
104
+ end
105
+
106
+ def add_field(collection, field)
107
+ settings[collection][:fields] << field
108
+
109
+ class_eval <<-FIELD
110
+ def #{field}
111
+ @#{field} ||= 0
112
+ end
113
+ FIELD
114
+ end
115
+
116
+ def settings_property(collection, key)
117
+ settings.fetch(collection, {}).fetch(key, [])
118
+ end
119
+ end
120
+
121
+ end
122
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongoid/report/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongoid-report"
8
+ spec.version = Mongoid::Report::VERSION
9
+ spec.authors = ["Alexandr Korsak"]
10
+ spec.email = ["alex.korsak@gmail.com"]
11
+ spec.summary = %q{Easily build mongoid reports using aggregation framework}
12
+ spec.description = %q{Easily build mongoid reports using aggregation frameworkk}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'mongoid', '> 3.0.1'
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Report do
4
+ describe '.aggregate_for' do
5
+ let(:klass) { Model }
6
+ let(:yesterday) { Date.parse("19-12-2004") }
7
+ let(:today) { Date.parse("20-12-2004") }
8
+
9
+ it 'aggregates fields by default group _id as well' do
10
+ instance1 = klass.create!(day: today , field1: 1)
11
+ instance2 = klass.create!(day: today , field1: 1)
12
+ instance3 = klass.create!(day: yesterday , field1: 1)
13
+
14
+ example = Report2.new
15
+ rows = example.aggregate_for(klass)
16
+
17
+ expect(rows.size).to eq(3)
18
+ expect(rows[0]['field1']).to eq(1)
19
+ expect(rows[1]['field1']).to eq(1)
20
+ expect(rows[2]['field1']).to eq(1)
21
+ end
22
+
23
+ it 'aggregates field by defined field of the mode' do
24
+ klass.create!(day: today , field1: 1)
25
+ klass.create!(day: today , field1: 1)
26
+ klass.create!(day: yesterday , field1: 1)
27
+
28
+ example = Report3.new
29
+ rows = example.aggregate_for(klass)
30
+
31
+ expect(rows.size).to eq(2)
32
+ expect(rows[0]['field1']).to eq(1)
33
+ expect(rows[0]['day']).to eq(yesterday)
34
+ expect(rows[1]['field1']).to eq(2)
35
+ expect(rows[1]['day']).to eq(today)
36
+ end
37
+
38
+ let(:two_days_ago) { Date.parse("18-12-2004") }
39
+
40
+ it 'wraps group query by extra match queries' do
41
+ klass.create(day: today , field1: 1 , field2: 2)
42
+ klass.create(day: today , field1: 1 , field2: 2)
43
+ klass.create(day: yesterday , field1: 1 , field2: 2)
44
+ klass.create(day: two_days_ago , field1: 1 , field2: 2)
45
+ klass.create(day: today , field1: 1 , field2: 3)
46
+
47
+ example = Report3.new
48
+ rows = example.aggregate_for(Model) do |queries|
49
+ # adding extra queries before the main
50
+ queries.unshift({ '$match' => { :day => { '$gte' => yesterday.mongoize, '$lte' => today.mongoize } } })
51
+ queries.unshift({ '$match' => { :field2 => 2 } })
52
+ # adding sort to the end of aggregation query
53
+ queries.concat([{ '$sort' => { day: -1 } }])
54
+ queries
55
+ end
56
+
57
+ expect(rows.size).to eq(2)
58
+ expect(rows[0]['field1']).to eq(2)
59
+ expect(rows[0]['day']).to eq(today)
60
+ expect(rows[1]['field1']).to eq(1)
61
+ expect(rows[1]['day']).to eq(yesterday)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Report::QueriesBuilder do
4
+
5
+ describe '.queries' do
6
+ it 'builds queries for aggregation using default group _id field' do
7
+ queries = Report1.new.queries(Model)
8
+ expect(queries.size).to eq(3)
9
+ expect(queries[0]).to eq(
10
+ '$project' => {
11
+ :_id => 1,
12
+ :field1 => 1,
13
+ })
14
+ expect(queries[1]).to eq(
15
+ '$group' => {
16
+ :_id => { :_id => '$_id' },
17
+ :field1 => { '$sum' => '$field1' },
18
+ })
19
+ expect(queries[2]).to eq(
20
+ '$project' => {
21
+ :_id => '$_id',
22
+ :field1 => 1,
23
+ })
24
+ end
25
+
26
+ it 'builds queries using custom one group' do
27
+ queries = Report4.new.queries(Model)
28
+ expect(queries.size).to eq(3)
29
+ expect(queries[0]).to eq(
30
+ '$project' => {
31
+ :_id => 1,
32
+ :field1 => 1,
33
+ :day => 1,
34
+ })
35
+ expect(queries[1]).to eq(
36
+ '$group' => {
37
+ :_id => { :day => '$day' },
38
+ :field1 => { '$sum' => '$field1' },
39
+ })
40
+ expect(queries[2]).to eq(
41
+ '$project' => {
42
+ :_id => 0,
43
+ :day => '$_id.day',
44
+ :field1 => 1,
45
+ })
46
+ end
47
+
48
+ class Report5
49
+ include Mongoid::Report
50
+
51
+ attach_to Model do
52
+ group_by :day, :field2
53
+
54
+ aggregation_field :field1, :field3
55
+ end
56
+ end
57
+
58
+ it 'builds queries using custom one group' do
59
+ queries = Report5.new.queries(Model)
60
+ expect(queries.size).to eq(3)
61
+ expect(queries[0]).to eq(
62
+ '$project' => {
63
+ :_id => 1,
64
+ :field1 => 1,
65
+ :field3 => 1,
66
+ :day => 1,
67
+ :field2 => 1,
68
+ })
69
+ expect(queries[1]).to eq(
70
+ '$group' => {
71
+ :_id => { :day => '$day', :field2 => '$field2' },
72
+ :field1 => { '$sum' => '$field1' },
73
+ :field3 => { '$sum' => '$field3' },
74
+ })
75
+ expect(queries[2]).to eq(
76
+ '$project' => {
77
+ :_id => 0,
78
+ :day => '$_id.day',
79
+ :field2 => '$_id.field2',
80
+ :field1 => 1,
81
+ :field3 => 1,
82
+ })
83
+ end
84
+ end
85
+
86
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Report do
4
+
5
+ describe '.summary' do
6
+ let(:klass) { Model }
7
+ let(:yesterday) { Date.parse("19-12-2004") }
8
+ let(:today) { Date.parse("20-12-2004") }
9
+
10
+ it 'returns summary for all rows' do
11
+ klass.create!(day: today , field1: 1)
12
+ klass.create!(day: today , field1: 1)
13
+ klass.create!(day: yesterday , field1: 1)
14
+
15
+ example = Report3.new
16
+ rows = example.aggregate_for(klass)
17
+
18
+ expect(rows.count).to eq(2)
19
+ expect(rows.summary[:field1]).to eq(3)
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Report do
4
+
5
+ describe '.aggregation_field' do
6
+ it 'defines field for aggregation using class method' do
7
+ example = Report1.new
8
+ expect(example).to be_respond_to(:field1)
9
+ end
10
+
11
+ it 'defines as reader only' do
12
+ example = Report1.new
13
+ expect{ example.field1 = 'value' }.to raise_error
14
+ end
15
+
16
+ it 'defines field with 0 by default' do
17
+ example = Report1.new
18
+ expect(example.field1).to eq(0)
19
+ end
20
+
21
+ it 'defines aggegration settings' do
22
+ expect(Report1).to be_respond_to(:settings)
23
+ end
24
+
25
+ it 'defines aggregation field for specific model to make queries' do
26
+ fields = Report1.fields(Model)
27
+ expect(fields).to eq([:field1])
28
+ end
29
+ end
30
+
31
+ describe '.attach_to' do
32
+ it 'defines method in report class to attach report to the model' do
33
+ expect(Report2).to be_respond_to(:attach_to)
34
+ end
35
+
36
+ it 'defines field in terms of attached model' do
37
+ fields = Report2.fields(Model)
38
+ expect(fields).to eq([:field1])
39
+ end
40
+ end
41
+
42
+ describe '.group_by' do
43
+ it 'defines group by method as class method' do
44
+ expect(Report3).to be_respond_to(:group_by)
45
+ end
46
+
47
+ it 'stores group by settings under report class' do
48
+ group_by_settings = Report3.settings[Model][:group_by]
49
+ expect(group_by_settings).to eq([:day])
50
+ end
51
+
52
+ it 'defines groups in terms of attached model' do
53
+ groups = Report4.groups(Model)
54
+ expect(groups).to eq([:day])
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,39 @@
1
+ require 'mongoid'
2
+ require 'active_support/all'
3
+
4
+ require 'rspec'
5
+ require 'database_cleaner'
6
+
7
+ Mongoid.configure do |config|
8
+ config.connect_to('mongoid_report_test')
9
+ end
10
+
11
+ RSpec.configure do |config|
12
+ config.after(:each) do
13
+ Mongoid.purge!
14
+ end
15
+
16
+ config.backtrace_exclusion_patterns = [%r{lib\/rspec\/(core|expectations|matchers|mocks)}]
17
+ end
18
+
19
+ require_relative '../lib/mongoid/report.rb'
20
+ require_relative 'support/models.rb'
21
+
22
+ RSpec.configure do |config|
23
+ require 'rspec/expectations'
24
+ config.include RSpec::Matchers
25
+
26
+ config.mock_with :rspec
27
+
28
+ config.before(:suite) do
29
+ DatabaseCleaner.clean_with(:truncation)
30
+ end
31
+
32
+ config.before(:each) do
33
+ DatabaseCleaner.start
34
+ end
35
+
36
+ config.after(:each) do
37
+ DatabaseCleaner.clean
38
+ end
39
+ end
@@ -0,0 +1,41 @@
1
+ class Model
2
+ include Mongoid::Document
3
+
4
+ field :field1, type: Integer, default: 0
5
+ field :field2, type: Integer, default: 0
6
+ field :field3, type: Integer, default: 0
7
+
8
+ field :day, type: Date
9
+ end
10
+
11
+ class Report1
12
+ include Mongoid::Report
13
+
14
+ aggregation_field :field1, for: Model
15
+ end
16
+
17
+ class Report2
18
+ include Mongoid::Report
19
+
20
+ attach_to Model do
21
+ aggregation_field :field1
22
+ end
23
+ end
24
+
25
+ class Report3
26
+ include Mongoid::Report
27
+
28
+ group_by :day, for: Model
29
+
30
+ aggregation_field :field1, for: Model
31
+ end
32
+
33
+ class Report4
34
+ include Mongoid::Report
35
+
36
+ attach_to Model do
37
+ group_by :day
38
+
39
+ aggregation_field :field1
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-report
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Alexandr Korsak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>'
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>'
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Easily build mongoid reports using aggregation frameworkk
56
+ email:
57
+ - alex.korsak@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .DS_Store
63
+ - .gitignore
64
+ - .rspec
65
+ - .ruby-version
66
+ - .vimrc
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - lib/mongoid/report.rb
73
+ - lib/mongoid/report/attach_proxy.rb
74
+ - lib/mongoid/report/collection.rb
75
+ - lib/mongoid/report/queries_builder.rb
76
+ - lib/mongoid/report/version.rb
77
+ - mongoid-report.gemspec
78
+ - spec/mongoid/report/aggregation_spec.rb
79
+ - spec/mongoid/report/queries_builder_spec.rb
80
+ - spec/mongoid/report/summary_spec.rb
81
+ - spec/mongoid/report_spec.rb
82
+ - spec/spec_helper.rb
83
+ - spec/support/models.rb
84
+ homepage: ''
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.1.11
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Easily build mongoid reports using aggregation framework
108
+ test_files:
109
+ - spec/mongoid/report/aggregation_spec.rb
110
+ - spec/mongoid/report/queries_builder_spec.rb
111
+ - spec/mongoid/report/summary_spec.rb
112
+ - spec/mongoid/report_spec.rb
113
+ - spec/spec_helper.rb
114
+ - spec/support/models.rb