Module: Mongoid::Report::ClassMethods

Defined in:
lib/mongoid/report.rb

Instance Method Summary collapse

Instance Method Details

#aggregation_field(*fields) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/mongoid/report.rb', line 82

def aggregation_field(*fields)
  define_report_method(*fields) do |columns, report_name, options|
    columns.each do |column|
      name = options.fetch(:as) { column }
      add_field(report_name, column, name)
    end
  end
end

#attach_to(collection, options = {}, &block) ⇒ Object



57
58
59
60
# File 'lib/mongoid/report.rb', line 57

def attach_to(collection, options = {}, &block)
  proxy = AttachProxy.new(self, collection, options)
  proxy.instance_eval(&block)
end

#fields(collection) ⇒ Object



91
92
93
# File 'lib/mongoid/report.rb', line 91

def fields(collection)
  settings_property(collection, :fields, {})
end

#filter(*fields) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mongoid/report.rb', line 62

def filter(*fields)
  define_report_method(*fields) do |_, report_name, options|
    queries = self.settings_property(report_name, :queries)

    options.each do |key, value|
      value = value.call if value.respond_to?(:call)
        queries
          .concat([{
            '$match' => { key => value }
          }])
    end
  end
end

#group_by(*fields) ⇒ Object



76
77
78
79
80
# File 'lib/mongoid/report.rb', line 76

def group_by(*fields)
  define_report_method(*fields) do |groups, report_name, _|
    settings[report_name][:group_by] = groups
  end
end

#groups(collection) ⇒ Object



95
96
97
# File 'lib/mongoid/report.rb', line 95

def groups(collection)
  settings_property(collection, :group_by, [])
end

#report(name, &block) ⇒ Object



52
53
54
55
# File 'lib/mongoid/report.rb', line 52

def report(name, &block)
  proxy = ReportProxy.new(self, name)
  proxy.instance_eval(&block)
end

#settings_property(collection, key, default = []) ⇒ Object



99
100
101
102
103
# File 'lib/mongoid/report.rb', line 99

def settings_property(collection, key, default = [])
  settings
    .fetch(collection) { {} }
    .fetch(key) { default }
end