Sha256: 12a7458d47948ff6b17b056e46d6f84dd465d037197d5ab114a43e7f6c312b69

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

module Partisan
  module Followable
    extend ActiveSupport::Concern
    include Partisan::FollowHelper

    included do
      has_many :followings, as: :followable, class_name: 'Partisan::Follow', dependent: :destroy
    end

    # Return true or false if the resource is following another
    #
    # @example
    #
    #   @team.followed_by?(@user)
    #
    # @return (Boolean)
    def followed_by?(resource)
      resource.following?(self)
    end

    # 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)
    def followers_by_type(follower_type)
      opts = {
        'follows.followable_id' => self.id,
        'follows.followable_type' => parent_class_name(self)
      }

      follower_type.constantize.joins(:follows).where(opts)
    end

    # Get ids of resources following self
    # Use #pluck for an optimized sql query
    #
    # @example
    #
    #   @team.following_ids_by_type('User')
    #
    # @return (Array)
    def follower_fields_by_type(follower_type, follower_field)
      followers_by_type(follower_type).pluck("#{follower_type.tableize}.#{follower_field}")
    end

    # Update cache counter
    # Called in after_create and after_destroy
    def update_follow_counter
      self.update_attribute('followers_count', self.followings.count) if self.respond_to?(:followers_count)
    end

    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

    def respond_to?(m, include_private = false)
      super || m.to_s[/(.+)_follower_(.+)s$/] || m.to_s[/(.+)_follower/]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
partisan-0.1.1 lib/partisan/followable.rb
partisan-0.1 lib/partisan/followable.rb
partisan-0.0.1 lib/partisan/followable.rb