<commit_before>class TaxonFramework < ActiveRecord::Base
  belongs_to :taxon, inverse_of: :taxon_framework
  belongs_to :source
  belongs_to :user
  belongs_to :updater, class_name: "User"
  has_many :taxon_framework_relationships, dependent: :destroy
  has_many :taxon_curators, inverse_of: :taxon_framework, dependent: :destroy
  
  before_save :check_taxon_framework_relationships
  before_save :check_taxon_curators
  after_save :check_other_taxon_framework_relationships
  after_save :handle_change_in_completeness
  
  accepts_nested_attributes_for :source
  validate :rank_level_below_taxon_rank
  validates :taxon_id, presence: true

  attr_accessor :skip_reindexing_taxa
  
  def handle_change_in_completeness
    # would use new_record? here if this was called any time other than after_save
    return true unless complete_changed? || ( rank_level_changed? && !id_changed? )
    return true if skip_reindexing_taxa
    Taxon.
      delay( priority: INTEGRITY_PRIORITY, unique_hash: { "Taxon::reindex_taxa_covered_by": self.id } ).
      reindex_taxa_covered_by( self )
    true
  end
  
  def check_taxon_framework_relationships
    return true if new_record?
    return true if rank_level_was.nil? || source_id_was.nil?
    return true unless rank_level_changed? || source_id_changed? || taxon_id_changed?
    taxon_framework_relationships.destroy_all
    true
  end
  
  def check_other_taxon_framework_relationships
    return true unless rank_level
    return true unless new_record? || rank_level_changed? || source_id_changed? || taxon_id_changed?
    
    upstream_taxon_frameworks = TaxonFramework.where( "taxon_id IN (?)", taxon.ancestor_ids ).pluck( :id )
    ancestor_string = taxon.rank == "stateofmatter" ? taxon.id.to_s : "%/#{ taxon.id }"
    tr = TaxonFrameworkRelationship.joins( :taxa ).where( "taxon_framework_id IN (?) AND (taxa.id = ? OR taxa.ancestry LIKE (?) OR taxa.ancestry LIKE (?))", upstream_taxon_frameworks, taxon.id, "#{ ancestor_string }", "#{ ancestor_string }/%" )
    return tr.destroy_all
  end
  
  def check_taxon_curators
    return true if new_record?
    return true if rank_level_was.nil?
    return true unless rank_level_changed? && rank_level.nil?
    taxon_curators.destroy_all
    true
  end
  
  def rank_level_below_taxon_rank
    return  true if rank_level.nil?
    if rank_level.to_i > taxon.rank_level.to_i
      errors.add( :rank_level, "must be below the taxon rank" )
    end
    true
  end<commit_msg>skip some taxon framework callbacks<commit_after>class TaxonFramework < ActiveRecord::Base
  belongs_to :taxon, inverse_of: :taxon_framework
  belongs_to :source
  belongs_to :user
  belongs_to :updater, class_name: "User"
  has_many :taxon_framework_relationships, dependent: :destroy
  has_many :taxon_curators, inverse_of: :taxon_framework, dependent: :destroy
  
  before_save :check_taxon_framework_relationships
  before_save :check_taxon_curators
  after_save :check_other_taxon_framework_relationships
  after_save :handle_change_in_completeness
  
  accepts_nested_attributes_for :source
  validate :rank_level_below_taxon_rank
  validates :taxon_id, presence: true

  attr_accessor :skip_reindexing_taxa
  attr_accessor :skip_check_frameworks
  attr_accessor :skip_check_curators
  
  def handle_change_in_completeness
    # would use new_record? here if this was called any time other than after_save
    return true unless complete_changed? || ( rank_level_changed? && !id_changed? )
    return true if skip_reindexing_taxa
    Taxon.
      delay( priority: INTEGRITY_PRIORITY, unique_hash: { "Taxon::reindex_taxa_covered_by": self.id } ).
      reindex_taxa_covered_by( self )
    true
  end
  
  def check_taxon_framework_relationships
    return true if skip_check_frameworks
    return true if new_record?
    return true if rank_level_was.nil? || source_id_was.nil?
    return true unless rank_level_changed? || source_id_changed? || taxon_id_changed?
    taxon_framework_relationships.destroy_all
    true
  end
  
  def check_other_taxon_framework_relationships
    return true if skip_check_frameworks
    return true unless rank_level
    return true unless new_record? || rank_level_changed? || source_id_changed? || taxon_id_changed?
    
    upstream_taxon_frameworks = TaxonFramework.where( "taxon_id IN (?)", taxon.ancestor_ids ).pluck( :id )
    ancestor_string = taxon.rank == "stateofmatter" ? taxon.id.to_s : "%/#{ taxon.id }"
    tr = TaxonFrameworkRelationship.joins( :taxa ).where( "taxon_framework_id IN (?) AND (taxa.id = ? OR taxa.ancestry LIKE (?) OR taxa.ancestry LIKE (?))", upstream_taxon_frameworks, taxon.id, "#{ ancestor_string }", "#{ ancestor_string }/%" )
    return tr.destroy_all
  end
  
  def check_taxon_curators
    return true if skip_check_curators
    return true if new_record?
    return true if rank_level_was.nil?
    return true unless rank_level_changed? && rank_level.nil?
    taxon_curators.destroy_all
    true
  end
  
  def rank_level_below_taxon_rank
    return  true if rank_level.nil?
    if rank_level.to_i > taxon.rank_level.to_i
      errors.add( :rank_level, "must be below the taxon rank" )
    end
    true
  end
{'fraction_non_alphanumeric': 0.07449078564500485, 'fraction_numerical': 0.0, 'mean_word_length': 4.562028047464941, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, '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': '790189', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1735, 'n_words': 385}