<commit_before>require 'squeel'

module Amistad
  module ActiveRecordFriendModel
    extend ActiveSupport::Concern

    included do
      #####################################################################################
      # friendships
      #####################################################################################
      has_many  :friendships,
        :class_name => "Amistad::Friendships::#{Amistad.friendship_model}",
        :foreign_key => "friendable_id"

      has_many  :pending_invited, -> { where(:'friendships.pending' => true, :'friendships.blocker_id' => nil)}, :through => :friendships, :source => :friend
      has_many  :invited, -> { where(:'friendships.pending' => false, :'friendships.blocker_id' => nil)}, :through => :friendships, :source => :friend
      #####################################################################################
      # inverse friendships
      #####################################################################################
      has_many  :inverse_friendships,
        :class_name => "Amistad::Friendships::#{Amistad.friendship_model}",
        :foreign_key => "friend_id"

      has_many  :pending_invited_by, ->  { where(:'friendships.pending' => true, :'friendships.blocker_id' => nil)}, :through => :inverse_friendships, :source => :friendable
      has_many  :invited_by, -> { where(:'friendships.pending' => false, :'friendships.blocker_id' => nil)}, :through => :inverse_friendships, :source => :friendable
      #####################################################################################
      # blocked friendships
      #####################################################################################
      has_many  :blocked_friendships,
        :class_name => "Amistad::Friendships::#{Amistad.friendship_model}",
        :foreign_key => "blocker_id"

      has_many  :blockades, -> {where("friend_id <> blocker_id")}, :through => :blocked_friendships, :source => :friend
      has_many  :blockades_by, -> {where("friendable_id <> blocker_id")} , :through => :blocked_friendships, :source => :friendable
    end

    # suggest a user to become a friend. If the operation succeeds, the method returns true, else false
    def invite(user)
      return false if user == self || find_any_friendship_with(user)
      Amistad.friendship_class.new{ |f| f.friendable = self ; f.friend = user }.save
    end

    # approve a friendship invitation. If the operation succeeds, the method returns true, else false
    def approve(user)
      friendship = find_any_friendship_with(user)
      return false if friendship.nil? || invited?(user)
      friendship.update_attribute(:pending, false)
    end

    # deletes a friendship
    def remove_friendship(user)<commit_msg>Fixes<commit_after>require 'squeel'

module Amistad
  module ActiveRecordFriendModel
    extend ActiveSupport::Concern

    included do
      #####################################################################################
      # friendships
      #####################################################################################
      has_many  :friendships,
        :class_name => "Amistad::Friendships::#{Amistad.friendship_model}",
        :foreign_key => "friendable_id"


      has_many  :pending_invited, -> { where(:'friendships.pending' => true, :'friendships.blocker_id' => nil)}, :through => :friendships, :source => :friend
      has_many  :invited, -> { where(:'friendships.pending' => false, :'friendships.blocker_id' => nil)}, :through => :friendships, :source => :friend

      #####################################################################################
      # inverse friendships
      #####################################################################################
      has_many  :inverse_friendships,
        :class_name => "Amistad::Friendships::#{Amistad.friendship_model}",
        :foreign_key => "friend_id"

      has_many  :pending_invited_by, ->  { where(:'friendships.pending' => true, :'friendships.blocker_id' => nil)}, :through => :inverse_friendships, :source => :friendable
      has_many  :invited_by, -> { where(:'friendships.pending' => false, :'friendships.blocker_id' => nil)}, :through => :inverse_friendships, :source => :friendable

      #####################################################################################
      # blocked friendships
      #####################################################################################
      has_many  :blocked_friendships,
        :class_name => "Amistad::Friendships::#{Amistad.friendship_model}",
        :foreign_key => "blocker_id"

      has_many  :blockades, -> {where("friend_id <> blocker_id")}, :through => :blocked_friendships, :source => :friend
      has_many  :blockades_by, -> {where("friendable_id <> blocker_id")} , :through => :blocked_friendships, :source => :friendable

    end

    # suggest a user to become a friend. If the operation succeeds, the method returns true, else false
    def invite(user)
      return false if user == self || find_any_friendship_with(user)
      Amistad.friendship_class.new{ |f| f.friendable = self ; f.friend = user }.save
    end

    # approve a friendship invitation. If the operation succeeds, the method returns true, else false
    def approve(user)
      friendship = find_any_friendship_with(user)
      return false if friendship.nil? || invited?(user)
      friendship.update_attribute(:pending, false)
    end

    # deletes a friendship
    def remove_friendship(user)
{'fraction_non_alphanumeric': 0.2888647866955893, 'fraction_numerical': 0.0, 'mean_word_length': 4.651685393258427, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 71, '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': '5924681', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1412, 'n_words': 307}