module Mongoid::Report::ClassMethods

Public Instance Methods

attach_to(*fields, &block) click to toggle source
# File lib/mongoid/report.rb, line 149
def attach_to(*fields, &block)
  options = fields.extract_options!
  collection = fields[0]

  options.merge!(report_name: options[:as]) if options[:as]

  define_report_method(options.merge(collection: collection)) do
    proxy = AttachProxy.new(self, collection, options)
    proxy.instance_eval(&block)
  end
end
batches(*fields) click to toggle source
# File lib/mongoid/report.rb, line 161
def batches(*fields)
  define_report_method(*fields) do |_, report_module, report_name, batches|
    self.set_settings(report_module, report_name, :batches, batches.stringify_keys!)
  end
end
column(*fields) click to toggle source
# File lib/mongoid/report.rb, line 196
def column(*fields)
  define_report_method(*fields) do |columns, report_module, report_name, _|
    columns.each do |field|
      self.get_settings(report_module, report_name, :fields) << field.to_s
    end
  end
end
columns(*fields) click to toggle source
# File lib/mongoid/report.rb, line 204
def columns(*fields)
  define_report_method(*fields) do |_, report_module, report_name, columns|
    self.set_settings(report_module, report_name, :columns, columns.stringify_keys!)
  end
end
get_settings(report_module, report_name, field) click to toggle source
# File lib/mongoid/report.rb, line 222
def get_settings(report_module, report_name, field)
  unless report_name
    self.settings[report_module][field]
  else
    self.settings[report_module][:reports][report_name][field]
  end
end
group_by(*fields) click to toggle source
# File lib/mongoid/report.rb, line 190
def group_by(*fields)
  define_report_method(*fields) do |groups, report_module, report_name, _|
    self.set_settings(report_module, report_name, :group_by, groups.map(&:to_s))
  end
end
mapping(*fields) click to toggle source
# File lib/mongoid/report.rb, line 210
def mapping(*fields)
  define_report_method(*fields) do |_, report_module, report_name, mapping|
    mapping.stringify_keys!

    mapping.each do |key, value|
      mapping[key] = value.to_s
    end

    self.set_settings(report_module, report_name, :mapping, mapping)
  end
end
match(*fields) click to toggle source
# File lib/mongoid/report.rb, line 167
def match(*fields)
  define_report_method(*fields) do |_, report_module, report_name, options|
    queries = self.get_settings(report_module, report_name, :queries)

    options.each do |key, value|
      queries
        .concat([{
          '$match' => { key => value }
        }])
    end
  end
end
query(*fields) click to toggle source
# File lib/mongoid/report.rb, line 180
def query(*fields)
  define_report_method(*fields) do |_, report_module, report_name, options|
    queries = self.get_settings(report_module, report_name, :queries)

    options.each do |key, value|
      queries.concat([{ key => value }])
    end
  end
end
report(name, &block) click to toggle source
# File lib/mongoid/report.rb, line 144
def report(name, &block)
  proxy = ReportProxy.new(self, name)
  proxy.instance_eval(&block)
end
set_settings(report_module, report_name, field, value) click to toggle source
# File lib/mongoid/report.rb, line 230
def set_settings(report_module, report_name, field, value)
  unless report_name
    self.settings[report_module][field] = value
  else
    self.settings[report_module][:reports][report_name][field] = value
  end
end

Private Instance Methods

define_report_method(*fields) { |fields, report_module, report_name, options || {}| ... } click to toggle source
# File lib/mongoid/report.rb, line 240
def define_report_method(*fields)
  options = fields.extract_options!

  # We should always specify model to attach fields, groups
  collection = options.fetch(:collection)
  options.delete(:collection)

  # In case if user passed mongoid model we should get name of collection
  # instead of using mongoid models. on deep_dup operations it will work
  # find with strings.
  collection = Collections.name(collection)

  report_module = options.delete(:report_module)
  report_module ||= self.name

  report_name = options.delete(:report_name)
  report_name ||= Collections.name(collection)

  # We should always have for option
  initialize_settings_by(report_module, report_name, collection)

  # Because of modifying fields(usign exract options method of
  # ActiveSupport) lets pass fields to the next block with collection.
  yield fields, report_module, report_name, options || {}
end
initialize_settings_by(report_module, report_name, collection) click to toggle source
# File lib/mongoid/report.rb, line 266
def initialize_settings_by(report_module, report_name, collection)
  # Global settings for the report block
  settings[report_module] ||= settings.fetch(report_module) do
    {
      reports:   {},
      fields:    [],
      group_by:  [],
      batches:   {},
      columns:   {},
      mapping:   {},
      # needs to be cloned
      queries:   [],
    }
  end

  return unless report_name

  settings[report_module][:reports][report_name] ||=
    settings[report_module][:reports].fetch(report_name) do
      {
        collection: collection,
        fields:     [],
        group_by:   [],
        batches:    {},
        columns:    {},
        mapping:    {},
        # needs to be cloned
        queries:    [],
      }
    end
end