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

  included do
    validates :published_at, presence: true, if: -> document { document.published? }
  end

  module ClassMethods
    def by_published_at
      order(arel_table[:published_at].desc)
    end

    def latest_published_at
      latest_published_document = published.by_published_at.first
      latest_published_document && latest_published_document.published_at
    end
  end

  def publishable_by?(user)
    reason_to_prevent_publication_by(user).nil?
  end

  def force_publishable_by?(user)
    reason_to_prevent_publication_by(user, force: true).nil?
  end

  def reason_to_prevent_publication_by(user, options = {})
    if published?
      "This edition has already been published"
    elsif archived?
      "This edition has been archived"
    elsif deleted?
      "This edition has been deleted"
    elsif rejected?
      "This edition has been rejected"
    elsif !submitted? && !options[:force]
      "Not ready for publication"
    elsif user == creator && !options[:force]
      "You are not the second set of eyes"
    elsif !user.departmental_editor?
      "Only departmental editors can publish"
    end
  end

  def publish_as(user, options = {})
    if options[:force] && force_publishable_by?(user) || publishable_by?(user)
      self.lock_version = lock_version
      self.published_at = Time.zone.now
      publish!
      true
    else
      errors.add(:base, reason_to_prevent_publication_by(user, options))
      false
    end
  end
end<commit_msg>Simplify Document.latest_published_at implementation<commit_after>module Document::Publishing
  extend ActiveSupport::Concern

  included do
    validates :published_at, presence: true, if: -> document { document.published? }
  end

  module ClassMethods
    def by_published_at
      order(arel_table[:published_at].desc)
    end

    def latest_published_at
      published.maximum(:published_at)
    end
  end

  def publishable_by?(user)
    reason_to_prevent_publication_by(user).nil?
  end

  def force_publishable_by?(user)
    reason_to_prevent_publication_by(user, force: true).nil?
  end

  def reason_to_prevent_publication_by(user, options = {})
    if published?
      "This edition has already been published"
    elsif archived?
      "This edition has been archived"
    elsif deleted?
      "This edition has been deleted"
    elsif rejected?
      "This edition has been rejected"
    elsif !submitted? && !options[:force]
      "Not ready for publication"
    elsif user == creator && !options[:force]
      "You are not the second set of eyes"
    elsif !user.departmental_editor?
      "Only departmental editors can publish"
    end
  end

  def publish_as(user, options = {})
    if options[:force] && force_publishable_by?(user) || publishable_by?(user)
      self.lock_version = lock_version
      self.published_at = Time.zone.now
      publish!
      true
    else
      errors.add(:base, reason_to_prevent_publication_by(user, options))
      false
    end
  end
end
{'fraction_non_alphanumeric': 0.08032786885245902, 'fraction_numerical': 0.0, 'mean_word_length': 3.4281567489114657, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3846853', 'n_tokens_mistral': 924, 'n_tokens_neox': 910, 'n_words': 236}