module Partisan::Follow

Constants

FOLLOWABLE_BEING_FOLLOWED_ACCESSORS
FOLLOWABLE_BEING_UNFOLLOWED_ACCESSORS
FOLLOWER_FOLLOW_ACCESSORS

Constants

FOLLOWER_UNFOLLOW_ACCESSORS

Protected Instance Methods

around_create_followable(&blk) click to toggle source
# File lib/partisan/follow.rb, line 41
def around_create_followable(&blk)
  execute_callback :followable, :being_followed, &blk
end
around_create_follower(&blk) click to toggle source
# File lib/partisan/follow.rb, line 37
def around_create_follower(&blk)
  execute_callback :follower, :follow, &blk
end
around_destroy_followable(&blk) click to toggle source
# File lib/partisan/follow.rb, line 49
def around_destroy_followable(&blk)
  execute_callback :followable, :being_unfollowed, &blk
end
around_destroy_follower(&blk) click to toggle source
# File lib/partisan/follow.rb, line 45
def around_destroy_follower(&blk)
  execute_callback :follower, :unfollow, &blk
end
update_follow_counter() click to toggle source
# File lib/partisan/follow.rb, line 32
def update_follow_counter
  follower.update_followings_counter
  followable.update_followers_counter
end

Private Instance Methods

execute_callback(association, callback, &blk) click to toggle source
# File lib/partisan/follow.rb, line 55
def execute_callback(association, callback, &blk)
  # Fetch our associated objects
  object = send(association)
  reverse_object = association == :follower ? send(:followable) : send(:follower)

  # Associate each given accessor to the reverse object
  accessors = self.class.accessors_for_follow_callback(association, callback)
  accessors.map { |accessor| object.send "#{accessor}=", reverse_object }

  # Run the callbacks
  object.run_callbacks(callback, &blk)

  # Reset each accessor value
  accessors.map { |accessor| object.send "#{accessor}=", nil }

  true
end