module Partisan::Followable

Public Instance Methods

followed_by?(resource) click to toggle source

Return true or false if the resource is following another

@example

@team.followed_by?(@user)

@return (Boolean)

# File lib/partisan/followable.rb, line 20
def followed_by?(resource)
  resource.following?(self)
end
follower_fields_by_type(follower_type, follower_field) click to toggle source

Get ids of resources following self Use pluck for an optimized sql query

@example

@team.following_ids_by_type('User')

@return (Array)

# File lib/partisan/followable.rb, line 49
def follower_fields_by_type(follower_type, follower_field)
  followers_by_type(follower_type).pluck("#{follower_type.tableize}.#{follower_field}")
end
followers_by_type(follower_type) click to toggle source

Get resource records for a specific type, used by method_missing It conveniently returns an ActiveRecord::Relation for easy chaining of useful ActiveRecord methods

@example

@team.followers_by_type('User')

@return (ActiveRecord::Relation)

# File lib/partisan/followable.rb, line 32
def followers_by_type(follower_type)
  opts = {
    'follows.followable_id' => self.id,
    'follows.followable_type' => Partisan::Helper.parent_class_name(self)
  }

  follower_type.constantize.joins(:follows).where(opts)
end
method_missing(m, *args) click to toggle source
Calls superclass method
# File lib/partisan/followable.rb, line 59
def method_missing(m, *args)
  if m.to_s[/(.+)_follower_(.+)s$/]
    follower_fields_by_type($1.singularize.classify, $2)
  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/partisan/followable.rb, line 69
def respond_to?(m, include_private = false)
  super || m.to_s[/(.+)_follower_(.+)s$/] || m.to_s[/(.+)_follower/]
end
update_followers_counter() click to toggle source

Update cache counter Called in after_create and after_destroy

# File lib/partisan/followable.rb, line 55
def update_followers_counter
  self.update_attribute('followers_count', self.followings.count) if self.respond_to?(:followers_count)
end