<commit_before>class Consultation < Publicationesque
  include Edition::Images
  include Edition::NationalApplicability
  include Edition::RoleAppointments
  include Edition::FactCheckable
  include Edition::AlternativeFormatProvider
  include Edition::CanApplyToLocalGovernmentThroughRelatedPolicies
  include Edition::TopicalEvents

  validates :opening_at, presence: true, unless: ->(consultation) { consultation.can_have_some_invalid_data? }
  validates :closing_at, presence: true, unless: ->(consultation) { consultation.can_have_some_invalid_data? }
  validates :external_url, presence: true, if: :external?
  validates :external_url, uri: true, allow_blank: true
  validate :validate_closes_after_opens

  has_one :outcome, class_name: 'ConsultationOutcome', foreign_key: :edition_id, dependent: :destroy
  has_one :public_feedback, class_name: 'ConsultationPublicFeedback', foreign_key: :edition_id, dependent: :destroy
  has_one :consultation_participation, foreign_key: :edition_id, dependent: :destroy

  accepts_nested_attributes_for :consultation_participation, reject_if: :all_blank_or_empty_hashes

  scope :closed, -> { where("closing_at < ?",  Time.zone.now) }
  scope :closed_since, ->(time) { closed.where('closing_at >= ?', time) }
  scope :open, -> { where('closing_at >= ? AND opening_at <= ?', Time.zone.now, Time.zone.now) }
  scope :open_since, ->(time) { open.where('opening_at >= ?', time) }
  scope :upcoming, -> { where('opening_at > ?', Time.zone.now) }
  scope :responded, -> { joins(:outcome) }

  add_trait do
    def process_associations_after_save(edition)
      if @edition.consultation_participation.present?
        attributes = @edition.consultation_participation.attributes.except('id', 'edition_id')
        edition.create_consultation_participation(attributes)
      end

      if @edition.outcome.present?
        new_outcome = edition.build_outcome(@edition.outcome.attributes.except('id', 'edition_id'))
        @edition.outcome.attachments.each do |attachment|
          new_outcome.attachments << attachment.deep_clone
        end
        new_outcome.save!
      end

      if @edition.public_feedback.present?
        new_feedback = edition.build_public_feedback(@edition.public_feedback.attributes.except('id', 'edition_id'))
        @edition.public_feedback.attachments.each do |attachment|
          new_feedback.attachments << attachment.deep_clone
        end
        new_feedback.save!
      end
    end
  end

  # A consultation changes outside the edition workflow when it opens or
  # closes. We need to republish the consultation at these times to ensure
  # changes are reflected in any external systems.
  after_save do
    if opening_at.try(:future?)
      PublishingApiDocumentRepublishingWorker
        .perform_at(opening_at, document.id)
    end

    if closing_at.try(:future?)
      PublishingApiDocumentRepublishingWorker
        .perform_at(closing_at, document.id)
    end
  end

  def rendering_app
    # TODO rendered by Whitehall::RenderingApp::GOVERNMENT_FRONTEND
    # but we need to return WHITEHALL_FRONTEND here to continue to use
    # Whitehall for preview until draft links are implemented
    Whitehall::RenderingApp::WHITEHALL_FRONTEND
  end

  def allows_inline_attachments?
    false
  end

  def not_yet_open?
    opening_at.nil? || (opening_at > Time.zone.now)
  end

  def open?
    opening_at.present? && !closed? && opening_at <= Time.zone.now
  end

  def closed?
    closing_at.nil? || (closing_at < Time.zone.now)
  end

  def outcome_published?
    closed? && outcome.present?
  end

  def outcome_published_on
    outcome.published_on
  end

  def allows_attachment_references?
    true
  end

  def can_have_attached_house_of_commons_papers?<commit_msg>Switch Consultation rendering app to government-frontend<commit_after>class Consultation < Publicationesque
  include Edition::Images
  include Edition::NationalApplicability
  include Edition::RoleAppointments
  include Edition::FactCheckable
  include Edition::AlternativeFormatProvider
  include Edition::CanApplyToLocalGovernmentThroughRelatedPolicies
  include Edition::TopicalEvents

  validates :opening_at, presence: true, unless: ->(consultation) { consultation.can_have_some_invalid_data? }
  validates :closing_at, presence: true, unless: ->(consultation) { consultation.can_have_some_invalid_data? }
  validates :external_url, presence: true, if: :external?
  validates :external_url, uri: true, allow_blank: true
  validate :validate_closes_after_opens

  has_one :outcome, class_name: 'ConsultationOutcome', foreign_key: :edition_id, dependent: :destroy
  has_one :public_feedback, class_name: 'ConsultationPublicFeedback', foreign_key: :edition_id, dependent: :destroy
  has_one :consultation_participation, foreign_key: :edition_id, dependent: :destroy

  accepts_nested_attributes_for :consultation_participation, reject_if: :all_blank_or_empty_hashes

  scope :closed, -> { where("closing_at < ?",  Time.zone.now) }
  scope :closed_since, ->(time) { closed.where('closing_at >= ?', time) }
  scope :open, -> { where('closing_at >= ? AND opening_at <= ?', Time.zone.now, Time.zone.now) }
  scope :open_since, ->(time) { open.where('opening_at >= ?', time) }
  scope :upcoming, -> { where('opening_at > ?', Time.zone.now) }
  scope :responded, -> { joins(:outcome) }

  add_trait do
    def process_associations_after_save(edition)
      if @edition.consultation_participation.present?
        attributes = @edition.consultation_participation.attributes.except('id', 'edition_id')
        edition.create_consultation_participation(attributes)
      end

      if @edition.outcome.present?
        new_outcome = edition.build_outcome(@edition.outcome.attributes.except('id', 'edition_id'))
        @edition.outcome.attachments.each do |attachment|
          new_outcome.attachments << attachment.deep_clone
        end
        new_outcome.save!
      end

      if @edition.public_feedback.present?
        new_feedback = edition.build_public_feedback(@edition.public_feedback.attributes.except('id', 'edition_id'))
        @edition.public_feedback.attachments.each do |attachment|
          new_feedback.attachments << attachment.deep_clone
        end
        new_feedback.save!
      end
    end
  end

  # A consultation changes outside the edition workflow when it opens or
  # closes. We need to republish the consultation at these times to ensure
  # changes are reflected in any external systems.
  after_save do
    if opening_at.try(:future?)
      PublishingApiDocumentRepublishingWorker
        .perform_at(opening_at, document.id)
    end

    if closing_at.try(:future?)
      PublishingApiDocumentRepublishingWorker
        .perform_at(closing_at, document.id)
    end
  end

  def rendering_app
    Whitehall::RenderingApp::GOVERNMENT_FRONTEND
  end

  def allows_inline_attachments?
    false
  end

  def not_yet_open?
    opening_at.nil? || (opening_at > Time.zone.now)
  end

  def open?
    opening_at.present? && !closed? && opening_at <= Time.zone.now
  end

  def closed?
    closing_at.nil? || (closing_at < Time.zone.now)
  end

  def outcome_published?
    closed? && outcome.present?
  end

  def outcome_published_on
    outcome.published_on
  end

  def allows_attachment_references?
    true
  end

  def can_have_attached_house_of_commons_papers?
{'fraction_non_alphanumeric': 0.09369467520087157, 'fraction_numerical': 0.0, 'mean_word_length': 4.2796549245147375, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, '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': '2613117', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2229, 'n_words': 477}