Module: Mongoid::Flaggable::ClassMethods

Defined in:
lib/mongoid_flaggable/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#bulk_add_flag!(flag, conditions = {}) ⇒ Object



58
59
60
# 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 = {}) ⇒ Object



62
63
64
# 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) ⇒ Object Also known as: by_flag, by_flags



37
38
39
40
# File 'lib/mongoid_flaggable/class_methods.rb', line 37

def by_all_flags(*flags)
  flags.flatten!
  where(:flag_array.all => flags)
end

#by_any_flags(*flags) ⇒ Object



44
45
46
47
# File 'lib/mongoid_flaggable/class_methods.rb', line 44

def by_any_flags(*flags)
  flags.flatten!
  where(:flag_array.in => flags)
end

#distinct_flagsObject



66
67
68
# File 'lib/mongoid_flaggable/class_methods.rb', line 66

def distinct_flags
  distinct :flag_array
end

#flag_count(*flags) ⇒ Object



49
50
51
52
53
54
55
56
# 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_frequencyObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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