Module: Mongo::Followable::Followed

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongo_followable/followed.rb

Defined Under Namespace

Modules: ClassMethods

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]


109
110
111
# File 'lib/mongo_followable/followed.rb', line 109

def all_followers
  rebuild_instances(self.followers)
end

#common_followers?(model) ⇒ Boolean

return if there is any common followers

Example:

>> @ruby.common_followees?(@python)
=> true

Returns:

  • (Boolean)


168
169
170
# File 'lib/mongo_followable/followed.rb', line 168

def common_followers?(model)
  0 < (rebuild_instances(self.followers) & rebuild_instances(model.followers)).length
end

#common_followers_with(model) ⇒ Object

get common followers with some model

Example:

>> @ruby.common_followers_with(@python)
=> [@jim]


178
179
180
# File 'lib/mongo_followable/followed.rb', line 178

def common_followers_with(model)
  rebuild_instances(self.followers) & rebuild_instances(model.followers)
end

#followed?Boolean

return true if self is followed by some models

Example:

>> @ruby.followed?
=> true

Returns:

  • (Boolean)


99
100
101
# File 'lib/mongo_followable/followed.rb', line 99

def followed?
  0 < self.followers.length
end

#followee_of?(model) ⇒ Boolean

see if this model is followee of some model

Example:

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

Returns:

  • (Boolean)


89
90
91
# File 'lib/mongo_followable/followed.rb', line 89

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]


138
139
140
# File 'lib/mongo_followable/followed.rb', line 138

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


148
149
150
# File 'lib/mongo_followable/followed.rb', line 148

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


158
159
160
# File 'lib/mongo_followable/followed.rb', line 158

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

#unfollowed(*models, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/mongo_followable/followed.rb', line 113

def unfollowed(*models, &block)
  if block_given?
    models.delete_if { |model| !yield(model) }
  end

  models.each do |model|
    unless model == self or !self.followee_of?(model) or !model.follower_of?(self)
      model.followees.by_model(self).first.destroy
      self.followers.by_model(model).first.destroy
    end
  end
end

#unfollowed_allObject

unfollow all



128
129
130
# File 'lib/mongo_followable/followed.rb', line 128

def unfollowed_all
  unfollowed(*self.all_followers)
end