<commit_before>class Relationship < ActiveRecord::Base
  include CheckElasticSearch

  attr_accessor :current_id, :is_being_copied

  belongs_to :source, class_name: 'ProjectMedia'
  belongs_to :target, class_name: 'ProjectMedia'
  belongs_to :user

  serialize :relationship_type

  before_validation :set_user
  validate :relationship_type_is_valid
  validate :child_or_parent_does_not_have_another_parent, on: :create, if: proc { |x| !x.is_being_copied? }

  after_create :increment_counters, :index_source
  after_update :propagate_inversion, :reset_counters
  after_destroy :decrement_counters, :unindex_source

  has_paper_trail on: [:create, :update, :destroy], if: proc { |x| User.current.present? && !x.is_being_copied? }

  notifies_pusher on: [:save, :destroy],
                  event: 'relationship_change',
                  targets: proc { |r| r.source.nil? ? [] : [r.source.media] }, bulk_targets: proc { |r| [r.source.media] },
                  if: proc { |r| !r.skip_notifications },
                  data: proc { |r| Relationship.where(id: r.id).last.nil? ? { source_id: r.source_id }.to_json : r.to_json }

  def siblings(inclusive = false, limit = 50)
    query = Relationship
    .includes(:target)
    .where(source_id: self.source_id)
    .where('relationship_type = ?', self.relationship_type.to_yaml)
    .order('id DESC')
    .limit(limit)
    query = inclusive ? query : query.where.not(target_id: self.target_id)
    pms = []
    query.collect do |r|
      t = r.target
      next if t.inactive
      pm = t
      pm.relationship = r
      pms << pm
    end
    pms
  end

  def graphql_deleted_id
    self.target&.graphql_id.to_s
  end

  def current_project_media
    ProjectMedia.where(id: self.current_id.to_i).last<commit_msg>#7584 Notify both ends and send both ids<commit_after>class Relationship < ActiveRecord::Base
  include CheckElasticSearch

  attr_accessor :current_id, :is_being_copied

  belongs_to :source, class_name: 'ProjectMedia'
  belongs_to :target, class_name: 'ProjectMedia'
  belongs_to :user

  serialize :relationship_type

  before_validation :set_user
  validate :relationship_type_is_valid
  validate :child_or_parent_does_not_have_another_parent, on: :create, if: proc { |x| !x.is_being_copied? }

  after_create :increment_counters, :index_source
  after_update :propagate_inversion, :reset_counters
  after_destroy :decrement_counters, :unindex_source

  has_paper_trail on: [:create, :update, :destroy], if: proc { |x| User.current.present? && !x.is_being_copied? }

  notifies_pusher on: [:save, :destroy],
                  event: 'relationship_change',
                  targets: proc { |r| r.source.nil? ? [] : [r.source.media, r.target.media] }, bulk_targets: proc { |r| [r.source.media, r.target.media] },
                  if: proc { |r| !r.skip_notifications },
                  data: proc { |r| Relationship.where(id: r.id).last.nil? ? { source_id: r.source_id, target_id: r.target_id }.to_json : r.to_json }

  def siblings(inclusive = false, limit = 50)
    query = Relationship
    .includes(:target)
    .where(source_id: self.source_id)
    .where('relationship_type = ?', self.relationship_type.to_yaml)
    .order('id DESC')
    .limit(limit)
    query = inclusive ? query : query.where.not(target_id: self.target_id)
    pms = []
    query.collect do |r|
      t = r.target
      next if t.inactive
      pm = t
      pm.relationship = r
      pms << pm
    end
    pms
  end

  def graphql_deleted_id
    self.target&.graphql_id.to_s
  end

  def current_project_media
    ProjectMedia.where(id: self.current_id.to_i).last
{'fraction_non_alphanumeric': 0.11422234543942335, 'fraction_numerical': 0.0022179096201829776, 'mean_word_length': 3.416156670746634, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '1044158', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1222, 'n_words': 260}