<commit_before>module Document::Workflow
  extend ActiveSupport::Concern

  included do
    include ::Transitions
    include ActiveRecord::Transitions
    include Rails.application.routes.url_helpers
    include PublicDocumentRoutesHelper

    default_scope where(%{documents.state <> "deleted"})
    scope :draft, where(state: "draft")
    scope :submitted, where(state: "submitted")
    scope :rejected, where(state: "rejected")
    scope :published, where(state: "published")

    state_machine do
      state :draft
      state :submitted
      state :rejected
      state :published
      state :archived
      state :deleted

      event :delete do
        transitions from: [:draft, :submitted, :rejected], to: :deleted
      end

      event :submit do
        transitions from: [:draft, :rejected], to: :submitted
      end

      event :reject do
        transitions from: :submitted, to: :rejected
      end

      event :publish, success: :on_publish_success do
        transitions from: [:draft, :submitted], to: :published
      end

      event :archive, success: :on_archive_success do
        transitions from: :published, to: :archived
      end
    end

    validates_with DocumentHasNoUnpublishedDocumentsValidator, on: :create
    validates_with DocumentHasNoOtherPublishedDocumentsValidator, on: :create
  end

  def on_publish_success
    archive_previous_documents
    update_in_search_index unless instance_of?(Document)
  end

  def on_archive_success
    remove_from_search_index unless instance_of?(Document)
  end

  def archive_previous_documents
    document_identity.documents.published.each do |document|
      document.archive! unless document == self
    end
  end

  def update_in_search_index
    Rummageable.index(search_index)
  end

  def remove_from_search_index
    Rummageable.delete(public_document_path(self))
  end

  def search_index
    { "title" => title, "link" => public_document_path(self),
      "indexable_content" => body_without_markup, "format" => type.underscore }
  end

  def body_without_markup
    Govspeak::Document.new(body).to_text
  end

  module ClassMethods
    def search_index_published
      published.map(&:search_index)
    end
  end

  class DocumentHasNoUnpublishedDocumentsValidator < ActiveModel::Validator
    def validate(record)
      if record.document_identity && (existing_edition = record.document_identity.unpublished_edition)
        record.errors.add(:base, "There is already an active #{existing_edition.state} edition for this document")
      end
    end
  end

  class DocumentHasNoOtherPublishedDocumentsValidator < ActiveModel::Validator
    def validate(record)
      if record.published? && record.document_identity && record.document_identity.documents.published.any?
        record.errors.add(:base, "There is already a published edition for this document")
      end
    end
  end

end<commit_msg>Remove test-only code from document publish/archive callbacks<commit_after>module Document::Workflow
  extend ActiveSupport::Concern

  included do
    include ::Transitions
    include ActiveRecord::Transitions
    include Rails.application.routes.url_helpers
    include PublicDocumentRoutesHelper

    default_scope where(%{documents.state <> "deleted"})
    scope :draft, where(state: "draft")
    scope :submitted, where(state: "submitted")
    scope :rejected, where(state: "rejected")
    scope :published, where(state: "published")

    state_machine do
      state :draft
      state :submitted
      state :rejected
      state :published
      state :archived
      state :deleted

      event :delete do
        transitions from: [:draft, :submitted, :rejected], to: :deleted
      end

      event :submit do
        transitions from: [:draft, :rejected], to: :submitted
      end

      event :reject do
        transitions from: :submitted, to: :rejected
      end

      event :publish, success: :on_publish_success do
        transitions from: [:draft, :submitted], to: :published
      end

      event :archive, success: :on_archive_success do
        transitions from: :published, to: :archived
      end
    end

    validates_with DocumentHasNoUnpublishedDocumentsValidator, on: :create
    validates_with DocumentHasNoOtherPublishedDocumentsValidator, on: :create
  end

  def on_publish_success
    archive_previous_documents
    update_in_search_index
  end

  def on_archive_success
    remove_from_search_index
  end

  def archive_previous_documents
    document_identity.documents.published.each do |document|
      document.archive! unless document == self
    end
  end

  def update_in_search_index
    Rummageable.index(search_index)
  end

  def remove_from_search_index
    Rummageable.delete(public_document_path(self))
  end

  def search_index
    { "title" => title, "link" => public_document_path(self),
      "indexable_content" => body_without_markup, "format" => type.underscore }
  end

  def body_without_markup
    Govspeak::Document.new(body).to_text
  end

  module ClassMethods
    def search_index_published
      published.map(&:search_index)
    end
  end

  class DocumentHasNoUnpublishedDocumentsValidator < ActiveModel::Validator
    def validate(record)
      if record.document_identity && (existing_edition = record.document_identity.unpublished_edition)
        record.errors.add(:base, "There is already an active #{existing_edition.state} edition for this document")
      end
    end
  end

  class DocumentHasNoOtherPublishedDocumentsValidator < ActiveModel::Validator
    def validate(record)
      if record.published? && record.document_identity && record.document_identity.documents.published.any?
        record.errors.add(:base, "There is already a published edition for this document")
      end
    end
  end

end
{'fraction_non_alphanumeric': 0.07612456747404844, 'fraction_numerical': 0.0, 'mean_word_length': 3.6211031175059953, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '4204161', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1607, 'n_words': 421}