module ActsAsFollower::Followable::InstanceMethods

Public Instance Methods

block(follower) click to toggle source
# File lib/acts_as_follower/followable.rb, line 89
def block(follower)
  get_follow_for(follower) ? block_existing_follow(follower) : block_future_follow(follower)
end
blocked_followers_count() click to toggle source
# File lib/acts_as_follower/followable.rb, line 62
def blocked_followers_count
  self.followings.blocked.count
end
blocks(options={}) click to toggle source
# File lib/acts_as_follower/followable.rb, line 77
def blocks(options={})
  blocked_followers_scope = followers_scoped.blocked
  blocked_followers_scope = apply_options_to_scope(blocked_followers_scope, options)
  blocked_followers_scope.to_a.collect{|f| f.follower}
end
followed_by?(follower) click to toggle source

Returns true if the current instance is followed by the passed record Returns false if the current instance is blocked by the passed record or no follow is found

# File lib/acts_as_follower/followable.rb, line 85
def followed_by?(follower)
  self.followings.unblocked.for_follower(follower).first.present?
end
followers(options={}) click to toggle source
# File lib/acts_as_follower/followable.rb, line 71
def followers(options={})
  followers_scope = followers_scoped.unblocked
  followers_scope = apply_options_to_scope(followers_scope, options)
  followers_scope.to_a.collect{|f| f.follower}
end
followers_by_type(follower_type, options={}) click to toggle source

Returns the followers by a given type

# File lib/acts_as_follower/followable.rb, line 24
def followers_by_type(follower_type, options={})
  follows = follower_type.constantize.
    joins(:follows).
    where('follows.blocked'         => false,
          'follows.followable_id'   => self.id,
          'follows.followable_type' => parent_class_name(self),
          'follows.follower_type'   => follower_type)
  if options.has_key?(:limit)
    follows = follows.limit(options[:limit])
  end
  if options.has_key?(:includes)
    follows = follows.includes(options[:includes])
  end
  follows
end
followers_by_type_count(follower_type) click to toggle source
# File lib/acts_as_follower/followable.rb, line 40
def followers_by_type_count(follower_type)
  self.followings.unblocked.for_follower_type(follower_type).count
end
followers_count() click to toggle source

Returns the number of followers a record has.

# File lib/acts_as_follower/followable.rb, line 19
def followers_count
  self.followings.unblocked.count
end
followers_scoped() click to toggle source

Returns the followings records scoped

# File lib/acts_as_follower/followable.rb, line 67
def followers_scoped
  self.followings.includes(:follower)
end
get_follow_for(follower) click to toggle source
# File lib/acts_as_follower/followable.rb, line 97
def get_follow_for(follower)
  self.followings.for_follower(follower).first
end
method_missing(m, *args) click to toggle source

Allows magic names on followers_by_type e.g. user_followers == followers_by_type(‘User’) Allows magic names on followers_by_type_count e.g. count_user_followers == followers_by_type_count(‘User’)

Calls superclass method
# File lib/acts_as_follower/followable.rb, line 48
def method_missing(m, *args)
  if m.to_s[/count_(.+)_followers/]
    followers_by_type_count($1.singularize.classify)
  elsif m.to_s[/(.+)_followers/]
    followers_by_type($1.singularize.classify)
  else
    super
  end
end
respond_to?(m, include_private = false) click to toggle source
Calls superclass method
# File lib/acts_as_follower/followable.rb, line 58
def respond_to?(m, include_private = false)
  super || m.to_s[/count_(.+)_followers/] || m.to_s[/(.+)_followers/]
end
unblock(follower) click to toggle source
# File lib/acts_as_follower/followable.rb, line 93
def unblock(follower)
  get_follow_for(follower).try(:delete)
end

Private Instance Methods

block_existing_follow(follower) click to toggle source
# File lib/acts_as_follower/followable.rb, line 107
def block_existing_follow(follower)
  get_follow_for(follower).block!
end
block_future_follow(follower) click to toggle source
# File lib/acts_as_follower/followable.rb, line 103
def block_future_follow(follower)
  Follow.create(followable: self, follower: follower, blocked: true)
end