module Partisan::Follower
Public Instance Methods
fetch_follows(resource)
click to toggle source
Get all follows record related to a resource
@example
@user.fetch_follows(@team)
@return [Follow, …]
# File lib/partisan/follower.rb, line 64 def fetch_follows(resource) follows.where followable_id: resource.id, followable_type: Partisan::Helper.parent_class_name(resource) end
follow(resource)
click to toggle source
Add follow record if it doesn’t exist
@example
@user.follow(@team)
@return (Follow
)
# File lib/partisan/follower.rb, line 20 def follow(resource) return if self == resource follow = fetch_follows(resource).first_or_initialize follow.save! end
Also aliased as: start_following
following_by_type(followable_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
@user.following_by_type('Team')
@return (ActiveRecord::Relation)
# File lib/partisan/follower.rb, line 76 def following_by_type(followable_type) opts = { 'follows.follower_id' => self.id, 'follows.follower_type' => Partisan::Helper.parent_class_name(self) } followable_type.constantize.joins(:followings).where(opts) end
following_fields_by_type(followable_type, followable_field)
click to toggle source
Get ids of resources following self Use pluck for an optimized sql query
@example
@user.following_ids_by_type('Team')
@return (Array)
# File lib/partisan/follower.rb, line 93 def following_fields_by_type(followable_type, followable_field) following_by_type(followable_type).pluck("#{followable_type.tableize}.#{followable_field}") end
follows?(resource)
click to toggle source
Return true or false if the resource is following another
@example
@user.following?(@team)
@return (Boolean)
# File lib/partisan/follower.rb, line 50 def follows?(resource) return false if self == resource !!fetch_follows(resource).exists? end
Also aliased as: following?
method_missing(m, *args)
click to toggle source
Calls superclass method
# File lib/partisan/follower.rb, line 103 def method_missing(m, *args) if m.to_s[/following_(.+)_(.+)s$/] following_fields_by_type($1.singularize.classify, $2) elsif m.to_s[/following_(.+)/] following_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/follower.rb, line 113 def respond_to?(m, include_private = false) super || m.to_s[/following_(.+)_(.+)s$/] || m.to_s[/following_(.+)/] end
unfollow(resource)
click to toggle source
Delete follow record if it exists
@example
@user.unfollow(@team)
@return (Follow
) || nil
# File lib/partisan/follower.rb, line 35 def unfollow(resource) return if self == resource record = fetch_follows(resource).first record.try(:destroy) end
Also aliased as: stop_following
update_followings_counter()
click to toggle source
Update cache counter Called in after_create and after_destroy
# File lib/partisan/follower.rb, line 99 def update_followings_counter self.update_attribute('followings_count', self.follows.count) if self.respond_to?(:followings_count) end