Module: Mongoid::Follower

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid_followable/follower.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#all_followeesObject

get all the followees of this model, same with classmethod followees_of

Example:

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


72
73
74
# File 'lib/mongoid_followable/follower.rb', line 72

def all_followees
  rebuild_instances(self.followees)
end

#ever_followObject

see user’s follow history

Example:

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


138
139
140
141
142
143
144
# File 'lib/mongoid_followable/follower.rb', line 138

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



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mongoid_followable/follower.rb', line 88

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]


82
83
84
# File 'lib/mongoid_followable/follower.rb', line 82

def followees_by_type(type)
  rebuild_instances(self.followees.by_type(type))
end

#followees_countObject

get the number of followees

Example:

>> @jim.followers_count
=> 1


118
119
120
# File 'lib/mongoid_followable/follower.rb', line 118

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


128
129
130
# File 'lib/mongoid_followable/follower.rb', line 128

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

Returns:

  • (Boolean)


62
63
64
# File 'lib/mongoid_followable/follower.rb', line 62

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


40
41
42
43
44
45
# File 'lib/mongoid_followable/follower.rb', line 40

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

#unfollow(*models) ⇒ Object

unfollow some model



103
104
105
106
107
108
109
110
# File 'lib/mongoid_followable/follower.rb', line 103

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



49
50
51
52
53
54
# File 'lib/mongoid_followable/follower.rb', line 49

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