Module: Mongoid::Follower
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongoid_followable/follower.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#all_followees ⇒ Object
get all the followees of this model, same with classmethod followees_of.
-
#common_followees?(model) ⇒ Boolean
return if there is any common followees.
-
#common_followees_with(model) ⇒ Object
get common followees with some model.
-
#ever_follow ⇒ Object
see user’s follow history.
-
#follow(*models) ⇒ Object
follow some model.
-
#followees_by_type(type) ⇒ Object
get all the followees of this model in certain type.
-
#followees_count ⇒ Object
get the number of followees.
-
#followees_count_by_type(type) ⇒ Object
get the number of followers in certain type.
-
#follower_of?(model) ⇒ Boolean
see if this model is follower of some model.
-
#set_authorization(*models) ⇒ Object
set which models user cannot follow.
-
#unfollow(*models) ⇒ Object
unfollow some model.
-
#unset_authorization(*models) ⇒ Object
unset which models user cannot follow.
Instance Method Details
#all_followees ⇒ Object
get all the followees of this model, same with classmethod followees_of
Example:
>> @jim.all_followees
=> [@ruby]
104 105 106 |
# File 'lib/mongoid_followable/follower.rb', line 104 def all_followees rebuild_instances(self.followees) end |
#common_followees?(model) ⇒ Boolean
return if there is any common followees
Example:
>> @jim.common_followees?(@tom)
=> true
184 185 186 |
# File 'lib/mongoid_followable/follower.rb', line 184 def common_followees?(model) 0 < (rebuild_instances(self.followees) & rebuild_instances(model.followees)).length end |
#common_followees_with(model) ⇒ Object
get common followees with some model
Example:
>> @jim.common_followees_with(@tom)
=> [@ruby]
194 195 196 |
# File 'lib/mongoid_followable/follower.rb', line 194 def common_followees_with(model) rebuild_instances(self.followees) & rebuild_instances(model.followees) end |
#ever_follow ⇒ Object
see user’s follow history
Example:
>> @jim.ever_follow
=> [@ruby]
170 171 172 173 174 175 176 |
# File 'lib/mongoid_followable/follower.rb', line 170 def ever_follow follow = [] self.follow_history.each do |h| follow << h.split('_')[0].constantize.find(h.split('_')[1]) end follow end |
#follow(*models) ⇒ Object
follow some model
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/mongoid_followable/follower.rb', line 120 def follow(*models) models.each do |model| unless model == self or self.follower_of?(model) or model.followee_of?(self) or self.cannot_follow.include?(model.class.name) or model.cannot_followed.include?(self.class.name) model.followers.create!(:f_type => self.class.name, :f_id => self.id.to_s) model.followed_history << self.class.name + '_' + self.id.to_s model.save self.followees.create!(:f_type => model.class.name, :f_id => model.id.to_s) self.follow_history << model.class.name + '_' + model.id.to_s self.save end end end |
#followees_by_type(type) ⇒ Object
get all the followees of this model in certain type
Example:
>> @ruby.followees_by_type("group")
=> [@ruby]
114 115 116 |
# File 'lib/mongoid_followable/follower.rb', line 114 def followees_by_type(type) rebuild_instances(self.followees.by_type(type)) end |
#followees_count ⇒ Object
get the number of followees
Example:
>> @jim.followers_count
=> 1
150 151 152 |
# File 'lib/mongoid_followable/follower.rb', line 150 def followees_count self.followees.count end |
#followees_count_by_type(type) ⇒ Object
get the number of followers in certain type
Example:
>> @ruby.followers_count_by_type("user")
=> 1
160 161 162 |
# File 'lib/mongoid_followable/follower.rb', line 160 def followees_count_by_type(type) self.followees.by_type(type).count end |
#follower_of?(model) ⇒ Boolean
see if this model is follower of some model
Example:
>> @jim.follower_of?(@ruby)
=> true
94 95 96 |
# File 'lib/mongoid_followable/follower.rb', line 94 def follower_of?(model) 0 < self.followees.by_model(model).limit(1).count * model.followers.by_model(self).limit(1).count end |
#set_authorization(*models) ⇒ Object
set which models user cannot follow
Example:
>> @jim.set_authorization('group', 'user')
=> true
72 73 74 75 76 77 |
# File 'lib/mongoid_followable/follower.rb', line 72 def (*models) models.each do |model| self.cannot_follow << model.capitalize end self.save end |
#unfollow(*models) ⇒ Object
unfollow some model
135 136 137 138 139 140 141 142 |
# File 'lib/mongoid_followable/follower.rb', line 135 def unfollow(*models) models.each do |model| unless model == self or !self.follower_of?(model) or !model.followee_of?(self) or self.cannot_follow.include?(model.class.name) or model.cannot_followed.include?(self.class.name) model.followers.by_model(self).first.destroy self.followees.by_model(model).first.destroy end end end |
#unset_authorization(*models) ⇒ Object
unset which models user cannot follow
81 82 83 84 85 86 |
# File 'lib/mongoid_followable/follower.rb', line 81 def (*models) models.each do |model| self.cannot_follow -= [model.capitalize] end self.save end |