<commit_before># == Schema Information
#
# Table name: champs
#
#  id               :integer          not null, primary key
#  private          :boolean          default(FALSE), not null
#  row              :integer
#  type             :string
#  value            :string
#  created_at       :datetime
#  updated_at       :datetime
#  dossier_id       :integer
#  etablissement_id :integer
#  parent_id        :bigint
#  type_de_champ_id :integer
#
class Champ < ApplicationRecord
  belongs_to :dossier, -> { with_discarded }, inverse_of: :champs, touch: true, optional: false
  belongs_to :type_de_champ, inverse_of: :champ, optional: false
  belongs_to :parent, class_name: 'Champ', optional: true
  has_many :commentaires
  has_one_attached :piece_justificative_file

  # We declare champ specific relationships (Champs::CarteChamp, Champs::SiretChamp and Champs::RepetitionChamp)
  # here because otherwise we can't easily use includes in our queries.
  has_many :geo_areas, dependent: :destroy
  belongs_to :etablissement, optional: true, dependent: :destroy
  has_many :champs, -> { ordered }, foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy

  delegate :libelle,
    :type_champ,
    :procedure,
    :order_place,
    :mandatory?,
    :description,
    :drop_down_list_options,
    :drop_down_list_options?,
    :drop_down_list_disabled_options,
    :drop_down_list_enabled_non_empty_options,
    :exclude_from_export?,
    :exclude_from_view?,
    :repetition?,
    :dossier_link?,
    to: :type_de_champ

  scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) }
  scope :public_only, -> { where(private: false) }
  scope :private_only, -> { where(private: true) }
  scope :ordered, -> { includes(:type_de_champ).order(:row, 'types_de_champ.order_place') }
  scope :public_ordered, -> { public_only.joins(dossier: { revision: :revision_types_de_champ }).where('procedure_revision_types_de_champ.type_de_champ_id = champs.type_de_champ_id').order(:position) }
  scope :private_ordered, -> { private_only.joins(dossier: { revision: :revision_types_de_champ_private }).where('procedure_revision_types_de_champ.type_de_champ_id = champs.type_de_champ_id').order(:position) }

  scope :root, -> { where(parent_id: nil) }

  before_validation :set_dossier_id, if: :needs_dossier_id?

  validates :type_de_champ_id, uniqueness: { scope: [:dossier_id, :row] }

  def public?
    !private?
  end

  def siblings
    if parent
      parent&.champs
    elsif public?
      dossier&.champs
    else
      dossier&.champs_private
    end
  end

  def mandatory_and_blank?
    mandatory? && blank?
  end

  def blank?
    case type_de_champ.type_champ
    when TypeDeChamp.type_champs.fetch(:carte)
      geo_areas.blank? || value == '[]'
    else<commit_msg>Fix champ order joins sql error<commit_after># == Schema Information
#
# Table name: champs
#
#  id               :integer          not null, primary key
#  private          :boolean          default(FALSE), not null
#  row              :integer
#  type             :string
#  value            :string
#  created_at       :datetime
#  updated_at       :datetime
#  dossier_id       :integer
#  etablissement_id :integer
#  parent_id        :bigint
#  type_de_champ_id :integer
#
class Champ < ApplicationRecord
  belongs_to :dossier, -> { with_discarded }, inverse_of: :champs, touch: true, optional: false
  belongs_to :type_de_champ, inverse_of: :champ, optional: false
  belongs_to :parent, class_name: 'Champ', optional: true
  has_many :commentaires
  has_one_attached :piece_justificative_file

  # We declare champ specific relationships (Champs::CarteChamp, Champs::SiretChamp and Champs::RepetitionChamp)
  # here because otherwise we can't easily use includes in our queries.
  has_many :geo_areas, dependent: :destroy
  belongs_to :etablissement, optional: true, dependent: :destroy
  has_many :champs, -> { ordered }, foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy

  delegate :libelle,
    :type_champ,
    :procedure,
    :order_place,
    :mandatory?,
    :description,
    :drop_down_list_options,
    :drop_down_list_options?,
    :drop_down_list_disabled_options,
    :drop_down_list_enabled_non_empty_options,
    :exclude_from_export?,
    :exclude_from_view?,
    :repetition?,
    :dossier_link?,
    to: :type_de_champ

  scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) }
  scope :public_only, -> { where(private: false) }
  scope :private_only, -> { where(private: true) }
  scope :ordered, -> { includes(:type_de_champ).order(:row, 'types_de_champ.order_place') }
  scope :public_ordered, -> { public_only.joins(dossier: { revision: :revision_types_de_champ }).where('procedure_revision_types_de_champ.type_de_champ_id = champs.type_de_champ_id').order(:position) }
  # we need to do private champs order as manual join to avoid conflicting join names
  scope :private_ordered, -> do
    private_only.joins('INNER JOIN types_de_champ types_de_champ_private
      ON types_de_champ_private.id = champs.type_de_champ_id AND types_de_champ_private.private = true
      INNER JOIN procedure_revision_types_de_champ procedure_revision_types_de_champ_private
      ON procedure_revision_types_de_champ_private.type_de_champ_id = types_de_champ_private.id').order(:position)
  end

  scope :root, -> { where(parent_id: nil) }

  before_validation :set_dossier_id, if: :needs_dossier_id?

  validates :type_de_champ_id, uniqueness: { scope: [:dossier_id, :row] }

  def public?
    !private?
  end

  def siblings
    if parent
      parent&.champs
    elsif public?
      dossier&.champs
    else
      dossier&.champs_private
    end
  end

  def mandatory_and_blank?
    mandatory? && blank?
  end

  def blank?
    case type_de_champ.type_champ
    when TypeDeChamp.type_champs.fetch(:carte)
      geo_areas.blank? || value == '[]'
    else
{'fraction_non_alphanumeric': 0.10145911096029861, 'fraction_numerical': 0.0, 'mean_word_length': 3.8678777869529313, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '4696222', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1981, 'n_words': 421}