<commit_before>class Post < ApplicationRecord
  belongs_to :user
  belongs_to :post_type
  belongs_to :parent, class_name: 'Post', required: false, counter_cache: :answer_count
  belongs_to :closed_by, class_name: 'User', required: false
  belongs_to :deleted_by, class_name: 'User', required: false
  has_and_belongs_to_many :tags, dependent: :destroy
  has_many :votes, dependent: :destroy
  has_many :comments, dependent: :destroy
  has_many :post_histories, dependent: :destroy
  has_many :flags, dependent: :destroy
  has_many :children, class_name: 'Post', foreign_key: 'parent_id', dependent: :destroy

  serialize :tags_cache, Array

  validates :body, presence: true, length: {minimum: 30, maximum: 30000}
  validates :doc_slug, uniqueness: true, if: -> { doc_slug.present? }

  scope :undeleted, -> { where(deleted: false) }
  scope :deleted, -> { where(deleted: true) }
  scope :qa_only, -> { where(post_type_id: [Question.post_type_id, Answer.post_type_id]) }

  after_save :check_attribution_notice
  after_save :modify_author_reputation
  after_save :reset_last_activity
  after_create :create_initial_revision

  def self.search(term)
    match_search term, posts: :body_markdown
  end

  # Double-define: initial definitions are less efficient, so if we have a record of the post type we'll
  # override them later with more efficient methods.
  ['Question', 'Answer', 'PolicyDoc', 'HelpDoc'].each do |pt|
    klass = pt.classify
    define_method "#{pt.underscore}?" do
      post_type_id == klass.post_type_id
    end
  end

  PostType.all.each do |pt|
    define_method "#{pt.name.underscore}?" do
      post_type_id == pt.id
    end
  end

  def reassign_user(new_user)
    # Three updates: one to remove rep from previous user, one to reassign, one to re-grant rep to new user
    update(deleted: true, deleted_at: DateTime.now, deleted_by: User.find(-1))
    update(user: new_user)
    votes.update_all(recv_user_id: new_user.id)
    update(deleted: false, deleted_at: nil, deleted_by: nil)
  end

  def remove_attribution_notice!
    update(att_source: nil, att_license_link: nil, att_license_name: nil)
  end

  private<commit_msg>I swear I know what I'm doing<commit_after>class Post < ApplicationRecord
  belongs_to :user
  belongs_to :post_type
  belongs_to :parent, class_name: 'Post', required: false, counter_cache: :answer_count
  belongs_to :closed_by, class_name: 'User', required: false
  belongs_to :deleted_by, class_name: 'User', required: false
  has_and_belongs_to_many :tags, dependent: :destroy
  has_many :votes, dependent: :destroy
  has_many :comments, dependent: :destroy
  has_many :post_histories, dependent: :destroy
  has_many :flags, dependent: :destroy
  has_many :children, class_name: 'Post', foreign_key: 'parent_id', dependent: :destroy

  serialize :tags_cache, Array

  validates :body, presence: true, length: {minimum: 30, maximum: 30000}
  validates :doc_slug, uniqueness: true, if: -> { doc_slug.present? }

  scope :undeleted, -> { where(deleted: false) }
  scope :deleted, -> { where(deleted: true) }
  scope :qa_only, -> { where(post_type_id: [Question.post_type_id, Answer.post_type_id]) }

  after_save :check_attribution_notice
  after_save :modify_author_reputation
  after_save :reset_last_activity
  after_create :create_initial_revision

  def self.search(term)
    match_search term, posts: :body_markdown
  end

  # Double-define: initial definitions are less efficient, so if we have a record of the post type we'll
  # override them later with more efficient methods.
  ['Question', 'Answer', 'PolicyDoc', 'HelpDoc'].each do |pt|
    klass = pt.constantize
    define_method "#{pt.underscore}?" do
      post_type_id == klass.post_type_id
    end
  end

  PostType.all.each do |pt|
    define_method "#{pt.name.underscore}?" do
      post_type_id == pt.id
    end
  end

  def reassign_user(new_user)
    # Three updates: one to remove rep from previous user, one to reassign, one to re-grant rep to new user
    update(deleted: true, deleted_at: DateTime.now, deleted_by: User.find(-1))
    update(user: new_user)
    votes.update_all(recv_user_id: new_user.id)
    update(deleted: false, deleted_at: nil, deleted_by: nil)
  end

  def remove_attribution_notice!
    update(att_source: nil, att_license_link: nil, att_license_name: nil)
  end

  private
{'fraction_non_alphanumeric': 0.10140910140910141, 'fraction_numerical': 0.003696003696003696, 'mean_word_length': 4.660130718954249, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '463879', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1433, 'n_words': 401}