Module: Mongo::Followable::Follower
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongo_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.
-
#follow(*models, &block) ⇒ 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.
-
#following? ⇒ Boolean
return true if self is following some models.
-
#unfollow(*models, &block) ⇒ Object
unfollow some model.
-
#unfollow_all ⇒ Object
unfollow all.
Instance Method Details
#all_followees ⇒ Object
get all the followees of this model, same with classmethod followees_of
Example:
>> @jim.all_followees
=> [@ruby]
109 110 111 |
# File 'lib/mongo_followable/follower.rb', line 109 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
191 192 193 |
# File 'lib/mongo_followable/follower.rb', line 191 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]
201 202 203 |
# File 'lib/mongo_followable/follower.rb', line 201 def common_followees_with(model) rebuild_instances(self.followees) & rebuild_instances(model.followees) end |
#follow(*models, &block) ⇒ Object
follow some model
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/mongo_followable/follower.rb', line 125 def follow(*models, &block) if block_given? models.delete_if { |model| !yield(model) } end models.each do |model| unless model == self or self.follower_of?(model) or model.followee_of?(self) model.followers.create!(:f_type => self.class.name, :f_id => self.id.to_s) self.followees.create!(:f_type => model.class.name, :f_id => model.id.to_s) model.followed_history << self.class.name + '_' + self.id.to_s if model.respond_to? :followed_history self.follow_history << model.class.name + '_' + model.id.to_s if self.respond_to? :follow_history model.save 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]
119 120 121 |
# File 'lib/mongo_followable/follower.rb', line 119 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
171 172 173 |
# File 'lib/mongo_followable/follower.rb', line 171 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
181 182 183 |
# File 'lib/mongo_followable/follower.rb', line 181 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
89 90 91 |
# File 'lib/mongo_followable/follower.rb', line 89 def follower_of?(model) 0 < self.followees.by_model(model).limit(1).count * model.followers.by_model(self).limit(1).count end |
#following? ⇒ Boolean
return true if self is following some models
Example:
>> @jim.following?
=> true
99 100 101 |
# File 'lib/mongo_followable/follower.rb', line 99 def following? 0 < self.followees.length end |
#unfollow(*models, &block) ⇒ Object
unfollow some model
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/mongo_followable/follower.rb', line 146 def unfollow(*models, &block) if block_given? models.delete_if { |model| !yield(model) } end models.each do |model| unless model == self or !self.follower_of?(model) or !model.followee_of?(self) model.followers.by_model(self).first.destroy self.followees.by_model(model).first.destroy end end end |
#unfollow_all ⇒ Object
unfollow all
161 162 163 |
# File 'lib/mongo_followable/follower.rb', line 161 def unfollow_all unfollow(*self.all_followees) end |