Module: Mongoid::Follower::ClassMethods

Defined in:
lib/mongoid_followable/follower.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

4 methods in this function

Example:

>> User.with_max_followees
=> [@jim]
>> User.with_max_followees_by_type('group')
=> [@jim]


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mongoid_followable/follower.rb', line 40

def method_missing(name, *args)
  if name.to_s =~ /^with_(max|min)_followees$/i
    follow_array = self.all.to_a.sort! { |a, b| a.followees_count <=> b.followees_count }
    if $1 == "max"
      max = follow_array[-1].followees_count
      follow_array.select { |c| c.followees_count == max }
    elsif $1 == "min"
      min = follow_array[0].followees_count
      follow_array.select { |c| c.followees_count == min }
    end
  elsif name.to_s =~ /^with_(max|min)_followees_by_type$/i
    follow_array = self.all.to_a.sort! { |a, b| a.followees_count_by_type(args[0]) <=> b.followees_count_by_type(args[0]) }
    if $1 == "max"
      max = follow_array[-1].followees_count_by_type(args[0])
      follow_array.select { |c| c.followees_count_by_type(args[0]) == max }
    elsif $1 == "min"
      min = follow_array[0].followees_count
      follow_array.select { |c| c.followees_count_by_type(args[0]) == min }
    end
  else
    super
  end
end

Instance Method Details

#followers_of(model) ⇒ Object

get certain model’s followers of this type

Example:

>> @jim = User.new
>> @ruby = Group.new
>> @jim.save
>> @ruby.save

>> @jim.follow(@ruby)
>> User.followers_of(@ruby)
=> [@jim]

Arguments:
  model: instance of some followable model


28
29
30
# File 'lib/mongoid_followable/follower.rb', line 28

def followers_of(model)
  model.followers_by_type(self.name)
end