Sha256: 62a98b4f87430c6cb9e0677b0f7060acf37fcf5d5c85c520e80a5eab520cb1ae

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

module Mongoid
  module Followee
    extend ActiveSupport::Concern

    included do |base|
      base.field    :fferc, :type => Integer, :default => 0
      base.has_many :followers, :class_name => 'Follow', :as => :follower, :dependent => :destroy
    end

    # know if self is followed by model
    #
    # Example:
    # >> @clyde.follower?(@bonnie)
    # => true
    def follower?(model)
      0 < self.followers.find(:all, conditions: {ff_id: model.id}).limit(1).count
    end

    # get followees count
    #
    # Example:
    # >> @bonnie.followees_count
    # => 1
    def followers_count
      self.fferc
    end

    # view all selfs followers
    #
    # Example:
    # >> @clyde.all_followers
    # => [@bonnie, @alec]
    def all_followers
      get_followers_of(self)
    end

    # view all common followers of self against model
    #
    # Example:
    # >> @clyde.common_followers_with(@gang)
    # => [@bonnie, @alec]
    def common_followers_with(model)
      model_followers = get_followers_of(model)
      self_followers = get_followers_of(self)

      self_followers & model_followers
    end

    private
    def get_followers_of(me)
      me.followers.collect do |f|
        f.ff_type.constantize.find(f.ff_id)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid_follow-0.2.1 lib/mongoid_follow/followee.rb
mongoid_follow-0.2.0 lib/mongoid_follow/followee.rb
mongoid_follow-0.1.0 lib/mongoid_follow/followee.rb