<commit_before>class EmailAlertPresenter
  attr_reader :document

  def initialize(document)
    @document = document
  end

  def to_json
    {
      subject: document.title + " updated",
      body: body,
      tags: tags,
      document_type: document.publishing_api_document_type,
    }.merge(extra_options)
  end

private

  def body
    if document.publishing_api_document_type == "medical_safety_alert"
      standard_body("email_alerts/medical_safety_alerts/publication")
    else
      standard_body("email_alerts/publication")
    end
  end

  def tags
    { format: document.publishing_api_document_type }.deep_merge(metadata)
  end

  def metadata
    merged_fields = document.format_specific_fields.map { |f|
      {
        f => document.send(f)
      }
    }.reduce({}, :merge)
    merged_fields.reject { |_k, v| v.blank? }
  end

  def standard_body(template_path)
    view_renderer.render(
      template: template_path,
      formats: ["html"],
      locals:   {
        document_title: document.title,
        document_summary: document.summary,
        document_url: File.join(Plek.current.website_root, document.base_path),
        document_change_note: document.change_note,
      }
    )
  end

  def extra_options
    {
      header: header,
      footer: footer,
    }.reject { |_k, v| v.nil? }
  end

  def view_renderer
    ActionView::Base.new("app/views")
  end
<commit_msg>Refactor EmailAlertPresenter#tags<commit_after>class EmailAlertPresenter
  attr_reader :document

  def initialize(document)
    @document = document
  end

  def to_json
    {
      subject: document.title + " updated",
      body: body,
      tags: tags,
      document_type: document.publishing_api_document_type,
    }.merge(extra_options)
  end

private

  def body
    if document.publishing_api_document_type == "medical_safety_alert"
      standard_body("email_alerts/medical_safety_alerts/publication")
    else
      standard_body("email_alerts/publication")
    end
  end

  def tags
    {
      format: document.publishing_api_document_type
    }.deep_merge(document.format_specific_metadata.reject { |_k, v| v.blank? })
  end

  def standard_body(template_path)
    view_renderer.render(
      template: template_path,
      formats: ["html"],
      locals:   {
        document_title: document.title,
        document_summary: document.summary,
        document_url: File.join(Plek.current.website_root, document.base_path),
        document_change_note: document.change_note,
      }
    )
  end

  def extra_options
    {
      header: header,
      footer: footer,
    }.reject { |_k, v| v.nil? }
  end

  def view_renderer
    ActionView::Base.new("app/views")
  end

{'fraction_non_alphanumeric': 0.09263392857142858, 'fraction_numerical': 0.0, 'mean_word_length': 2.966076696165192, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '7377098', 'n_tokens_mistral': 881, 'n_tokens_neox': 853, 'n_words': 162}