Module: Mongoid::Followit::Follower

Defined in:
lib/mongoid_followit/follower.rb

Overview

Public: Module that add follower capabilities to a Mongoid model.

Examples

class MyModel
  include Mongoid::Document
  include Mongoid::Followit::Follower

  before_follow   :do_something_before
  before_unfollow :do_otherthing_before

  after_follow   :do_something_after
  after_unfollow :do_otherthing_after

end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_callbacks(base) ⇒ Object

Internal: Class method that generate callbacks

for the #follow and #unfollow methods.

base - Mongoid model class which will be patched.

PS: This method is used inside the .included method inside this Module.

Returns nothing.



119
120
121
122
123
124
125
126
127
# File 'lib/mongoid_followit/follower.rb', line 119

def self.generate_callbacks(base)
  %w(before after).each do |callback|
    %w(follow unfollow).each do |action|
      base.define_singleton_method("#{callback}_#{action}") do |*ar, &blo|
        set_callback(action.to_sym, callback.to_sym, *ar, &blo)
      end
    end
  end
end

.included(base) ⇒ Object



21
22
23
24
# File 'lib/mongoid_followit/follower.rb', line 21

def self.included(base)
  patch_class(base)
  generate_callbacks(base)
end

.patch_class(base) ⇒ Object

Internal: Class method that configures the Mongoid model which has

Mongoid::Followit::Follower model included.

base - Mongoid model class which will be patched.

PS: This method is used inside the .included method inside this Module.

Returns nothing.



101
102
103
104
105
106
107
108
# File 'lib/mongoid_followit/follower.rb', line 101

def self.patch_class(base)
  base.class_eval do
    include Mongoid::Followit::Queryable
    include ActiveSupport::Callbacks
    define_callbacks :follow, :unfollow
    before_destroy :destroy_follow_data
  end
end

Instance Method Details

#follow(*followees) ⇒ Object

Public: Creates Follow entries, for the Followee models, representing

the models that are being followed.

*followees - A collection of Followee models to be followed.

Examples

# => person = Person.create!(name: 'Skywalker')
# => profile = Profile.create!(name: 'Jedi')
# => person.follow(profile)

Returns nothing. Raises Runtime error if at least one of the models passed does not

include the Mongoid::Followit::Followee module.


41
42
43
# File 'lib/mongoid_followit/follower.rb', line 41

def follow(*followees)
  perform_followable_action(:follow, followees)
end

#followees(criteria: false) ⇒ Object

Public: Peform a query to return all Mongoid model

that model is following.

criteria(optional) - if true the return will be the type of

Mongoid::Criteria

Examples

class Person
  include Mongoid::Document
  include Mongoid::Follower

  field :name, type: String
  validates_uniqueness_of :name
end

# => Person.find_by(name: 'Skywalker').followees

Returns An Array of followees if criteria argument is false. Returns A Mongoid::Criteria of followees if criteria argument is true

and followees are of only one type

Raises HasTwoFolloweeTypesError if criteria argument is true

and model has two or more types of followees


86
87
88
# File 'lib/mongoid_followit/follower.rb', line 86

def followees(criteria: false)
  follow_collection_for_a(:followee, criteria: criteria)
end

#unfollow(*followees) ⇒ Object

Public: Destroys the Follow entries, for the Followee models,

making the Followee models to be unfollowed.

*followees - A collection of Followee models to be unfollowed.

Examples

# => person.unfollow(jedi, padawan)

Returns nothing. Raises Runtime error if at least one of the models passed does not

include the Mongoid::Followit::Followee module.


58
59
60
# File 'lib/mongoid_followit/follower.rb', line 58

def unfollow(*followees)
  perform_followable_action(:unfollow, followees)
end