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.9"

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]


102
103
104
# File 'lib/mongoid_followable/followable.rb', line 102

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)


156
157
158
# File 'lib/mongoid_followable/followable.rb', line 156

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]


166
167
168
# File 'lib/mongoid_followable/followable.rb', line 166

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]


142
143
144
145
146
147
148
# File 'lib/mongoid_followable/followable.rb', line 142

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

Returns:

  • (Boolean)


92
93
94
# File 'lib/mongoid_followable/followable.rb', line 92

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]


112
113
114
# File 'lib/mongoid_followable/followable.rb', line 112

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


122
123
124
# File 'lib/mongoid_followable/followable.rb', line 122

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


132
133
134
# File 'lib/mongoid_followable/followable.rb', line 132

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


72
73
74
75
76
77
# File 'lib/mongoid_followable/followable.rb', line 72

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

#unset_authorization(*models) ⇒ Object



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

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