mongoid-report 0.1.6 → 0.1.7
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 +8 -8
 - data/Gemfile.lock +1 -1
 - data/README.md +5 -5
 - data/lib/mongoid/report.rb +22 -14
 - data/lib/mongoid/report/attach_proxy.rb +14 -8
 - data/lib/mongoid/report/collection.rb +23 -20
 - data/lib/mongoid/report/queries_builder.rb +3 -1
 - data/lib/mongoid/report/scope.rb +1 -0
 - data/lib/mongoid/report/version.rb +1 -1
 - data/spec/mongoid/report/aggregation_spec.rb +120 -103
 - data/spec/mongoid/report/column_spec.rb +2 -2
 - data/spec/mongoid/report/queries_builder_spec.rb +43 -27
 - data/spec/mongoid/report/set_spec.rb +30 -0
 - data/spec/mongoid/report/summary_spec.rb +19 -4
 - data/spec/mongoid/report/threads_spec.rb +2 -2
 - data/spec/mongoid/report_spec.rb +84 -47
 - data/spec/support/models.rb +0 -32
 - metadata +4 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,15 +1,15 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            !binary "U0hBMQ==":
         
     | 
| 
       3 
3 
     | 
    
         
             
              metadata.gz: !binary |-
         
     | 
| 
       4 
     | 
    
         
            -
                 
     | 
| 
      
 4 
     | 
    
         
            +
                ZDE4ZGZlZmJmOWFmMDJkNjY5Yjk3YTRmY2FlMjMwZGVlOTQzNzY4Nw==
         
     | 
| 
       5 
5 
     | 
    
         
             
              data.tar.gz: !binary |-
         
     | 
| 
       6 
     | 
    
         
            -
                 
     | 
| 
      
 6 
     | 
    
         
            +
                MTZkNzhlMTkzNTQxZWY0ZmNlMzc4YmQzM2IxMmIyZjJlOWE1ZGEwNA==
         
     | 
| 
       7 
7 
     | 
    
         
             
            SHA512:
         
     | 
| 
       8 
8 
     | 
    
         
             
              metadata.gz: !binary |-
         
     | 
| 
       9 
     | 
    
         
            -
                 
     | 
| 
       10 
     | 
    
         
            -
                 
     | 
| 
       11 
     | 
    
         
            -
                 
     | 
| 
      
 9 
     | 
    
         
            +
                MDU4NDgwNDc2ZmM2MjRiMmFmYzkxZmM2ODBkNWRjNzY2NDIxOTE3YjQzNjBh
         
     | 
| 
      
 10 
     | 
    
         
            +
                YmJkNDMxYjgyZWQ5NTRkNzJlNGUwZDdmYjU3MjQzZTc2ZjkzMTMwMTAyZDQw
         
     | 
| 
      
 11 
     | 
    
         
            +
                OTYwZmJiYzc3Y2FjYjAzZGNhZDNkODA0M2U0NjNhYTY4NmU4NmM=
         
     | 
| 
       12 
12 
     | 
    
         
             
              data.tar.gz: !binary |-
         
     | 
| 
       13 
     | 
    
         
            -
                 
     | 
| 
       14 
     | 
    
         
            -
                 
     | 
| 
       15 
     | 
    
         
            -
                 
     | 
| 
      
 13 
     | 
    
         
            +
                MTNiMjcyZGZmMzZkNTIzZTI4MGVjZGQ2OTY4ZDBjMTY1ODAwOWVlNDI3MjM4
         
     | 
| 
      
 14 
     | 
    
         
            +
                ODVmMmQwNTUxMDNhOWY0M2NjOWNlOWY0MTBlYTVjNTkyNWQ3YTc1NTFkOTU5
         
     | 
| 
      
 15 
     | 
    
         
            +
                NWE3OTBmNGU3MTNjOWJjYzUxYjVkNjU2OGI5ZmE4NjNiYTA0MjY=
         
     | 
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | 
         @@ -21,14 +21,14 @@ framework. 
     | 
|
| 
       21 
21 
     | 
    
         
             
              class Report1
         
     | 
| 
       22 
22 
     | 
    
         
             
                include Mongoid::Report
         
     | 
| 
       23 
23 
     | 
    
         | 
| 
       24 
     | 
    
         
            -
                 
     | 
| 
      
 24 
     | 
    
         
            +
                column :field1, for: Model
         
     | 
| 
       25 
25 
     | 
    
         
             
              end
         
     | 
| 
       26 
26 
     | 
    
         | 
| 
       27 
27 
     | 
    
         
             
              class Report2
         
     | 
| 
       28 
28 
     | 
    
         
             
                include Mongoid::Report
         
     | 
| 
       29 
29 
     | 
    
         | 
| 
       30 
30 
     | 
    
         
             
                attach_to Model do
         
     | 
| 
       31 
     | 
    
         
            -
                   
     | 
| 
      
 31 
     | 
    
         
            +
                  column :field1
         
     | 
| 
       32 
32 
     | 
    
         
             
                end
         
     | 
| 
       33 
33 
     | 
    
         
             
              end
         
     | 
| 
       34 
34 
     | 
    
         | 
| 
         @@ -37,7 +37,7 @@ framework. 
     | 
|
| 
       37 
37 
     | 
    
         | 
| 
       38 
38 
     | 
    
         
             
                group_by :day, for: Model
         
     | 
| 
       39 
39 
     | 
    
         | 
| 
       40 
     | 
    
         
            -
                 
     | 
| 
      
 40 
     | 
    
         
            +
                column :field1, for: Model
         
     | 
| 
       41 
41 
     | 
    
         
             
              end
         
     | 
| 
       42 
42 
     | 
    
         | 
| 
       43 
43 
     | 
    
         
             
              class Report4
         
     | 
| 
         @@ -46,7 +46,7 @@ framework. 
     | 
|
| 
       46 
46 
     | 
    
         
             
                attach_to Model do
         
     | 
| 
       47 
47 
     | 
    
         
             
                  group_by :day
         
     | 
| 
       48 
48 
     | 
    
         | 
| 
       49 
     | 
    
         
            -
                   
     | 
| 
      
 49 
     | 
    
         
            +
                  column :field1
         
     | 
| 
       50 
50 
     | 
    
         
             
                end
         
     | 
| 
       51 
51 
     | 
    
         
             
              end
         
     | 
| 
       52 
52 
     | 
    
         | 
| 
         @@ -56,7 +56,7 @@ framework. 
     | 
|
| 
       56 
56 
     | 
    
         
             
                attach_to Model, as: 'summary-report' do
         
     | 
| 
       57 
57 
     | 
    
         
             
                  group_by :day
         
     | 
| 
       58 
58 
     | 
    
         | 
| 
       59 
     | 
    
         
            -
                   
     | 
| 
      
 59 
     | 
    
         
            +
                  column :field1
         
     | 
| 
       60 
60 
     | 
    
         
             
                end
         
     | 
| 
       61 
61 
     | 
    
         
             
              end
         
     | 
| 
       62 
62 
     | 
    
         
             
            ```
         
     | 
    
        data/lib/mongoid/report.rb
    CHANGED
    
    | 
         @@ -51,6 +51,10 @@ module Mongoid 
     | 
|
| 
       51 
51 
     | 
    
         
             
                    report_module_settings[klass][:queries]
         
     | 
| 
       52 
52 
     | 
    
         
             
                  end
         
     | 
| 
       53 
53 
     | 
    
         | 
| 
      
 54 
     | 
    
         
            +
                  def mapping(klass)
         
     | 
| 
      
 55 
     | 
    
         
            +
                    report_module_settings[klass][:mapping]
         
     | 
| 
      
 56 
     | 
    
         
            +
                  end
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
       54 
58 
     | 
    
         
             
                  # We should pass here mongoid document
         
     | 
| 
       55 
59 
     | 
    
         
             
                  def aggregate_for(report_name)
         
     | 
| 
       56 
60 
     | 
    
         
             
                    Scope.new(self, report_name)
         
     | 
| 
         @@ -87,11 +91,11 @@ module Mongoid 
     | 
|
| 
       87 
91 
     | 
    
         | 
| 
       88 
92 
     | 
    
         
             
                  def group_by(*fields)
         
     | 
| 
       89 
93 
     | 
    
         
             
                    define_report_method(*fields) do |groups, report_name, _|
         
     | 
| 
       90 
     | 
    
         
            -
                      settings[report_name][:group_by] = groups
         
     | 
| 
      
 94 
     | 
    
         
            +
                      settings[report_name][:group_by] = groups.map(&:to_s)
         
     | 
| 
       91 
95 
     | 
    
         
             
                    end
         
     | 
| 
       92 
96 
     | 
    
         
             
                  end
         
     | 
| 
       93 
97 
     | 
    
         | 
| 
       94 
     | 
    
         
            -
                  def  
     | 
| 
      
 98 
     | 
    
         
            +
                  def column(*fields)
         
     | 
| 
       95 
99 
     | 
    
         
             
                    define_report_method(*fields) do |columns, report_name, options|
         
     | 
| 
       96 
100 
     | 
    
         
             
                      columns.each do |column|
         
     | 
| 
       97 
101 
     | 
    
         
             
                        name = options.fetch(:as) { column }
         
     | 
| 
         @@ -100,13 +104,21 @@ module Mongoid 
     | 
|
| 
       100 
104 
     | 
    
         
             
                    end
         
     | 
| 
       101 
105 
     | 
    
         
             
                  end
         
     | 
| 
       102 
106 
     | 
    
         | 
| 
       103 
     | 
    
         
            -
                  def  
     | 
| 
       104 
     | 
    
         
            -
                    # Because of representing fields as hash instead of columns we should
         
     | 
| 
       105 
     | 
    
         
            -
                    # have the last variable as columns.
         
     | 
| 
      
 107 
     | 
    
         
            +
                  def columns(*fields)
         
     | 
| 
       106 
108 
     | 
    
         
             
                    define_report_method(*fields) do |_, report_name, columns|
         
     | 
| 
       107 
     | 
    
         
            -
                      columns 
     | 
| 
       108 
     | 
    
         
            -
             
     | 
| 
      
 109 
     | 
    
         
            +
                      self.settings[report_name][:columns] = columns.stringify_keys!
         
     | 
| 
      
 110 
     | 
    
         
            +
                    end
         
     | 
| 
      
 111 
     | 
    
         
            +
                  end
         
     | 
| 
      
 112 
     | 
    
         
            +
             
     | 
| 
      
 113 
     | 
    
         
            +
                  def mapping(*fields)
         
     | 
| 
      
 114 
     | 
    
         
            +
                    define_report_method(*fields) do |_, report_name, mapping|
         
     | 
| 
      
 115 
     | 
    
         
            +
                      mapping.stringify_keys!
         
     | 
| 
      
 116 
     | 
    
         
            +
             
     | 
| 
      
 117 
     | 
    
         
            +
                      mapping.each do |key, value|
         
     | 
| 
      
 118 
     | 
    
         
            +
                        mapping[key] = value.to_s
         
     | 
| 
       109 
119 
     | 
    
         
             
                      end
         
     | 
| 
      
 120 
     | 
    
         
            +
             
     | 
| 
      
 121 
     | 
    
         
            +
                      self.settings[report_name][:mapping] = mapping
         
     | 
| 
       110 
122 
     | 
    
         
             
                    end
         
     | 
| 
       111 
123 
     | 
    
         
             
                  end
         
     | 
| 
       112 
124 
     | 
    
         | 
| 
         @@ -153,20 +165,16 @@ module Mongoid 
     | 
|
| 
       153 
165 
     | 
    
         
             
                        fields:    ActiveSupport::OrderedHash.new,
         
     | 
| 
       154 
166 
     | 
    
         
             
                        group_by:  [],
         
     | 
| 
       155 
167 
     | 
    
         
             
                        queries:   [],
         
     | 
| 
      
 168 
     | 
    
         
            +
                        columns:   {},
         
     | 
| 
      
 169 
     | 
    
         
            +
                        mapping:   {},
         
     | 
| 
       156 
170 
     | 
    
         
             
                        compiled:  false,
         
     | 
| 
       157 
     | 
    
         
            -
                        columns:   ActiveSupport::OrderedHash.new,
         
     | 
| 
       158 
171 
     | 
    
         
             
                      }
         
     | 
| 
       159 
172 
     | 
    
         
             
                    end
         
     | 
| 
       160 
173 
     | 
    
         
             
                  end
         
     | 
| 
       161 
174 
     | 
    
         | 
| 
       162 
175 
     | 
    
         
             
                  def add_field(attach_name, field, name)
         
     | 
| 
       163 
     | 
    
         
            -
                    settings[attach_name][:fields][field] = name
         
     | 
| 
      
 176 
     | 
    
         
            +
                    settings[attach_name][:fields][field.to_s] = name.to_s
         
     | 
| 
       164 
177 
     | 
    
         
             
                  end
         
     | 
| 
       165 
     | 
    
         
            -
             
     | 
| 
       166 
     | 
    
         
            -
                  def add_column(attach_name, name, function)
         
     | 
| 
       167 
     | 
    
         
            -
                    settings[attach_name][:columns][name] = function
         
     | 
| 
       168 
     | 
    
         
            -
                  end
         
     | 
| 
       169 
     | 
    
         
            -
             
     | 
| 
       170 
178 
     | 
    
         
             
                end
         
     | 
| 
       171 
179 
     | 
    
         | 
| 
       172 
180 
     | 
    
         
             
              end
         
     | 
| 
         @@ -11,28 +11,34 @@ module Mongoid 
     | 
|
| 
       11 
11 
     | 
    
         
             
                    super(context, collection, options)
         
     | 
| 
       12 
12 
     | 
    
         
             
                  end
         
     | 
| 
       13 
13 
     | 
    
         | 
| 
       14 
     | 
    
         
            -
                  def  
     | 
| 
      
 14 
     | 
    
         
            +
                  def columns(*fields)
         
     | 
| 
       15 
15 
     | 
    
         
             
                    proxy_options = fields.extract_options!
         
     | 
| 
       16 
16 
     | 
    
         
             
                    proxy_options.merge!(options)
         
     | 
| 
       17 
     | 
    
         
            -
                    context. 
     | 
| 
      
 17 
     | 
    
         
            +
                    context.columns(*fields, proxy_options)
         
     | 
| 
       18 
18 
     | 
    
         
             
                  end
         
     | 
| 
       19 
19 
     | 
    
         | 
| 
       20 
     | 
    
         
            -
                  def  
     | 
| 
      
 20 
     | 
    
         
            +
                  def column(*fields)
         
     | 
| 
       21 
21 
     | 
    
         
             
                    proxy_options = fields.extract_options!
         
     | 
| 
       22 
22 
     | 
    
         
             
                    proxy_options.merge!(options)
         
     | 
| 
       23 
     | 
    
         
            -
                    context. 
     | 
| 
      
 23 
     | 
    
         
            +
                    context.column(*fields, proxy_options)
         
     | 
| 
       24 
24 
     | 
    
         
             
                  end
         
     | 
| 
       25 
25 
     | 
    
         | 
| 
       26 
     | 
    
         
            -
                  def  
     | 
| 
      
 26 
     | 
    
         
            +
                  def mapping(*fields)
         
     | 
| 
       27 
27 
     | 
    
         
             
                    proxy_options = fields.extract_options!
         
     | 
| 
       28 
28 
     | 
    
         
             
                    proxy_options.merge!(options)
         
     | 
| 
       29 
     | 
    
         
            -
                    context. 
     | 
| 
      
 29 
     | 
    
         
            +
                    context.mapping(*fields, proxy_options)
         
     | 
| 
       30 
30 
     | 
    
         
             
                  end
         
     | 
| 
       31 
31 
     | 
    
         | 
| 
       32 
     | 
    
         
            -
                  def  
     | 
| 
      
 32 
     | 
    
         
            +
                  def group_by(*fields)
         
     | 
| 
       33 
33 
     | 
    
         
             
                    proxy_options = fields.extract_options!
         
     | 
| 
       34 
34 
     | 
    
         
             
                    proxy_options.merge!(options)
         
     | 
| 
       35 
     | 
    
         
            -
                    context. 
     | 
| 
      
 35 
     | 
    
         
            +
                    context.group_by(*fields, proxy_options)
         
     | 
| 
      
 36 
     | 
    
         
            +
                  end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                  def filter(*fields)
         
     | 
| 
      
 39 
     | 
    
         
            +
                    proxy_options = fields.extract_options!
         
     | 
| 
      
 40 
     | 
    
         
            +
                    proxy_options.merge!(options)
         
     | 
| 
      
 41 
     | 
    
         
            +
                    context.filter(*fields, proxy_options)
         
     | 
| 
       36 
42 
     | 
    
         
             
                  end
         
     | 
| 
       37 
43 
     | 
    
         
             
                end
         
     | 
| 
       38 
44 
     | 
    
         | 
| 
         @@ -9,41 +9,44 @@ module Mongoid 
     | 
|
| 
       9 
9 
     | 
    
         
             
                    @rows    = rows
         
     | 
| 
       10 
10 
     | 
    
         
             
                    @fields  = fields
         
     | 
| 
       11 
11 
     | 
    
         
             
                    @columns = columns
         
     | 
| 
       12 
     | 
    
         
            -
             
     | 
| 
       13 
     | 
    
         
            -
                    # Apply dyncamic columns in context of row and apply indifferent access
         
     | 
| 
       14 
     | 
    
         
            -
                    # for the rows.
         
     | 
| 
       15 
     | 
    
         
            -
                    rows = compile_dynamic_fields(rows, columns)
         
     | 
| 
      
 12 
     | 
    
         
            +
                    @rows    = compile_rows
         
     | 
| 
       16 
13 
     | 
    
         | 
| 
       17 
14 
     | 
    
         
             
                    # Collection should behave like Array using delegator method.
         
     | 
| 
       18 
     | 
    
         
            -
                    super(rows)
         
     | 
| 
      
 15 
     | 
    
         
            +
                    super(@rows)
         
     | 
| 
       19 
16 
     | 
    
         
             
                  end
         
     | 
| 
       20 
17 
     | 
    
         | 
| 
       21 
     | 
    
         
            -
                  def  
     | 
| 
       22 
     | 
    
         
            -
                    @ 
     | 
| 
       23 
     | 
    
         
            -
                      @fields.each do |field|
         
     | 
| 
       24 
     | 
    
         
            -
                        next if @columns.has_key?(field.to_s)
         
     | 
| 
       25 
     | 
    
         
            -
                        summary[field] += row[field.to_s]
         
     | 
| 
       26 
     | 
    
         
            -
                      end
         
     | 
| 
       27 
     | 
    
         
            -
             
     | 
| 
      
 18 
     | 
    
         
            +
                  def compile_rows
         
     | 
| 
      
 19 
     | 
    
         
            +
                    @rows.map do |row|
         
     | 
| 
       28 
20 
     | 
    
         
             
                      @columns.each do |name, function|
         
     | 
| 
       29 
     | 
    
         
            -
                         
     | 
| 
      
 21 
     | 
    
         
            +
                        next unless @fields.include?(name)
         
     | 
| 
      
 22 
     | 
    
         
            +
                        row[name] = function.call(@context, row, { summary: false })
         
     | 
| 
       30 
23 
     | 
    
         
             
                      end
         
     | 
| 
       31 
24 
     | 
    
         | 
| 
       32 
     | 
    
         
            -
                       
     | 
| 
       33 
     | 
    
         
            -
                    end 
     | 
| 
      
 25 
     | 
    
         
            +
                      row
         
     | 
| 
      
 26 
     | 
    
         
            +
                    end
         
     | 
| 
       34 
27 
     | 
    
         
             
                  end
         
     | 
| 
       35 
28 
     | 
    
         | 
| 
       36 
     | 
    
         
            -
                   
     | 
| 
      
 29 
     | 
    
         
            +
                  def summary
         
     | 
| 
      
 30 
     | 
    
         
            +
                    @summary ||= reduce(Hash.new{|h, k| h[k] = 0}) do |summary, row|
         
     | 
| 
      
 31 
     | 
    
         
            +
                      # Find summary for aggregated rows
         
     | 
| 
      
 32 
     | 
    
         
            +
                      @fields.each do |field|
         
     | 
| 
      
 33 
     | 
    
         
            +
                        # Don't apply for dynamic calculated columns lets wait until we get
         
     | 
| 
      
 34 
     | 
    
         
            +
                        # all summaried mongo columns and then apply dynamic columns
         
     | 
| 
      
 35 
     | 
    
         
            +
                        # calculations.
         
     | 
| 
      
 36 
     | 
    
         
            +
                        next if @columns.has_key?(field)
         
     | 
| 
      
 37 
     | 
    
         
            +
                        summary[field] += row[field]
         
     | 
| 
      
 38 
     | 
    
         
            +
                      end
         
     | 
| 
       37 
39 
     | 
    
         | 
| 
       38 
     | 
    
         
            -
             
     | 
| 
       39 
     | 
    
         
            -
                    rows.map do |row|
         
     | 
| 
      
 40 
     | 
    
         
            +
                      # Apply dynamic columns for summarized row
         
     | 
| 
       40 
41 
     | 
    
         
             
                      @columns.each do |name, function|
         
     | 
| 
       41 
     | 
    
         
            -
                         
     | 
| 
      
 42 
     | 
    
         
            +
                        next unless @fields.include?(name)
         
     | 
| 
      
 43 
     | 
    
         
            +
                        summary[name] = function.call(@context, row, { summary: true })
         
     | 
| 
       42 
44 
     | 
    
         
             
                      end
         
     | 
| 
       43 
45 
     | 
    
         | 
| 
       44 
     | 
    
         
            -
                       
     | 
| 
      
 46 
     | 
    
         
            +
                      summary
         
     | 
| 
       45 
47 
     | 
    
         
             
                    end
         
     | 
| 
       46 
48 
     | 
    
         
             
                  end
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
       47 
50 
     | 
    
         
             
                end
         
     | 
| 
       48 
51 
     | 
    
         | 
| 
       49 
52 
     | 
    
         
             
              end
         
     | 
    
        data/lib/mongoid/report/scope.rb
    CHANGED
    
    
| 
         @@ -12,7 +12,7 @@ describe Mongoid::Report do 
     | 
|
| 
       12 
12 
     | 
    
         
             
                    include Mongoid::Report
         
     | 
| 
       13 
13 
     | 
    
         | 
| 
       14 
14 
     | 
    
         
             
                    attach_to Model do
         
     | 
| 
       15 
     | 
    
         
            -
                       
     | 
| 
      
 15 
     | 
    
         
            +
                      column :field1
         
     | 
| 
       16 
16 
     | 
    
         
             
                    end
         
     | 
| 
       17 
17 
     | 
    
         
             
                  end
         
     | 
| 
       18 
18 
     | 
    
         | 
| 
         @@ -33,7 +33,12 @@ describe Mongoid::Report do 
     | 
|
| 
       33 
33 
     | 
    
         
             
                  klass.create!(day: today     , field1: 1)
         
     | 
| 
       34 
34 
     | 
    
         
             
                  klass.create!(day: yesterday , field1: 1)
         
     | 
| 
       35 
35 
     | 
    
         | 
| 
       36 
     | 
    
         
            -
                   
     | 
| 
      
 36 
     | 
    
         
            +
                  Report = Class.new do
         
     | 
| 
      
 37 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 38 
     | 
    
         
            +
                    group_by :day, for: Model
         
     | 
| 
      
 39 
     | 
    
         
            +
                    column :field1, for: Model
         
     | 
| 
      
 40 
     | 
    
         
            +
                  end
         
     | 
| 
      
 41 
     | 
    
         
            +
                  example = Report.new
         
     | 
| 
       37 
42 
     | 
    
         | 
| 
       38 
43 
     | 
    
         
             
                  rows = example.aggregate_for(klass)
         
     | 
| 
       39 
44 
     | 
    
         
             
                  rows = rows.all
         
     | 
| 
         @@ -52,7 +57,13 @@ describe Mongoid::Report do 
     | 
|
| 
       52 
57 
     | 
    
         
             
                  klass.create(day: two_days_ago , field1: 1 , field2: 2)
         
     | 
| 
       53 
58 
     | 
    
         
             
                  klass.create(day: today        , field1: 1 , field2: 3)
         
     | 
| 
       54 
59 
     | 
    
         | 
| 
       55 
     | 
    
         
            -
                   
     | 
| 
      
 60 
     | 
    
         
            +
                  Report = Class.new do
         
     | 
| 
      
 61 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 62 
     | 
    
         
            +
                    group_by :day, for: Model
         
     | 
| 
      
 63 
     | 
    
         
            +
                    column :field1, for: Model
         
     | 
| 
      
 64 
     | 
    
         
            +
                  end
         
     | 
| 
      
 65 
     | 
    
         
            +
                  example = Report.new
         
     | 
| 
      
 66 
     | 
    
         
            +
             
     | 
| 
       56 
67 
     | 
    
         
             
                  scope = example.aggregate_for(Model)
         
     | 
| 
       57 
68 
     | 
    
         
             
                  scope = scope.query('$match' => { :day  => { '$gte' => yesterday.mongoize, '$lte' => today.mongoize } })
         
     | 
| 
       58 
69 
     | 
    
         
             
                  scope = scope.query('$match' => { :field2 => 2 })
         
     | 
| 
         @@ -71,7 +82,13 @@ describe Mongoid::Report do 
     | 
|
| 
       71 
82 
     | 
    
         
             
                it 'skips empty match in query' do
         
     | 
| 
       72 
83 
     | 
    
         
             
                  klass.create(day: today , field1: 1 , field2: 2)
         
     | 
| 
       73 
84 
     | 
    
         | 
| 
       74 
     | 
    
         
            -
                   
     | 
| 
      
 85 
     | 
    
         
            +
                  Report = Class.new do
         
     | 
| 
      
 86 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 87 
     | 
    
         
            +
                    group_by :day, for: Model
         
     | 
| 
      
 88 
     | 
    
         
            +
                    column :field1, for: Model
         
     | 
| 
      
 89 
     | 
    
         
            +
                  end
         
     | 
| 
      
 90 
     | 
    
         
            +
                  example = Report.new
         
     | 
| 
      
 91 
     | 
    
         
            +
             
     | 
| 
       75 
92 
     | 
    
         
             
                  scope = example.aggregate_for(Model)
         
     | 
| 
       76 
93 
     | 
    
         
             
                  scope = scope.query()
         
     | 
| 
       77 
94 
     | 
    
         
             
                  scope = scope.query({})
         
     | 
| 
         @@ -84,20 +101,6 @@ describe Mongoid::Report do 
     | 
|
| 
       84 
101 
     | 
    
         
             
                end
         
     | 
| 
       85 
102 
     | 
    
         
             
              end
         
     | 
| 
       86 
103 
     | 
    
         | 
| 
       87 
     | 
    
         
            -
              class Report7
         
     | 
| 
       88 
     | 
    
         
            -
                include Mongoid::Report
         
     | 
| 
       89 
     | 
    
         
            -
             
     | 
| 
       90 
     | 
    
         
            -
                attach_to Model, as: 'example1' do
         
     | 
| 
       91 
     | 
    
         
            -
                  group_by :day
         
     | 
| 
       92 
     | 
    
         
            -
                  aggregation_field :field1
         
     | 
| 
       93 
     | 
    
         
            -
                end
         
     | 
| 
       94 
     | 
    
         
            -
             
     | 
| 
       95 
     | 
    
         
            -
                attach_to Model, as: 'example2' do
         
     | 
| 
       96 
     | 
    
         
            -
                  group_by :day
         
     | 
| 
       97 
     | 
    
         
            -
                  aggregation_field :field2
         
     | 
| 
       98 
     | 
    
         
            -
                end
         
     | 
| 
       99 
     | 
    
         
            -
              end
         
     | 
| 
       100 
     | 
    
         
            -
             
     | 
| 
       101 
104 
     | 
    
         
             
              describe '.aggregate' do
         
     | 
| 
       102 
105 
     | 
    
         
             
                it 'aggregates all defined groups in the report class' do
         
     | 
| 
       103 
106 
     | 
    
         
             
                  klass.create(day: today        , field1: 1 , field2: 2)
         
     | 
| 
         @@ -105,7 +108,21 @@ describe Mongoid::Report do 
     | 
|
| 
       105 
108 
     | 
    
         
             
                  klass.create(day: yesterday    , field1: 1 , field2: 2)
         
     | 
| 
       106 
109 
     | 
    
         
             
                  klass.create(day: two_days_ago , field1: 1 , field2: 2)
         
     | 
| 
       107 
110 
     | 
    
         | 
| 
       108 
     | 
    
         
            -
                   
     | 
| 
      
 111 
     | 
    
         
            +
                  Report = Class.new do
         
     | 
| 
      
 112 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 113 
     | 
    
         
            +
             
     | 
| 
      
 114 
     | 
    
         
            +
                    attach_to Model, as: 'example1' do
         
     | 
| 
      
 115 
     | 
    
         
            +
                      group_by :day
         
     | 
| 
      
 116 
     | 
    
         
            +
                      column :field1
         
     | 
| 
      
 117 
     | 
    
         
            +
                    end
         
     | 
| 
      
 118 
     | 
    
         
            +
             
     | 
| 
      
 119 
     | 
    
         
            +
                    attach_to Model, as: 'example2' do
         
     | 
| 
      
 120 
     | 
    
         
            +
                      group_by :day
         
     | 
| 
      
 121 
     | 
    
         
            +
                      column :field2
         
     | 
| 
      
 122 
     | 
    
         
            +
                    end
         
     | 
| 
      
 123 
     | 
    
         
            +
                  end
         
     | 
| 
      
 124 
     | 
    
         
            +
             
     | 
| 
      
 125 
     | 
    
         
            +
                  example = Report.new
         
     | 
| 
       109 
126 
     | 
    
         
             
                  scope = example.aggregate
         
     | 
| 
       110 
127 
     | 
    
         
             
                  scope
         
     | 
| 
       111 
128 
     | 
    
         
             
                    .query('$match' => { :day  => { '$gte' => yesterday.mongoize, '$lte' => today.mongoize } })
         
     | 
| 
         @@ -128,29 +145,29 @@ describe Mongoid::Report do 
     | 
|
| 
       128 
145 
     | 
    
         
             
                  expect(rows[1]['day']).to eq(yesterday)
         
     | 
| 
       129 
146 
     | 
    
         
             
                end
         
     | 
| 
       130 
147 
     | 
    
         | 
| 
       131 
     | 
    
         
            -
                class Report8
         
     | 
| 
       132 
     | 
    
         
            -
                  include Mongoid::Report
         
     | 
| 
       133 
     | 
    
         
            -
             
     | 
| 
       134 
     | 
    
         
            -
                  report 'example' do
         
     | 
| 
       135 
     | 
    
         
            -
                    attach_to Model, as: 'model1' do
         
     | 
| 
       136 
     | 
    
         
            -
                      group_by :day
         
     | 
| 
       137 
     | 
    
         
            -
                      aggregation_field :field1
         
     | 
| 
       138 
     | 
    
         
            -
                    end
         
     | 
| 
       139 
     | 
    
         
            -
             
     | 
| 
       140 
     | 
    
         
            -
                    attach_to Model, as: 'model2' do
         
     | 
| 
       141 
     | 
    
         
            -
                      group_by :day
         
     | 
| 
       142 
     | 
    
         
            -
                      aggregation_field :field2
         
     | 
| 
       143 
     | 
    
         
            -
                    end
         
     | 
| 
       144 
     | 
    
         
            -
                  end
         
     | 
| 
       145 
     | 
    
         
            -
                end
         
     | 
| 
       146 
     | 
    
         
            -
             
     | 
| 
       147 
148 
     | 
    
         
             
                it 'should still aggregate with combined report' do
         
     | 
| 
       148 
149 
     | 
    
         
             
                  klass.create(day: today        , field1: 1 , field2: 2)
         
     | 
| 
       149 
150 
     | 
    
         
             
                  klass.create(day: today        , field1: 1 , field2: 2)
         
     | 
| 
       150 
151 
     | 
    
         
             
                  klass.create(day: yesterday    , field1: 1 , field2: 2)
         
     | 
| 
       151 
152 
     | 
    
         
             
                  klass.create(day: two_days_ago , field1: 1 , field2: 2)
         
     | 
| 
       152 
153 
     | 
    
         | 
| 
       153 
     | 
    
         
            -
                   
     | 
| 
      
 154 
     | 
    
         
            +
                  Report = Class.new do
         
     | 
| 
      
 155 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 156 
     | 
    
         
            +
             
     | 
| 
      
 157 
     | 
    
         
            +
                    report 'example' do
         
     | 
| 
      
 158 
     | 
    
         
            +
                      attach_to Model, as: 'model1' do
         
     | 
| 
      
 159 
     | 
    
         
            +
                        group_by :day
         
     | 
| 
      
 160 
     | 
    
         
            +
                        column :field1
         
     | 
| 
      
 161 
     | 
    
         
            +
                      end
         
     | 
| 
      
 162 
     | 
    
         
            +
             
     | 
| 
      
 163 
     | 
    
         
            +
                      attach_to Model, as: 'model2' do
         
     | 
| 
      
 164 
     | 
    
         
            +
                        group_by :day
         
     | 
| 
      
 165 
     | 
    
         
            +
                        column :field2
         
     | 
| 
      
 166 
     | 
    
         
            +
                      end
         
     | 
| 
      
 167 
     | 
    
         
            +
                    end
         
     | 
| 
      
 168 
     | 
    
         
            +
                  end
         
     | 
| 
      
 169 
     | 
    
         
            +
                  example = Report.new
         
     | 
| 
      
 170 
     | 
    
         
            +
             
     | 
| 
       154 
171 
     | 
    
         
             
                  scope = example.aggregate
         
     | 
| 
       155 
172 
     | 
    
         
             
                  scope
         
     | 
| 
       156 
173 
     | 
    
         
             
                    .query('$match' => { :day  => { '$gte' => yesterday.mongoize, '$lte' => today.mongoize } })
         
     | 
| 
         @@ -173,29 +190,29 @@ describe Mongoid::Report do 
     | 
|
| 
       173 
190 
     | 
    
         
             
                  expect(rows[1]['day']).to eq(yesterday)
         
     | 
| 
       174 
191 
     | 
    
         
             
                end
         
     | 
| 
       175 
192 
     | 
    
         | 
| 
       176 
     | 
    
         
            -
                class Report11
         
     | 
| 
       177 
     | 
    
         
            -
                  include Mongoid::Report
         
     | 
| 
       178 
     | 
    
         
            -
             
     | 
| 
       179 
     | 
    
         
            -
                  report 'example' do
         
     | 
| 
       180 
     | 
    
         
            -
                    attach_to Model, as: 'model1' do
         
     | 
| 
       181 
     | 
    
         
            -
                      group_by :day
         
     | 
| 
       182 
     | 
    
         
            -
                      aggregation_field :field1, as: 'new-field1'
         
     | 
| 
       183 
     | 
    
         
            -
                    end
         
     | 
| 
       184 
     | 
    
         
            -
             
     | 
| 
       185 
     | 
    
         
            -
                    attach_to Model, as: 'model2' do
         
     | 
| 
       186 
     | 
    
         
            -
                      group_by :day
         
     | 
| 
       187 
     | 
    
         
            -
                      aggregation_field :field2
         
     | 
| 
       188 
     | 
    
         
            -
                    end
         
     | 
| 
       189 
     | 
    
         
            -
                  end
         
     | 
| 
       190 
     | 
    
         
            -
                end
         
     | 
| 
       191 
     | 
    
         
            -
             
     | 
| 
       192 
193 
     | 
    
         
             
                it 'should still aggregate with combined report and project using the new names' do
         
     | 
| 
       193 
194 
     | 
    
         
             
                  klass.create(day: today        , field1: 1 , field2: 2)
         
     | 
| 
       194 
195 
     | 
    
         
             
                  klass.create(day: today        , field1: 1 , field2: 2)
         
     | 
| 
       195 
196 
     | 
    
         
             
                  klass.create(day: yesterday    , field1: 1 , field2: 2)
         
     | 
| 
       196 
197 
     | 
    
         
             
                  klass.create(day: two_days_ago , field1: 1 , field2: 2)
         
     | 
| 
       197 
198 
     | 
    
         | 
| 
       198 
     | 
    
         
            -
                   
     | 
| 
      
 199 
     | 
    
         
            +
                  Report = Class.new do
         
     | 
| 
      
 200 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 201 
     | 
    
         
            +
             
     | 
| 
      
 202 
     | 
    
         
            +
                    report 'example' do
         
     | 
| 
      
 203 
     | 
    
         
            +
                      attach_to Model, as: 'model1' do
         
     | 
| 
      
 204 
     | 
    
         
            +
                        group_by :day
         
     | 
| 
      
 205 
     | 
    
         
            +
                        column :field1, as: 'new-field1'
         
     | 
| 
      
 206 
     | 
    
         
            +
                      end
         
     | 
| 
      
 207 
     | 
    
         
            +
             
     | 
| 
      
 208 
     | 
    
         
            +
                      attach_to Model, as: 'model2' do
         
     | 
| 
      
 209 
     | 
    
         
            +
                        group_by :day
         
     | 
| 
      
 210 
     | 
    
         
            +
                        column :field2
         
     | 
| 
      
 211 
     | 
    
         
            +
                      end
         
     | 
| 
      
 212 
     | 
    
         
            +
                    end
         
     | 
| 
      
 213 
     | 
    
         
            +
                  end
         
     | 
| 
      
 214 
     | 
    
         
            +
                  example = Report.new
         
     | 
| 
      
 215 
     | 
    
         
            +
             
     | 
| 
       199 
216 
     | 
    
         
             
                  scope = example.aggregate
         
     | 
| 
       200 
217 
     | 
    
         
             
                  scope
         
     | 
| 
       201 
218 
     | 
    
         
             
                    .query('$match' => { :day  => { '$gte' => yesterday.mongoize, '$lte' => today.mongoize } })
         
     | 
| 
         @@ -219,19 +236,19 @@ describe Mongoid::Report do 
     | 
|
| 
       219 
236 
     | 
    
         
             
                end
         
     | 
| 
       220 
237 
     | 
    
         
             
              end
         
     | 
| 
       221 
238 
     | 
    
         | 
| 
       222 
     | 
    
         
            -
              class Report15
         
     | 
| 
       223 
     | 
    
         
            -
                include Mongoid::Report
         
     | 
| 
       224 
     | 
    
         
            -
             
     | 
| 
       225 
     | 
    
         
            -
                filter field2: 2, for: Model
         
     | 
| 
       226 
     | 
    
         
            -
                aggregation_field :field1, for: Model
         
     | 
| 
       227 
     | 
    
         
            -
              end
         
     | 
| 
       228 
     | 
    
         
            -
             
     | 
| 
       229 
239 
     | 
    
         
             
              describe '.filter' do
         
     | 
| 
       230 
240 
     | 
    
         
             
                it 'creates filter' do
         
     | 
| 
       231 
241 
     | 
    
         
             
                  klass.create(field1: 1, field2: 2)
         
     | 
| 
       232 
242 
     | 
    
         
             
                  klass.create(field1: 3, field2: 4)
         
     | 
| 
       233 
243 
     | 
    
         | 
| 
       234 
     | 
    
         
            -
                   
     | 
| 
      
 244 
     | 
    
         
            +
                  Report = Class.new do
         
     | 
| 
      
 245 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 246 
     | 
    
         
            +
             
     | 
| 
      
 247 
     | 
    
         
            +
                    filter field2: 2, for: Model
         
     | 
| 
      
 248 
     | 
    
         
            +
                    column :field1, for: Model
         
     | 
| 
      
 249 
     | 
    
         
            +
                  end
         
     | 
| 
      
 250 
     | 
    
         
            +
                  example = Report.new
         
     | 
| 
      
 251 
     | 
    
         
            +
             
     | 
| 
       235 
252 
     | 
    
         
             
                  scope = example.aggregate
         
     | 
| 
       236 
253 
     | 
    
         
             
                  scope = scope.all
         
     | 
| 
       237 
254 
     | 
    
         | 
| 
         @@ -240,22 +257,22 @@ describe Mongoid::Report do 
     | 
|
| 
       240 
257 
     | 
    
         
             
                  expect(rows[0]['field1']).to eq(1)
         
     | 
| 
       241 
258 
     | 
    
         
             
                end
         
     | 
| 
       242 
259 
     | 
    
         | 
| 
       243 
     | 
    
         
            -
                class Report16
         
     | 
| 
       244 
     | 
    
         
            -
                  include Mongoid::Report
         
     | 
| 
       245 
     | 
    
         
            -
             
     | 
| 
       246 
     | 
    
         
            -
                  report 'example' do
         
     | 
| 
       247 
     | 
    
         
            -
                    attach_to Model do
         
     | 
| 
       248 
     | 
    
         
            -
                      filter field2: 2
         
     | 
| 
       249 
     | 
    
         
            -
                      aggregation_field :field1
         
     | 
| 
       250 
     | 
    
         
            -
                    end
         
     | 
| 
       251 
     | 
    
         
            -
                  end
         
     | 
| 
       252 
     | 
    
         
            -
                end
         
     | 
| 
       253 
     | 
    
         
            -
             
     | 
| 
       254 
260 
     | 
    
         
             
                it 'creates filter in report scope' do
         
     | 
| 
       255 
261 
     | 
    
         
             
                  klass.create(field1: 1, field2: 2)
         
     | 
| 
       256 
262 
     | 
    
         
             
                  klass.create(field1: 3, field2: 4)
         
     | 
| 
       257 
263 
     | 
    
         | 
| 
       258 
     | 
    
         
            -
                   
     | 
| 
      
 264 
     | 
    
         
            +
                  Report = Class.new do
         
     | 
| 
      
 265 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 266 
     | 
    
         
            +
             
     | 
| 
      
 267 
     | 
    
         
            +
                    report 'example' do
         
     | 
| 
      
 268 
     | 
    
         
            +
                      attach_to Model do
         
     | 
| 
      
 269 
     | 
    
         
            +
                        filter field2: 2
         
     | 
| 
      
 270 
     | 
    
         
            +
                        column :field1
         
     | 
| 
      
 271 
     | 
    
         
            +
                      end
         
     | 
| 
      
 272 
     | 
    
         
            +
                    end
         
     | 
| 
      
 273 
     | 
    
         
            +
                  end
         
     | 
| 
      
 274 
     | 
    
         
            +
             
     | 
| 
      
 275 
     | 
    
         
            +
                  example = Report.new
         
     | 
| 
       259 
276 
     | 
    
         
             
                  scope = example.aggregate
         
     | 
| 
       260 
277 
     | 
    
         
             
                  scope = scope.all
         
     | 
| 
       261 
278 
     | 
    
         | 
| 
         @@ -264,24 +281,24 @@ describe Mongoid::Report do 
     | 
|
| 
       264 
281 
     | 
    
         
             
                  expect(rows[0]['field1']).to eq(1)
         
     | 
| 
       265 
282 
     | 
    
         
             
                end
         
     | 
| 
       266 
283 
     | 
    
         | 
| 
       267 
     | 
    
         
            -
                class Report17
         
     | 
| 
       268 
     | 
    
         
            -
                  include Mongoid::Report
         
     | 
| 
       269 
     | 
    
         
            -
             
     | 
| 
       270 
     | 
    
         
            -
                  report 'example' do
         
     | 
| 
       271 
     | 
    
         
            -
                    attach_to Model do
         
     | 
| 
       272 
     | 
    
         
            -
                      filter field2: 2,
         
     | 
| 
       273 
     | 
    
         
            -
                             day: ->(context) { Date.parse("20-12-2004").mongoize }
         
     | 
| 
       274 
     | 
    
         
            -
                      aggregation_field :field1
         
     | 
| 
       275 
     | 
    
         
            -
                    end
         
     | 
| 
       276 
     | 
    
         
            -
                  end
         
     | 
| 
       277 
     | 
    
         
            -
                end
         
     | 
| 
       278 
     | 
    
         
            -
             
     | 
| 
       279 
284 
     | 
    
         
             
                it 'creates filter in report scope' do
         
     | 
| 
       280 
285 
     | 
    
         
             
                  klass.create(day: today     , field1: 1 , field2: 2)
         
     | 
| 
       281 
286 
     | 
    
         
             
                  klass.create(day: yesterday , field1: 1 , field2: 2)
         
     | 
| 
       282 
287 
     | 
    
         
             
                  klass.create(day: today     , field1: 3 , field2: 4)
         
     | 
| 
       283 
288 
     | 
    
         | 
| 
       284 
     | 
    
         
            -
                   
     | 
| 
      
 289 
     | 
    
         
            +
                  Report = Class.new do
         
     | 
| 
      
 290 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 291 
     | 
    
         
            +
             
     | 
| 
      
 292 
     | 
    
         
            +
                    report 'example' do
         
     | 
| 
      
 293 
     | 
    
         
            +
                      attach_to Model do
         
     | 
| 
      
 294 
     | 
    
         
            +
                        filter field2: 2,
         
     | 
| 
      
 295 
     | 
    
         
            +
                          day: ->(context) { Date.parse("20-12-2004").mongoize }
         
     | 
| 
      
 296 
     | 
    
         
            +
                        column :field1
         
     | 
| 
      
 297 
     | 
    
         
            +
                      end
         
     | 
| 
      
 298 
     | 
    
         
            +
                    end
         
     | 
| 
      
 299 
     | 
    
         
            +
                  end
         
     | 
| 
      
 300 
     | 
    
         
            +
                  example = Report.new
         
     | 
| 
      
 301 
     | 
    
         
            +
             
     | 
| 
       285 
302 
     | 
    
         
             
                  scope = example.aggregate
         
     | 
| 
       286 
303 
     | 
    
         
             
                  scope = scope.all
         
     | 
| 
       287 
304 
     | 
    
         | 
| 
         @@ -290,29 +307,29 @@ describe Mongoid::Report do 
     | 
|
| 
       290 
307 
     | 
    
         
             
                  expect(rows[0]['field1']).to eq(1)
         
     | 
| 
       291 
308 
     | 
    
         
             
                end
         
     | 
| 
       292 
309 
     | 
    
         | 
| 
       293 
     | 
    
         
            -
                class Report18
         
     | 
| 
       294 
     | 
    
         
            -
                  include Mongoid::Report
         
     | 
| 
       295 
     | 
    
         
            -
             
     | 
| 
       296 
     | 
    
         
            -
                  def values
         
     | 
| 
       297 
     | 
    
         
            -
                    [1, 2]
         
     | 
| 
       298 
     | 
    
         
            -
                  end
         
     | 
| 
       299 
     | 
    
         
            -
             
     | 
| 
       300 
     | 
    
         
            -
                  report 'example' do
         
     | 
| 
       301 
     | 
    
         
            -
                    attach_to Model do
         
     | 
| 
       302 
     | 
    
         
            -
                      group_by :day
         
     | 
| 
       303 
     | 
    
         
            -
                      filter field2: ->(context) { { '$in' => context.values } }
         
     | 
| 
       304 
     | 
    
         
            -
                      aggregation_field :field1
         
     | 
| 
       305 
     | 
    
         
            -
                    end
         
     | 
| 
       306 
     | 
    
         
            -
                  end
         
     | 
| 
       307 
     | 
    
         
            -
                end
         
     | 
| 
       308 
     | 
    
         
            -
             
     | 
| 
       309 
310 
     | 
    
         
             
                it 'creates filter in report scope' do
         
     | 
| 
       310 
311 
     | 
    
         
             
                  klass.create(day: today     , field1: 1 , field2: 2)
         
     | 
| 
       311 
312 
     | 
    
         
             
                  klass.create(day: today     , field1: 1 , field2: 2)
         
     | 
| 
       312 
313 
     | 
    
         
             
                  klass.create(day: yesterday , field1: 1 , field2: 2)
         
     | 
| 
       313 
314 
     | 
    
         
             
                  klass.create(day: today     , field1: 3 , field2: 4)
         
     | 
| 
       314 
315 
     | 
    
         | 
| 
       315 
     | 
    
         
            -
                   
     | 
| 
      
 316 
     | 
    
         
            +
                  Report = Class.new do
         
     | 
| 
      
 317 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 318 
     | 
    
         
            +
             
     | 
| 
      
 319 
     | 
    
         
            +
                    def values
         
     | 
| 
      
 320 
     | 
    
         
            +
                      [1, 2]
         
     | 
| 
      
 321 
     | 
    
         
            +
                    end
         
     | 
| 
      
 322 
     | 
    
         
            +
             
     | 
| 
      
 323 
     | 
    
         
            +
                    report 'example' do
         
     | 
| 
      
 324 
     | 
    
         
            +
                      attach_to Model do
         
     | 
| 
      
 325 
     | 
    
         
            +
                        group_by :day
         
     | 
| 
      
 326 
     | 
    
         
            +
                        filter field2: ->(context) { { '$in' => context.values } }
         
     | 
| 
      
 327 
     | 
    
         
            +
                        column :field1
         
     | 
| 
      
 328 
     | 
    
         
            +
                      end
         
     | 
| 
      
 329 
     | 
    
         
            +
                    end
         
     | 
| 
      
 330 
     | 
    
         
            +
                  end
         
     | 
| 
      
 331 
     | 
    
         
            +
                  example = Report.new
         
     | 
| 
      
 332 
     | 
    
         
            +
             
     | 
| 
       316 
333 
     | 
    
         
             
                  scope = example.aggregate
         
     | 
| 
       317 
334 
     | 
    
         
             
                  scope = scope.all
         
     | 
| 
       318 
335 
     | 
    
         | 
| 
         @@ -12,8 +12,8 @@ describe Mongoid::Report do 
     | 
|
| 
       12 
12 
     | 
    
         
             
                  report 'example' do
         
     | 
| 
       13 
13 
     | 
    
         
             
                    attach_to Model do
         
     | 
| 
       14 
14 
     | 
    
         
             
                      group_by :day
         
     | 
| 
       15 
     | 
    
         
            -
                       
     | 
| 
       16 
     | 
    
         
            -
                      column  
     | 
| 
      
 15 
     | 
    
         
            +
                      columns :'dynamic-field1' => ->(context, row, options) { row['field1'] * 10 }
         
     | 
| 
      
 16 
     | 
    
         
            +
                      column :field1, :'dynamic-field1'
         
     | 
| 
       17 
17 
     | 
    
         
             
                    end
         
     | 
| 
       18 
18 
     | 
    
         
             
                  end
         
     | 
| 
       19 
19 
     | 
    
         
             
                end
         
     | 
| 
         @@ -4,44 +4,60 @@ describe Mongoid::Report::QueriesBuilder do 
     | 
|
| 
       4 
4 
     | 
    
         | 
| 
       5 
5 
     | 
    
         
             
              describe '.queries' do
         
     | 
| 
       6 
6 
     | 
    
         
             
                it 'builds queries for aggregation' do
         
     | 
| 
       7 
     | 
    
         
            -
                   
     | 
| 
      
 7 
     | 
    
         
            +
                  Report = Class.new do
         
     | 
| 
      
 8 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 9 
     | 
    
         
            +
                    column :field1, for: Model
         
     | 
| 
      
 10 
     | 
    
         
            +
                  end
         
     | 
| 
      
 11 
     | 
    
         
            +
                  report = Report.new
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                  queries = report.queries(Model)
         
     | 
| 
       8 
14 
     | 
    
         
             
                  expect(queries.size).to eq(3)
         
     | 
| 
       9 
15 
     | 
    
         
             
                  expect(queries[0]).to eq(
         
     | 
| 
       10 
16 
     | 
    
         
             
                    '$project' => {
         
     | 
| 
       11 
17 
     | 
    
         
             
                      :_id    => 1,
         
     | 
| 
       12 
     | 
    
         
            -
                       
     | 
| 
      
 18 
     | 
    
         
            +
                      'field1' => 1,
         
     | 
| 
       13 
19 
     | 
    
         
             
                    })
         
     | 
| 
       14 
20 
     | 
    
         
             
                  expect(queries[1]).to eq(
         
     | 
| 
       15 
21 
     | 
    
         
             
                    '$group' => {
         
     | 
| 
       16 
22 
     | 
    
         
             
                      :_id    => { },
         
     | 
| 
       17 
     | 
    
         
            -
                       
     | 
| 
      
 23 
     | 
    
         
            +
                      'field1' => { '$sum'  => '$field1' },
         
     | 
| 
       18 
24 
     | 
    
         
             
                    })
         
     | 
| 
       19 
25 
     | 
    
         
             
                  expect(queries[2]).to eq(
         
     | 
| 
       20 
26 
     | 
    
         
             
                    '$project' => {
         
     | 
| 
       21 
27 
     | 
    
         
             
                      :_id    => 0,
         
     | 
| 
       22 
     | 
    
         
            -
                       
     | 
| 
      
 28 
     | 
    
         
            +
                      'field1' => '$field1',
         
     | 
| 
       23 
29 
     | 
    
         
             
                    })
         
     | 
| 
       24 
30 
     | 
    
         
             
                end
         
     | 
| 
       25 
31 
     | 
    
         | 
| 
       26 
32 
     | 
    
         
             
                it 'builds queries using custom one group' do
         
     | 
| 
       27 
     | 
    
         
            -
                   
     | 
| 
      
 33 
     | 
    
         
            +
                  Report = Class.new do
         
     | 
| 
      
 34 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
                    attach_to Model do
         
     | 
| 
      
 37 
     | 
    
         
            +
                      group_by :day
         
     | 
| 
      
 38 
     | 
    
         
            +
                      column :field1
         
     | 
| 
      
 39 
     | 
    
         
            +
                    end
         
     | 
| 
      
 40 
     | 
    
         
            +
                  end
         
     | 
| 
      
 41 
     | 
    
         
            +
                  report = Report.new
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
                  queries = report.queries(Model)
         
     | 
| 
       28 
44 
     | 
    
         
             
                  expect(queries.size).to eq(3)
         
     | 
| 
       29 
45 
     | 
    
         
             
                  expect(queries[0]).to eq(
         
     | 
| 
       30 
46 
     | 
    
         
             
                    '$project' => {
         
     | 
| 
       31 
     | 
    
         
            -
                      :_id 
     | 
| 
       32 
     | 
    
         
            -
                       
     | 
| 
       33 
     | 
    
         
            -
                       
     | 
| 
      
 47 
     | 
    
         
            +
                      :_id     => 1,
         
     | 
| 
      
 48 
     | 
    
         
            +
                      'field1' => 1,
         
     | 
| 
      
 49 
     | 
    
         
            +
                      'day'    => 1,
         
     | 
| 
       34 
50 
     | 
    
         
             
                    })
         
     | 
| 
       35 
51 
     | 
    
         
             
                  expect(queries[1]).to eq(
         
     | 
| 
       36 
52 
     | 
    
         
             
                    '$group' => {
         
     | 
| 
       37 
     | 
    
         
            -
                      :_id    => {  
     | 
| 
       38 
     | 
    
         
            -
                       
     | 
| 
      
 53 
     | 
    
         
            +
                      :_id    => { 'day' => '$day' },
         
     | 
| 
      
 54 
     | 
    
         
            +
                      'field1' => { '$sum' => '$field1' },
         
     | 
| 
       39 
55 
     | 
    
         
             
                    })
         
     | 
| 
       40 
56 
     | 
    
         
             
                  expect(queries[2]).to eq(
         
     | 
| 
       41 
57 
     | 
    
         
             
                    '$project' => {
         
     | 
| 
       42 
     | 
    
         
            -
                      :_id 
     | 
| 
       43 
     | 
    
         
            -
                       
     | 
| 
       44 
     | 
    
         
            -
                       
     | 
| 
      
 58 
     | 
    
         
            +
                      :_id     => 0,
         
     | 
| 
      
 59 
     | 
    
         
            +
                      'day'    => '$_id.day',
         
     | 
| 
      
 60 
     | 
    
         
            +
                      'field1' => '$field1',
         
     | 
| 
       45 
61 
     | 
    
         
             
                    })
         
     | 
| 
       46 
62 
     | 
    
         
             
                end
         
     | 
| 
       47 
63 
     | 
    
         | 
| 
         @@ -51,7 +67,7 @@ describe Mongoid::Report::QueriesBuilder do 
     | 
|
| 
       51 
67 
     | 
    
         
             
                  attach_to Model do
         
     | 
| 
       52 
68 
     | 
    
         
             
                    group_by :day, :field2
         
     | 
| 
       53 
69 
     | 
    
         | 
| 
       54 
     | 
    
         
            -
                     
     | 
| 
      
 70 
     | 
    
         
            +
                    column :field1, :field3
         
     | 
| 
       55 
71 
     | 
    
         
             
                  end
         
     | 
| 
       56 
72 
     | 
    
         
             
                end
         
     | 
| 
       57 
73 
     | 
    
         | 
| 
         @@ -60,25 +76,25 @@ describe Mongoid::Report::QueriesBuilder do 
     | 
|
| 
       60 
76 
     | 
    
         
             
                  expect(queries.size).to eq(3)
         
     | 
| 
       61 
77 
     | 
    
         
             
                  expect(queries[0]).to eq(
         
     | 
| 
       62 
78 
     | 
    
         
             
                    '$project' => {
         
     | 
| 
       63 
     | 
    
         
            -
                      :_id 
     | 
| 
       64 
     | 
    
         
            -
                       
     | 
| 
       65 
     | 
    
         
            -
                       
     | 
| 
       66 
     | 
    
         
            -
                       
     | 
| 
       67 
     | 
    
         
            -
                       
     | 
| 
      
 79 
     | 
    
         
            +
                      :_id     => 1,
         
     | 
| 
      
 80 
     | 
    
         
            +
                      'field1' => 1,
         
     | 
| 
      
 81 
     | 
    
         
            +
                      'field3' => 1,
         
     | 
| 
      
 82 
     | 
    
         
            +
                      'day'    => 1,
         
     | 
| 
      
 83 
     | 
    
         
            +
                      'field2' => 1,
         
     | 
| 
       68 
84 
     | 
    
         
             
                    })
         
     | 
| 
       69 
85 
     | 
    
         
             
                  expect(queries[1]).to eq(
         
     | 
| 
       70 
86 
     | 
    
         
             
                    '$group' => {
         
     | 
| 
       71 
     | 
    
         
            -
                      :_id    => {  
     | 
| 
       72 
     | 
    
         
            -
                       
     | 
| 
       73 
     | 
    
         
            -
                       
     | 
| 
      
 87 
     | 
    
         
            +
                      :_id    => { 'day' => '$day', 'field2' => '$field2' },
         
     | 
| 
      
 88 
     | 
    
         
            +
                      'field1' => { '$sum' => '$field1' },
         
     | 
| 
      
 89 
     | 
    
         
            +
                      'field3' => { '$sum' => '$field3' },
         
     | 
| 
       74 
90 
     | 
    
         
             
                    })
         
     | 
| 
       75 
91 
     | 
    
         
             
                  expect(queries[2]).to eq(
         
     | 
| 
       76 
92 
     | 
    
         
             
                    '$project' => {
         
     | 
| 
       77 
     | 
    
         
            -
                      :_id 
     | 
| 
       78 
     | 
    
         
            -
                       
     | 
| 
       79 
     | 
    
         
            -
                       
     | 
| 
       80 
     | 
    
         
            -
                       
     | 
| 
       81 
     | 
    
         
            -
                       
     | 
| 
      
 93 
     | 
    
         
            +
                      :_id     => 0,
         
     | 
| 
      
 94 
     | 
    
         
            +
                      'day'    => '$_id.day',
         
     | 
| 
      
 95 
     | 
    
         
            +
                      'field2' => '$_id.field2',
         
     | 
| 
      
 96 
     | 
    
         
            +
                      'field1' => '$field1',
         
     | 
| 
      
 97 
     | 
    
         
            +
                      'field3' => '$field3',
         
     | 
| 
       82 
98 
     | 
    
         
             
                    })
         
     | 
| 
       83 
99 
     | 
    
         
             
                end
         
     | 
| 
       84 
100 
     | 
    
         
             
              end
         
     | 
| 
         @@ -0,0 +1,30 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe Mongoid::Report do
         
     | 
| 
      
 4 
     | 
    
         
            +
              let(:klass) { Model }
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
              it 'allows to save options per report and attached model' do
         
     | 
| 
      
 7 
     | 
    
         
            +
                2.times { klass.create!(field1: 1) }
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
                Report = Class.new do
         
     | 
| 
      
 10 
     | 
    
         
            +
                  include Mongoid::Report
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                  report 'example' do
         
     | 
| 
      
 13 
     | 
    
         
            +
                    attach_to Model do
         
     | 
| 
      
 14 
     | 
    
         
            +
                      columns :'new-field' => ->(context, row, options) { row[context.mapping('example-models')['field3'].to_s] }
         
     | 
| 
      
 15 
     | 
    
         
            +
                      mapping :'field3' => :field1
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                      column :field1, :'new-field'
         
     | 
| 
      
 18 
     | 
    
         
            +
                    end
         
     | 
| 
      
 19 
     | 
    
         
            +
                  end
         
     | 
| 
      
 20 
     | 
    
         
            +
                end
         
     | 
| 
      
 21 
     | 
    
         
            +
                example = Report.new
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                rows = example.aggregate_for('example-models')
         
     | 
| 
      
 24 
     | 
    
         
            +
                rows = rows.all
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                expect(rows.size).to eq(1)
         
     | 
| 
      
 27 
     | 
    
         
            +
                expect(rows[0]['field1']).to eq(2)
         
     | 
| 
      
 28 
     | 
    
         
            +
                expect(rows[0]['new-field']).to eq(2)
         
     | 
| 
      
 29 
     | 
    
         
            +
              end
         
     | 
| 
      
 30 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -12,12 +12,17 @@ describe Mongoid::Report do 
     | 
|
| 
       12 
12 
     | 
    
         
             
                  klass.create!(day: today     , field1: 1)
         
     | 
| 
       13 
13 
     | 
    
         
             
                  klass.create!(day: yesterday , field1: 1)
         
     | 
| 
       14 
14 
     | 
    
         | 
| 
       15 
     | 
    
         
            -
                   
     | 
| 
      
 15 
     | 
    
         
            +
                  Report = Class.new do
         
     | 
| 
      
 16 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 17 
     | 
    
         
            +
                    group_by :day, for: Model
         
     | 
| 
      
 18 
     | 
    
         
            +
                    column :field1, for: Model
         
     | 
| 
      
 19 
     | 
    
         
            +
                  end
         
     | 
| 
      
 20 
     | 
    
         
            +
                  example = Report.new
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
       16 
22 
     | 
    
         
             
                  rows = example.aggregate_for(klass)
         
     | 
| 
       17 
23 
     | 
    
         
             
                  rows = rows.all
         
     | 
| 
       18 
24 
     | 
    
         | 
| 
       19 
25 
     | 
    
         
             
                  expect(rows.count).to eq(2)
         
     | 
| 
       20 
     | 
    
         
            -
                  expect(rows.summary[:field1]).to eq(3)
         
     | 
| 
       21 
26 
     | 
    
         
             
                  expect(rows.summary['field1']).to eq(3)
         
     | 
| 
       22 
27 
     | 
    
         
             
                end
         
     | 
| 
       23 
28 
     | 
    
         | 
| 
         @@ -25,10 +30,15 @@ describe Mongoid::Report do 
     | 
|
| 
       25 
30 
     | 
    
         
             
                  Report = Class.new do
         
     | 
| 
       26 
31 
     | 
    
         
             
                    include Mongoid::Report
         
     | 
| 
       27 
32 
     | 
    
         | 
| 
      
 33 
     | 
    
         
            +
                    COLUMNS = {
         
     | 
| 
      
 34 
     | 
    
         
            +
                      :'new-field1' => ->(context, row, options) { row['field1'] * 10 },
         
     | 
| 
      
 35 
     | 
    
         
            +
                      :'new-field2' => ->(context, row, options) { row['field1'] * 20 },
         
     | 
| 
      
 36 
     | 
    
         
            +
                    }
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
       28 
38 
     | 
    
         
             
                    report 'example' do
         
     | 
| 
       29 
39 
     | 
    
         
             
                      attach_to Model do
         
     | 
| 
       30 
     | 
    
         
            -
                         
     | 
| 
       31 
     | 
    
         
            -
                        column  
     | 
| 
      
 40 
     | 
    
         
            +
                        columns COLUMNS
         
     | 
| 
      
 41 
     | 
    
         
            +
                        column :field1, 'new-field1'
         
     | 
| 
       32 
42 
     | 
    
         
             
                      end
         
     | 
| 
       33 
43 
     | 
    
         
             
                    end
         
     | 
| 
       34 
44 
     | 
    
         
             
                  end
         
     | 
| 
         @@ -41,8 +51,13 @@ describe Mongoid::Report do 
     | 
|
| 
       41 
51 
     | 
    
         
             
                  rows = example.aggregate_for('example-models')
         
     | 
| 
       42 
52 
     | 
    
         
             
                  rows = rows.all
         
     | 
| 
       43 
53 
     | 
    
         | 
| 
      
 54 
     | 
    
         
            +
                  expect(rows[0].keys.size).to eq(2)
         
     | 
| 
       44 
55 
     | 
    
         
             
                  expect(rows[0]['field1']).to eq(3)
         
     | 
| 
       45 
56 
     | 
    
         
             
                  expect(rows[0]['new-field1']).to eq(30)
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
      
 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)
         
     | 
| 
       46 
61 
     | 
    
         
             
                end
         
     | 
| 
       47 
62 
     | 
    
         
             
              end
         
     | 
| 
       48 
63 
     | 
    
         | 
| 
         @@ -9,11 +9,11 @@ describe Mongoid::Report do 
     | 
|
| 
       9 
9 
     | 
    
         
             
                  include Mongoid::Report
         
     | 
| 
       10 
10 
     | 
    
         | 
| 
       11 
11 
     | 
    
         
             
                  attach_to Model, as: 'field1-aggregation' do
         
     | 
| 
       12 
     | 
    
         
            -
                     
     | 
| 
      
 12 
     | 
    
         
            +
                    column :field1
         
     | 
| 
       13 
13 
     | 
    
         
             
                  end
         
     | 
| 
       14 
14 
     | 
    
         | 
| 
       15 
15 
     | 
    
         
             
                  attach_to Model, as: 'field2-aggregation' do
         
     | 
| 
       16 
     | 
    
         
            -
                     
     | 
| 
      
 16 
     | 
    
         
            +
                    column :field2
         
     | 
| 
       17 
17 
     | 
    
         
             
                  end
         
     | 
| 
       18 
18 
     | 
    
         
             
                end
         
     | 
| 
       19 
19 
     | 
    
         | 
    
        data/spec/mongoid/report_spec.rb
    CHANGED
    
    | 
         @@ -2,89 +2,126 @@ require 'spec_helper' 
     | 
|
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            describe Mongoid::Report do
         
     | 
| 
       4 
4 
     | 
    
         | 
| 
       5 
     | 
    
         
            -
              describe '. 
     | 
| 
      
 5 
     | 
    
         
            +
              describe '.column' do
         
     | 
| 
      
 6 
     | 
    
         
            +
                let(:report_klass) do
         
     | 
| 
      
 7 
     | 
    
         
            +
                  Class.new do
         
     | 
| 
      
 8 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 9 
     | 
    
         
            +
                    column :field1, for: Model
         
     | 
| 
      
 10 
     | 
    
         
            +
                  end
         
     | 
| 
      
 11 
     | 
    
         
            +
                end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
       6 
13 
     | 
    
         
             
                it 'defines aggegration settings' do
         
     | 
| 
       7 
     | 
    
         
            -
                  expect( 
     | 
| 
      
 14 
     | 
    
         
            +
                  expect(report_klass).to be_respond_to(:settings)
         
     | 
| 
       8 
15 
     | 
    
         
             
                end
         
     | 
| 
       9 
16 
     | 
    
         | 
| 
       10 
17 
     | 
    
         
             
                it 'defines aggregation field for specific model to make queries' do
         
     | 
| 
       11 
     | 
    
         
            -
                  fields =  
     | 
| 
       12 
     | 
    
         
            -
                  expect(fields).to eq({ field1 
     | 
| 
      
 18 
     | 
    
         
            +
                  fields = report_klass.fields(Model)
         
     | 
| 
      
 19 
     | 
    
         
            +
                  expect(fields).to eq({ 'field1' => 'field1' })
         
     | 
| 
       13 
20 
     | 
    
         
             
                end
         
     | 
| 
       14 
21 
     | 
    
         
             
              end
         
     | 
| 
       15 
22 
     | 
    
         | 
| 
       16 
     | 
    
         
            -
             
     | 
| 
      
 23 
     | 
    
         
            +
             describe '.attach_to' do
         
     | 
| 
      
 24 
     | 
    
         
            +
                let(:report_klass) do
         
     | 
| 
      
 25 
     | 
    
         
            +
                  Class.new do
         
     | 
| 
      
 26 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 27 
     | 
    
         
            +
                    attach_to Model do
         
     | 
| 
      
 28 
     | 
    
         
            +
                      column :field1
         
     | 
| 
      
 29 
     | 
    
         
            +
                    end
         
     | 
| 
      
 30 
     | 
    
         
            +
                  end
         
     | 
| 
      
 31 
     | 
    
         
            +
                end
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
       17 
33 
     | 
    
         
             
                it 'defines method in report class to attach report to the model' do
         
     | 
| 
       18 
     | 
    
         
            -
                  expect( 
     | 
| 
      
 34 
     | 
    
         
            +
                  expect(report_klass).to be_respond_to(:attach_to)
         
     | 
| 
       19 
35 
     | 
    
         
             
                end
         
     | 
| 
       20 
36 
     | 
    
         | 
| 
       21 
37 
     | 
    
         
             
                it 'defines field in terms of attached model' do
         
     | 
| 
       22 
     | 
    
         
            -
                  fields =  
     | 
| 
       23 
     | 
    
         
            -
                  expect(fields).to eq({ field1 
     | 
| 
      
 38 
     | 
    
         
            +
                  fields = report_klass.fields(Model)
         
     | 
| 
      
 39 
     | 
    
         
            +
                  expect(fields).to eq({ 'field1' => 'field1' })
         
     | 
| 
       24 
40 
     | 
    
         
             
                end
         
     | 
| 
       25 
41 
     | 
    
         
             
              end
         
     | 
| 
       26 
42 
     | 
    
         | 
| 
       27 
43 
     | 
    
         
             
              describe '.group_by' do
         
     | 
| 
      
 44 
     | 
    
         
            +
                let(:report_klass1) do
         
     | 
| 
      
 45 
     | 
    
         
            +
                  Class.new do
         
     | 
| 
      
 46 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 47 
     | 
    
         
            +
                    group_by :day, for: Model
         
     | 
| 
      
 48 
     | 
    
         
            +
                    column :field1, for: Model
         
     | 
| 
      
 49 
     | 
    
         
            +
                  end
         
     | 
| 
      
 50 
     | 
    
         
            +
                end
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
       28 
52 
     | 
    
         
             
                it 'defines group by method as class method' do
         
     | 
| 
       29 
     | 
    
         
            -
                  expect( 
     | 
| 
      
 53 
     | 
    
         
            +
                  expect(report_klass1).to be_respond_to(:group_by)
         
     | 
| 
       30 
54 
     | 
    
         
             
                end
         
     | 
| 
       31 
55 
     | 
    
         | 
| 
       32 
56 
     | 
    
         
             
                it 'stores group by settings under report class' do
         
     | 
| 
       33 
     | 
    
         
            -
                  group_by_settings =  
     | 
| 
       34 
     | 
    
         
            -
                  expect(group_by_settings).to eq([ 
     | 
| 
      
 57 
     | 
    
         
            +
                  group_by_settings = report_klass1.settings[Model][:group_by]
         
     | 
| 
      
 58 
     | 
    
         
            +
                  expect(group_by_settings).to eq(['day'])
         
     | 
| 
       35 
59 
     | 
    
         
             
                end
         
     | 
| 
       36 
60 
     | 
    
         | 
| 
       37 
     | 
    
         
            -
                 
     | 
| 
       38 
     | 
    
         
            -
                   
     | 
| 
       39 
     | 
    
         
            -
             
     | 
| 
      
 61 
     | 
    
         
            +
                let(:report_klass2) do
         
     | 
| 
      
 62 
     | 
    
         
            +
                  Class.new do
         
     | 
| 
      
 63 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 64 
     | 
    
         
            +
                    attach_to Model do
         
     | 
| 
      
 65 
     | 
    
         
            +
                      group_by :day
         
     | 
| 
      
 66 
     | 
    
         
            +
                      column :field1
         
     | 
| 
      
 67 
     | 
    
         
            +
                    end
         
     | 
| 
      
 68 
     | 
    
         
            +
                  end
         
     | 
| 
       40 
69 
     | 
    
         
             
                end
         
     | 
| 
       41 
     | 
    
         
            -
              end
         
     | 
| 
       42 
     | 
    
         
            -
             
     | 
| 
       43 
     | 
    
         
            -
              class Report6
         
     | 
| 
       44 
     | 
    
         
            -
                include Mongoid::Report
         
     | 
| 
       45 
70 
     | 
    
         | 
| 
       46 
     | 
    
         
            -
                 
     | 
| 
       47 
     | 
    
         
            -
                   
     | 
| 
      
 71 
     | 
    
         
            +
                it 'defines groups in terms of attached model' do
         
     | 
| 
      
 72 
     | 
    
         
            +
                  groups = report_klass2.groups(Model)
         
     | 
| 
      
 73 
     | 
    
         
            +
                  expect(groups).to eq(['day'])
         
     | 
| 
       48 
74 
     | 
    
         
             
                end
         
     | 
| 
       49 
75 
     | 
    
         
             
              end
         
     | 
| 
       50 
76 
     | 
    
         | 
| 
       51 
77 
     | 
    
         
             
              describe '.as' do
         
     | 
| 
      
 78 
     | 
    
         
            +
                let(:report_klass) do
         
     | 
| 
      
 79 
     | 
    
         
            +
                  Class.new do
         
     | 
| 
      
 80 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 81 
     | 
    
         
            +
                    attach_to Model, as: 'example1' do
         
     | 
| 
      
 82 
     | 
    
         
            +
                      column :field1
         
     | 
| 
      
 83 
     | 
    
         
            +
                    end
         
     | 
| 
      
 84 
     | 
    
         
            +
                  end
         
     | 
| 
      
 85 
     | 
    
         
            +
                end
         
     | 
| 
      
 86 
     | 
    
         
            +
             
     | 
| 
       52 
87 
     | 
    
         
             
                it 'creates settings with "as" name' do
         
     | 
| 
       53 
     | 
    
         
            -
                  expect( 
     | 
| 
      
 88 
     | 
    
         
            +
                  expect(report_klass.settings).to have_key('example1')
         
     | 
| 
       54 
89 
     | 
    
         
             
                end
         
     | 
| 
       55 
90 
     | 
    
         
             
              end
         
     | 
| 
       56 
91 
     | 
    
         | 
| 
       57 
     | 
    
         
            -
               
     | 
| 
       58 
     | 
    
         
            -
                 
     | 
| 
       59 
     | 
    
         
            -
             
     | 
| 
       60 
     | 
    
         
            -
             
     | 
| 
       61 
     | 
    
         
            -
             
     | 
| 
       62 
     | 
    
         
            -
             
     | 
| 
       63 
     | 
    
         
            -
             
     | 
| 
       64 
     | 
    
         
            -
             
     | 
| 
       65 
     | 
    
         
            -
             
     | 
| 
       66 
     | 
    
         
            -
             
     | 
| 
      
 92 
     | 
    
         
            +
              describe '.report' do
         
     | 
| 
      
 93 
     | 
    
         
            +
                let(:report_klass) do
         
     | 
| 
      
 94 
     | 
    
         
            +
                  Class.new do
         
     | 
| 
      
 95 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 96 
     | 
    
         
            +
                    report 'example' do
         
     | 
| 
      
 97 
     | 
    
         
            +
                      attach_to Model, as: 'model1' do
         
     | 
| 
      
 98 
     | 
    
         
            +
                        column :field1
         
     | 
| 
      
 99 
     | 
    
         
            +
                      end
         
     | 
| 
      
 100 
     | 
    
         
            +
             
     | 
| 
      
 101 
     | 
    
         
            +
                      attach_to Model do
         
     | 
| 
      
 102 
     | 
    
         
            +
                        column :field1
         
     | 
| 
      
 103 
     | 
    
         
            +
                      end
         
     | 
| 
      
 104 
     | 
    
         
            +
                    end
         
     | 
| 
       67 
105 
     | 
    
         
             
                  end
         
     | 
| 
       68 
106 
     | 
    
         
             
                end
         
     | 
| 
       69 
     | 
    
         
            -
              end
         
     | 
| 
       70 
107 
     | 
    
         | 
| 
       71 
     | 
    
         
            -
              describe '.report' do
         
     | 
| 
       72 
108 
     | 
    
         
             
                it 'creates settings with report-<attached-model-name' do
         
     | 
| 
       73 
     | 
    
         
            -
                  expect( 
     | 
| 
       74 
     | 
    
         
            -
                  expect( 
     | 
| 
      
 109 
     | 
    
         
            +
                  expect(report_klass.settings).to have_key('example-model1')
         
     | 
| 
      
 110 
     | 
    
         
            +
                  expect(report_klass.settings).to have_key("example-#{Model.collection.name}")
         
     | 
| 
       75 
111 
     | 
    
         
             
                end
         
     | 
| 
       76 
112 
     | 
    
         
             
              end
         
     | 
| 
       77 
113 
     | 
    
         | 
| 
       78 
     | 
    
         
            -
               
     | 
| 
       79 
     | 
    
         
            -
                 
     | 
| 
       80 
     | 
    
         
            -
             
     | 
| 
       81 
     | 
    
         
            -
             
     | 
| 
       82 
     | 
    
         
            -
             
     | 
| 
      
 114 
     | 
    
         
            +
              describe '.column `as` option' do
         
     | 
| 
      
 115 
     | 
    
         
            +
                let(:report_klass) do
         
     | 
| 
      
 116 
     | 
    
         
            +
                  Class.new do
         
     | 
| 
      
 117 
     | 
    
         
            +
                    include Mongoid::Report
         
     | 
| 
      
 118 
     | 
    
         
            +
                    column :field1, for: Model, as: 'field-name'
         
     | 
| 
      
 119 
     | 
    
         
            +
                  end
         
     | 
| 
      
 120 
     | 
    
         
            +
                end
         
     | 
| 
       83 
121 
     | 
    
         | 
| 
       84 
     | 
    
         
            -
              describe '.aggregation_field `as` option' do
         
     | 
| 
       85 
122 
     | 
    
         
             
                it 'creates settings with report-<attached-model-name' do
         
     | 
| 
       86 
     | 
    
         
            -
                  expect( 
     | 
| 
       87 
     | 
    
         
            -
                  expect( 
     | 
| 
      
 123 
     | 
    
         
            +
                  expect(report_klass.fields(Model).keys).to eq(['field1'])
         
     | 
| 
      
 124 
     | 
    
         
            +
                  expect(report_klass.fields(Model).values).to eq(['field-name'])
         
     | 
| 
       88 
125 
     | 
    
         
             
                end
         
     | 
| 
       89 
126 
     | 
    
         
             
              end
         
     | 
| 
       90 
127 
     | 
    
         | 
| 
         @@ -95,7 +132,7 @@ describe Mongoid::Report do 
     | 
|
| 
       95 
132 
     | 
    
         
             
                    include Mongoid::Report
         
     | 
| 
       96 
133 
     | 
    
         | 
| 
       97 
134 
     | 
    
         
             
                    attach_to Model do
         
     | 
| 
       98 
     | 
    
         
            -
                       
     | 
| 
      
 135 
     | 
    
         
            +
                      column :field1
         
     | 
| 
       99 
136 
     | 
    
         
             
                    end
         
     | 
| 
       100 
137 
     | 
    
         
             
                  end
         
     | 
| 
       101 
138 
     | 
    
         | 
| 
         @@ -103,7 +140,7 @@ describe Mongoid::Report do 
     | 
|
| 
       103 
140 
     | 
    
         
             
                    include Mongoid::Report
         
     | 
| 
       104 
141 
     | 
    
         | 
| 
       105 
142 
     | 
    
         
             
                    attach_to Model do
         
     | 
| 
       106 
     | 
    
         
            -
                       
     | 
| 
      
 143 
     | 
    
         
            +
                      column :field2
         
     | 
| 
       107 
144 
     | 
    
         
             
                    end
         
     | 
| 
       108 
145 
     | 
    
         
             
                  end
         
     | 
| 
       109 
146 
     | 
    
         | 
| 
         @@ -116,13 +153,13 @@ describe Mongoid::Report do 
     | 
|
| 
       116 
153 
     | 
    
         | 
| 
       117 
154 
     | 
    
         
             
                class ReportKlass1 < ReportKlass
         
     | 
| 
       118 
155 
     | 
    
         
             
                  attach_to Model do
         
     | 
| 
       119 
     | 
    
         
            -
                     
     | 
| 
      
 156 
     | 
    
         
            +
                    column :field1
         
     | 
| 
       120 
157 
     | 
    
         
             
                  end
         
     | 
| 
       121 
158 
     | 
    
         
             
                end
         
     | 
| 
       122 
159 
     | 
    
         | 
| 
       123 
160 
     | 
    
         
             
                class ReportKlass2 < ReportKlass
         
     | 
| 
       124 
161 
     | 
    
         
             
                  attach_to Model do
         
     | 
| 
       125 
     | 
    
         
            -
                     
     | 
| 
      
 162 
     | 
    
         
            +
                    column :field2
         
     | 
| 
       126 
163 
     | 
    
         
             
                  end
         
     | 
| 
       127 
164 
     | 
    
         
             
                end
         
     | 
| 
       128 
165 
     | 
    
         | 
    
        data/spec/support/models.rb
    CHANGED
    
    | 
         @@ -7,35 +7,3 @@ class Model 
     | 
|
| 
       7 
7 
     | 
    
         | 
| 
       8 
8 
     | 
    
         
             
              field :day,    type: Date
         
     | 
| 
       9 
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
    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. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.7
         
     | 
| 
       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- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2014-07-02 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: mongoid
         
     | 
| 
         @@ -85,6 +85,7 @@ files: 
     | 
|
| 
       85 
85 
     | 
    
         
             
            - spec/mongoid/report/column_spec.rb
         
     | 
| 
       86 
86 
     | 
    
         
             
            - spec/mongoid/report/config_spec.rb
         
     | 
| 
       87 
87 
     | 
    
         
             
            - spec/mongoid/report/queries_builder_spec.rb
         
     | 
| 
      
 88 
     | 
    
         
            +
            - spec/mongoid/report/set_spec.rb
         
     | 
| 
       88 
89 
     | 
    
         
             
            - spec/mongoid/report/summary_spec.rb
         
     | 
| 
       89 
90 
     | 
    
         
             
            - spec/mongoid/report/threads_spec.rb
         
     | 
| 
       90 
91 
     | 
    
         
             
            - spec/mongoid/report_spec.rb
         
     | 
| 
         @@ -119,6 +120,7 @@ test_files: 
     | 
|
| 
       119 
120 
     | 
    
         
             
            - spec/mongoid/report/column_spec.rb
         
     | 
| 
       120 
121 
     | 
    
         
             
            - spec/mongoid/report/config_spec.rb
         
     | 
| 
       121 
122 
     | 
    
         
             
            - spec/mongoid/report/queries_builder_spec.rb
         
     | 
| 
      
 123 
     | 
    
         
            +
            - spec/mongoid/report/set_spec.rb
         
     | 
| 
       122 
124 
     | 
    
         
             
            - spec/mongoid/report/summary_spec.rb
         
     | 
| 
       123 
125 
     | 
    
         
             
            - spec/mongoid/report/threads_spec.rb
         
     | 
| 
       124 
126 
     | 
    
         
             
            - spec/mongoid/report_spec.rb
         
     |