lib/mongoid/report.rb in mongoid-report-0.0.11 vs lib/mongoid/report.rb in mongoid-report-0.1.0
- old
+ new
@@ -27,11 +27,11 @@
# class.
@queries = builder.do
# Now we have access to compiled queries to run it in aggregation
# framework.
- configuration[:queries] = @queries
+ configuration[:queries].concat(@queries)
end
end
alias :initialize :initialize_report_module
def queries(klass)
@@ -57,10 +57,24 @@
def attach_to(collection, options = {}, &block)
proxy = AttachProxy.new(self, collection, options)
proxy.instance_eval(&block)
end
+ 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
+
def group_by(*fields)
define_report_method(*fields) do |groups, report_name, _|
settings[report_name][:group_by] = groups
end
end
@@ -93,14 +107,16 @@
def define_report_method(*fields)
options = fields.extract_options!
# We should always specify model to attach fields, groups
collection = options.fetch(:for)
+ options.delete(:for)
# If user didn't pass as option to name the report we are using
# collection class as key for settings.
attach_name = options.fetch(:attach_name) { collection }
+ options.delete(:attach_name)
# We should always have for option
initialize_settings_by(attach_name, collection)
# Because of modifying fields(usign exract options method of
@@ -112,9 +128,10 @@
settings[attach_name] ||= settings.fetch(attach_name) do
{
for: collection,
fields: {},
group_by: [],
+ queries: [],
}
end
end
def add_field(attach_name, field, name)