<commit_before>class Post < ApplicationRecord
  STATUSES = %w(active pending flagged deleted)

  define_callbacks :delete
  define_callbacks :undelete
  has_many :notes, lambda { order "id DESC" }
  has_one :flag_detail, :class_name => "FlaggedPostDetail"
  belongs_to :user
  before_validation :set_random!, :on => :create
  before_create :set_index_timestamp!
  belongs_to :approver, :class_name => "User"
  attr_accessor :updater_ip_addr, :updater_user_id
  attr_accessor :metatag_flagged
  has_many :post_votes
  has_many :avatars, :class_name => "User", :foreign_key => "avatar_post_id"
  set_callback :delete, :before, :clear_avatars
  after_save :commit_flag
  has_and_belongs_to_many :_tags, :class_name => "Tag"
  scope :available, lambda { where.not :status => "deleted" }
  scope :has_any_tags, lambda { |tags| where("posts.tags_array && ARRAY[?]::varchar[]", Array(tags)) }
  scope :has_all_tags, lambda { |tags| where("posts.tags_array @> ARRAY[?]::varchar[]", Array(tags)) }
  scope :flagged, lambda { where "status = ?", "flagged" }

  def self.slow_has_all_tags(tags)
    p = Post.scoped
    t_ids = Tag.where(:name => tags).pluck(:id)
    t_ids.each do |t_id|
      p = p.where(:id => PostsTag.where(:tag_id => t_id).select(:post_id))
    end
    p
  end

  def self.slow_has_any_tags(tags)
    t_ids = Tag.where(:name => tags).pluck(:id)
    Post.where(:id => PostsTag.where(:tag_id => t_ids).select(:post_id))
  end

  def next_id
    Post.available.where("posts.id > ?", id).minimum(:id)
  end

  def previous_id
    Post.available.where("posts.id < ?", id).maximum(:id)
  end<commit_msg>Limit maximum length for post source<commit_after>class Post < ApplicationRecord
  STATUSES = %w(active pending flagged deleted)

  define_callbacks :delete
  define_callbacks :undelete
  has_many :notes, lambda { order "id DESC" }
  has_one :flag_detail, :class_name => "FlaggedPostDetail"
  belongs_to :user
  before_validation :set_random!, :on => :create
  before_create :set_index_timestamp!
  belongs_to :approver, :class_name => "User"
  attr_accessor :updater_ip_addr, :updater_user_id
  attr_accessor :metatag_flagged
  has_many :post_votes
  has_many :avatars, :class_name => "User", :foreign_key => "avatar_post_id"
  set_callback :delete, :before, :clear_avatars
  after_save :commit_flag
  has_and_belongs_to_many :_tags, :class_name => "Tag"
  scope :available, lambda { where.not :status => "deleted" }
  scope :has_any_tags, lambda { |tags| where("posts.tags_array && ARRAY[?]::varchar[]", Array(tags)) }
  scope :has_all_tags, lambda { |tags| where("posts.tags_array @> ARRAY[?]::varchar[]", Array(tags)) }
  scope :flagged, lambda { where "status = ?", "flagged" }

  validates_length_of :source, maximum: 1000

  def self.slow_has_all_tags(tags)
    p = Post.scoped
    t_ids = Tag.where(:name => tags).pluck(:id)
    t_ids.each do |t_id|
      p = p.where(:id => PostsTag.where(:tag_id => t_id).select(:post_id))
    end
    p
  end

  def self.slow_has_any_tags(tags)
    t_ids = Tag.where(:name => tags).pluck(:id)
    Post.where(:id => PostsTag.where(:tag_id => t_ids).select(:post_id))
  end

  def next_id
    Post.available.where("posts.id > ?", id).minimum(:id)
  end

  def previous_id
    Post.available.where("posts.id < ?", id).maximum(:id)
  end
{'fraction_non_alphanumeric': 0.14216133942161338, 'fraction_numerical': 0.0012176560121765602, 'mean_word_length': 4.675302245250432, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, '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': '2050733', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1205, 'n_words': 246}