module Mongoid::Flaggable::ClassMethods

Public Instance Methods

bulk_add_flag!(flag, conditions = {}) click to toggle source
# File lib/mongoid_flaggable/class_methods.rb, line 58
def bulk_add_flag!(flag, conditions = {})
        where(conditions).add_to_set(:flag_array, flag.to_s)
end
bulk_remove_flag!(flag, conditions = {}) click to toggle source
# File lib/mongoid_flaggable/class_methods.rb, line 62
def bulk_remove_flag!(flag, conditions = {})
        where(conditions).pull(:flag_array, flag.to_s)
end
by_all_flags(*flags) click to toggle source
# File lib/mongoid_flaggable/class_methods.rb, line 37
def by_all_flags(*flags)
        flags.flatten!
        where(:flag_array.all => flags)
end
Also aliased as: by_flag, by_flags
by_any_flags(*flags) click to toggle source
# File lib/mongoid_flaggable/class_methods.rb, line 44
def by_any_flags(*flags)
        flags.flatten!
        where(:flag_array.in => flags)
end
by_flag(*flags)
Alias for: by_all_flags
by_flags(*flags)
Alias for: by_all_flags
distinct_flags() click to toggle source
# File lib/mongoid_flaggable/class_methods.rb, line 66
def distinct_flags
        distinct :flag_array
end
flag_count(*flags) click to toggle source
# File lib/mongoid_flaggable/class_methods.rb, line 49
def flag_count(*flags)
        flags.flatten!
        if flags.size == 1
                where(:flag_array => flags.first).count
        else
                by_all_flags(flags).count
        end
end
flag_frequency() click to toggle source
# File lib/mongoid_flaggable/class_methods.rb, line 4
def flag_frequency
        aggregation = collection.aggregate([
                {
                        '$match' => {
                                'flag_array' => {
                                        '$ne' => nil
                                }
                        }
                },
                {
                        '$project' => {
                                'flag_array' => 1
                        }
                },
                {
                        '$unwind' => '$flag_array'
                },
                {
                        '$group' => {
                                '_id' => '$flag_array',
                                'count' => {
                                        '$sum' => 1
                                }
                        }
                }
        ])
        aggregation.map!(&:values)
        aggregation.sort_by! do |value|
                value.last * -1
        end
        Hash[aggregation]
end