<commit_before>require 'gds_api/publishing_api_v2'

class DocumentsController <  ApplicationController
  include ActionView::Context
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::TextHelper

  before_action :fetch_document, only: [:edit, :show]

  def index
    unless params[:document_type]
      redirect_to "/#{document_types.keys.first}"
      return
    end

    @documents = publishing_api.get_content_items(
      content_format: current_format.format_name,
      fields: [
        :base_path,
        :content_id,
        :title,
        :public_updated_at,
      ]
    ).to_ostruct
  end

  def new
    @document = document_klass.new
  end

  def create
    @document = document_klass.new(
      filtered_params(params[current_format.format_name])
    )

    if @document.valid?
      presented_document = DocumentPresenter.new(@document)
      presented_links = DocumentLinksPresenter.new(@document)

      item_request = publishing_api.put_content(@document.content_id, presented_document.to_json)
      links_request = publishing_api.put_links(@document.content_id, presented_links.to_json)

      if item_request.code == 200 && links_request.code == 200
        flash.now[:success] = "Created #{@document.title}"
        redirect_to documents_path(current_format.document_type)
      else
        flash.now[:danger] = "There was an error publishing #{@document.title}. Please try again later."
        render :new
      end
    else
      document_errors = @document.errors.messages
      errors = content_tag(:p,
        %Q{
          There #{document_errors.length > 1 ? 'were' : 'was' } the following
          #{document_errors.length > 1 ? 'errors' : 'error' } with your
          #{current_format.title.singularize}:
        }
      )
      errors += content_tag :ul do
        @document.errors.full_messages.map { |e| content_tag(:li, e) }.join('').html_safe
      end

      flash.now[:danger] = errors
      render :new, status: 422
    end
  end

  def show; end

  def edit; end<commit_msg>Modify creation error message<commit_after>require 'gds_api/publishing_api_v2'

class DocumentsController <  ApplicationController
  include ActionView::Context
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::TextHelper

  before_action :fetch_document, only: [:edit, :show]

  def index
    unless params[:document_type]
      redirect_to "/#{document_types.keys.first}"
      return
    end

    @documents = publishing_api.get_content_items(
      content_format: current_format.format_name,
      fields: [
        :base_path,
        :content_id,
        :title,
        :public_updated_at,
      ]
    ).to_ostruct
  end

  def new
    @document = document_klass.new
  end

  def create
    @document = document_klass.new(
      filtered_params(params[current_format.format_name])
    )

    if @document.valid?
      presented_document = DocumentPresenter.new(@document)
      presented_links = DocumentLinksPresenter.new(@document)

      item_request = publishing_api.put_content(@document.content_id, presented_document.to_json)
      links_request = publishing_api.put_links(@document.content_id, presented_links.to_json)

      if item_request.code == 200 && links_request.code == 200
        flash.now[:success] = "Created #{@document.title}"
        redirect_to documents_path(current_format.document_type)
      else
        flash.now[:danger] = "There was an error creating #{@document.title}. Please try again later."
        render :new
      end
    else
      document_errors = @document.errors.messages
      errors = content_tag(:p,
        %Q{
          There #{document_errors.length > 1 ? 'were' : 'was' } the following
          #{document_errors.length > 1 ? 'errors' : 'error' } with your
          #{current_format.title.singularize}:
        }
      )
      errors += content_tag :ul do
        @document.errors.full_messages.map { |e| content_tag(:li, e) }.join('').html_safe
      end

      flash.now[:danger] = errors
      render :new, status: 422
    end
  end

  def show; end

  def edit; end
{'fraction_non_alphanumeric': 0.10112359550561797, 'fraction_numerical': 0.005862237420615535, 'mean_word_length': 2.991228070175439, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3733205', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1208, 'n_words': 254}