Module: Mongoid::Followable

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid_followable/version.rb,
lib/mongoid_followable/followable.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.1.5"

Instance Method Summary collapse

Instance Method Details

#all_followersObject

get all the followers of this model, same with classmethod followers_of

Example:

>> @ruby.all_followers
=> [@jim]


70
71
72
# File 'lib/mongoid_followable/followable.rb', line 70

def all_followers
  rebuild_instances(self.followers)
end

#ever_followedObject

see model’s followed history

Example:

>> @ruby.ever_followed
=> [@jim]


110
111
112
113
114
115
116
# File 'lib/mongoid_followable/followable.rb', line 110

def ever_followed
  follow = []
  self.followed_history.each do |h|
    follow << h.split('_')[0].constantize.find(h.split('_')[1])
  end
  follow
end

#followee_of?(model) ⇒ Boolean

see if this model is followee of some model

Example:

>> @ruby.followee_of?(@jim)
=> true


60
61
62
# File 'lib/mongoid_followable/followable.rb', line 60

def followee_of?(model)
  0 < self.followers.by_model(model).limit(1).count * model.followees.by_model(self).limit(1).count
end

#followers_by_type(type) ⇒ Object

get all the followers of this model in certain type

Example:

>> @ruby.followers_by_type("user")
=> [@jim]


80
81
82
# File 'lib/mongoid_followable/followable.rb', line 80

def followers_by_type(type)
  rebuild_instances(self.followers.by_type(type))
end

#followers_countObject

get the number of followers

Example:

>> @ruby.followers_count
=> 1


90
91
92
# File 'lib/mongoid_followable/followable.rb', line 90

def followers_count
  self.followers.count
end

#followers_count_by_type(type) ⇒ Object

get the number of followers in certain type

Example:

>> @ruby.followers_count_by_type("user")
=> 1


100
101
102
# File 'lib/mongoid_followable/followable.rb', line 100

def followers_count_by_type(type)
  self.followers.by_type(type).count
end

#set_authorization(*models) ⇒ Object

set which models cannot follow self

Example:

>> @ruby.set_authorization('user')
=> true


40
41
42
43
44
45
# File 'lib/mongoid_followable/followable.rb', line 40

def set_authorization(*models)
  models.each do |model|
    self.cannot_followed << model.capitalize
  end
  self.save
end

#unset_authorization(*models) ⇒ Object



47
48
49
50
51
52
# File 'lib/mongoid_followable/followable.rb', line 47

def unset_authorization(*models)
  models.each do |model|
    self.cannot_followed -= [model.capitalize]
  end
  self.save
end