<commit_before>module Mongoid
  module Follower
    extend ActiveSupport::Concern

    included do |base|
      after_destroy :reset_followees

      base.field    :ffeec, :type => Integer, :default => 0
      base.has_many :followees, :class_name => 'Follow', :as => :followee, :dependent => :destroy
    end

    # follow a model
    #
    # Example:
    # => @bonnie.follow(@clyde)
    def follow(model)
      if self.id != model.id && !self.follows?(model)

        model.before_followed_by(self) if model.respond_to?('before_followed_by')
        model.followers.create!(:ff_type => self.class.name, :ff_id => self.id)
        model.inc(:fferc, 1)
        model.after_followed_by(self) if model.respond_to?('after_followed_by')

        self.before_follow(model) if self.respond_to?('before_follow')
        self.followees.create!(:ff_type => model.class.name, :ff_id => model.id)
        self.inc(:ffeec, 1)
        self.after_follow(model) if self.respond_to?('after_follow')

      else
        return false
      end
    end

    # unfollow a model
    #
    # Example:
    # => @bonnie.unfollow(@clyde)
    def unfollow(model)
      if self.id != model.id && self.follows?(model)

        model.before_unfollowed_by(self) if model.respond_to?('before_unfollowed_by')
        model.followers.where(:ff_type => self.class.name, :ff_id => self.id).destroy
        model.inc(:fferc, -1)
        model.after_unfollowed_by(self) if model.respond_to?('after_unfollowed_by')

        self.before_unfollow(model) if self.respond_to?('before_unfollow')
        self.followees.where(:ff_type => model.class.name, :ff_id => model.id).destroy
        self.inc(:ffeec, -1)
        self.after_unfollow(model) if self.respond_to?('after_unfollow')

      else
        return false
      end
    end

    # know if self is already following model
    #
    # Example:
    # => @bonnie.follows?(@clyde)
    # => true
    def follows?(model)
      0 < self.followees.where(ff_id: model.id).limit(1).count
    end

    # get followees count
    # Note: this is a cache counter
    #
    # Example:
    # => @bonnie.followees_count
    # => 1
    def followees_count
      self.ffeec
    end

    # get followees count by model
    #
    # Example:<commit_msg>Update follower.rb<commit_after>module Mongoid
  module Follower
    extend ActiveSupport::Concern

    included do |base|
      after_destroy :reset_followees

      base.field    :ffeec, :type => Integer, :default => 0
      base.has_many :followees, :class_name => 'Follow', :as => :followee, :dependent => :destroy
    end

    # follow a model
    #
    # Example:
    # => @bonnie.follow(@clyde)
    def follow(model)
      if self.id != model.id && !self.follows?(model)

        model.before_followed_by(self) if model.respond_to?('before_followed_by')
        model.followers.create!(:ff_type => self.class.name, :ff_id => self.id)
        model.inc(:fferc, 1)
        model.after_followed_by(self) if model.respond_to?('after_followed_by')

        self.before_follow(model) if self.respond_to?('before_follow')
        self.followees.create!(:ff_type => model.class.name, :ff_id => model.id)
        self.inc(:ffeec, 1)
        self.after_follow(model) if self.respond_to?('after_follow')

      else
        return false
      end
    end

    # unfollow a model
    #
    # Example:
    # => @bonnie.unfollow(@clyde)
    def unfollow(model)
      if self.id != model.id && self.follows?(model)

        model.before_unfollowed_by(self) if model.respond_to?('before_unfollowed_by')
        model.followers.where(:ff_type => self.class.name, :ff_id => self.id).destroy
        model.inc(:fferc, -1)
        model.after_unfollowed_by(self) if model.respond_to?('after_unfollowed_by')

        self.before_unfollow(model) if self.respond_to?('before_unfollow')
        self.followees.where(:ff_type => model.class.name, :ff_id => model.id).destroy
        self.inc(:ffeec, -1)
        self.after_unfollow(model) if self.respond_to?('after_unfollow')

      else
        return false
      end
    end

    # know if self is already following model
    #
    # Example:
    # => @bonnie.follows?(@clyde)
    # => true
    def follows?(model)
      self.followers.where(follower_type: model.class.name, :follower_id => model.id, relation: relation).exists?
    end

    # get followees count
    # Note: this is a cache counter
    #
    # Example:
    # => @bonnie.followees_count
    # => 1
    def followees_count
      self.ffeec
    end

    # get followees count by model
    #
    # Example:
{'fraction_non_alphanumeric': 0.12779973649538867, 'fraction_numerical': 0.0030742204655248135, 'mean_word_length': 2.7062652563059397, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6694382', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1517, 'n_words': 291}