Sha256: 261a9f30c179dc0005eb7888f838385dd90fb90eef670ba88bac6ab0295c8bca

Contents?: true

Size: 1.79 KB

Versions: 8

Compression:

Stored size: 1.79 KB

Contents

module GOVUKDesignSystemFormBuilder
  module Elements
    class ErrorSummary < Base
      include Traits::Error

      def initialize(builder, object_name, title)
        @builder = builder
        @object_name = object_name
        @title = title
      end

      def html
        return nil unless object_has_errors?

        tag.div(class: summary_class, **summary_options) do
          safe_join([title, summary])
        end
      end

    private

      def title
        tag.h2(@title, id: summary_title_id, class: summary_class('title'))
      end

      def summary
        tag.div(class: summary_class('body')) do
          tag.ul(class: [%(#{brand}-list), summary_class('list')]) do
            safe_join(list)
          end
        end
      end

      def list
        @builder.object.errors.messages.map do |attribute, messages|
          list_item(attribute, messages.first)
        end
      end

      def list_item(attribute, message)
        tag.li(link_to(message, same_page_link(field_id(attribute)), data: { turbolinks: false }))
      end

      def same_page_link(target)
        '#'.concat(target)
      end

      def summary_class(part = nil)
        if part
          %(#{brand}-error-summary).concat('__', part)
        else
          %(#{brand}-error-summary)
        end
      end

      def field_id(attribute)
        build_id('field-error', attribute_name: attribute)
      end

      def summary_title_id
        'error-summary-title'
      end

      def object_has_errors?
        @builder.object.errors.any?
      end

      def summary_options
        {
          tabindex: -1,
          role: 'alert',
          data: {
            module: %(#{brand}-error-summary)
          },
          aria: {
            labelledby: summary_title_id
          }
        }
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-2.1.0 lib/govuk_design_system_formbuilder/elements/error_summary.rb
govuk_design_system_formbuilder-2.0.0 lib/govuk_design_system_formbuilder/elements/error_summary.rb
govuk_design_system_formbuilder-2.0.0b4 lib/govuk_design_system_formbuilder/elements/error_summary.rb
govuk_design_system_formbuilder-1.2.9 lib/govuk_design_system_formbuilder/elements/error_summary.rb
govuk_design_system_formbuilder-2.0.0b3 lib/govuk_design_system_formbuilder/elements/error_summary.rb
govuk_design_system_formbuilder-2.0.0b2 lib/govuk_design_system_formbuilder/elements/error_summary.rb
govuk_design_system_formbuilder-2.0.0b1 lib/govuk_design_system_formbuilder/elements/error_summary.rb
govuk_design_system_formbuilder-1.2.7 lib/govuk_design_system_formbuilder/elements/error_summary.rb