Module: Mongo::Followable

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongo_followable/followable.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]


133
134
135
# File 'lib/mongo_followable/followable.rb', line 133

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)


206
207
208
# File 'lib/mongo_followable/followable.rb', line 206

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]


216
217
218
# File 'lib/mongo_followable/followable.rb', line 216

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

#ever_followedObject

see model’s followed history

Example:

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


192
193
194
195
196
197
198
# File 'lib/mongo_followable/followable.rb', line 192

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

#followed?Boolean

return true if self is followed by some models

Example:

>> @ruby.followed?
=> true

Returns:

  • (Boolean)


123
124
125
# File 'lib/mongo_followable/followable.rb', line 123

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)


113
114
115
# File 'lib/mongo_followable/followable.rb', line 113

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]


162
163
164
# File 'lib/mongo_followable/followable.rb', line 162

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


172
173
174
# File 'lib/mongo_followable/followable.rb', line 172

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


182
183
184
# File 'lib/mongo_followable/followable.rb', line 182

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

#set_authorization(*models) ⇒ Object

set which mongoid cannot follow self

Example:

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


92
93
94
95
96
97
98
# File 'lib/mongo_followable/followable.rb', line 92

def set_authorization(*models)
  models.each do |model|

    self.cannot_followed << model.safe_capitalize
  end
  self.save
end

#unfollowed(*models, &block) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mongo_followable/followable.rb', line 137

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) or self.cannot_followed.include?(model.class.name) or model.cannot_follow.include?(self.class.name)
      model.followees.by_model(self).first.destroy
      self.followers.by_model(model).first.destroy
    end
  end
end

#unfollowed_allObject

unfollow all



152
153
154
# File 'lib/mongo_followable/followable.rb', line 152

def unfollowed_all
  unfollowed(*self.all_followers)
end

#unset_authorization(*models) ⇒ Object



100
101
102
103
104
105
# File 'lib/mongo_followable/followable.rb', line 100

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