trackoid
trackoid copied to clipboard
It would be nice to allow aggregate key to be of different data type
Currently, an aggregation key must be a String. For example
aggregate :browsers do |b|
b.id # <= This gets automatically casted to a string by calling ".to_s"
end
It would be nice to allow for BSON::ObjectID or DateTime keys. However, this would not be possible without using Inheritance for the Aggregation class model. I can replace the :ns (namespace) field and use Inheritance instead (automatically using the :_type field).
Wouldn't it be possible to do something like this?
Mongoid::Tracking::Aggregates
def aggregate(args*, &block)
*omitted*
name = args.shift
define_aggregate_model(args.extract_options!) if aggregate_klass.nil?
*omitted*
end
def define_aggregate_model(args)
field :key, args
end
And then in the model:
aggregate :sources, type: Symbol do |key|
key.present? ? key.to_s : nil
end