Module: Mongoid::Followable::ClassMethods

Defined in:
lib/mongoid_followable/followable.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:

>> Group.with_max_followers
=> [@ruby]
>> Group.with_max_followers_by_type('user')
=> [@ruby]


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/followable.rb', line 40

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

Instance Method Details

#followees_of(model) ⇒ Object

get certain model’s followees of this type

Example:

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

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

Arguments:
  model: instance of some followable model


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

def followees_of(model)
  model.followees_by_type(self.name)
end