Sha256: 8637429cc7ba825b97ecb026fa5c0e500d07fe84b2b35f2e033f5fff663fb1c1

Contents?: true

Size: 2 KB

Versions: 80

Compression:

Stored size: 2 KB

Contents

module GovukPublishingComponents
  module AppHelpers
    class TableHelper
      def self.helper(context, caption = nil, opt = {})
        builder = TableBuilder.new(context.tag)

        classes = %w(gem-c-table govuk-table)
        classes << "govuk-table--sortable" if opt[:sortable]

        caption_classes = %w(govuk-table__caption)
        caption_classes << opt[:caption_classes] if opt[:caption_classes]

        context.tag.table class: classes do
          context.concat context.tag.caption caption, class: caption_classes
          yield(builder)
        end
      end
      class TableBuilder
        include ActionView::Helpers::UrlHelper
        include ActionView::Helpers::TagHelper

        attr_reader :tag

        def initialize(tag)
          @tag = tag
        end

        def head
          tag.thead class: "govuk-table__head" do
            tag.tr class: "govuk-table__row" do
              yield(self)
            end
          end
        end

        def body
          tag.tbody class: "govuk-table__body" do
            yield(self)
          end
        end

        def row
          tag.tr class: "govuk-table__row" do
            yield(self)
          end
        end

        def header(str, opt = {})
          classes = %w(govuk-table__header)
          classes << "govuk-table__header--#{opt[:format]}" if opt[:format]
          classes << "govuk-table__header--active" if opt[:sort_direction]
          link_clases = %w(app-table__sort-link)
          link_clases << "app-table__sort-link--#{opt[:sort_direction]}" if opt[:sort_direction]
          str = link_to str, opt[:href], class: link_clases if opt[:href]
          tag.th str, class: classes, scope: opt[:scope] || "col"
        end

        def cell(str, opt = {})
          classes = %w(govuk-table__cell)
          classes << "govuk-table__cell--" + opt[:format] if opt[:format]
          classes << "govuk-table__cell--empty" unless str
          str ||= "Not set"
          tag.td str, class: classes
        end
      end
    end
  end
end

Version data entries

80 entries across 80 versions & 1 rubygems

Version Path
govuk_publishing_components-17.9.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-17.8.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-17.7.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-17.6.1 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-17.6.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-17.5.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-17.4.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-17.3.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-17.2.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-17.1.1 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-17.1.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-17.0.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-16.29.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-16.28.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-16.27.1 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-16.27.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-16.26.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-16.25.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-16.24.0 lib/govuk_publishing_components/app_helpers/table_helper.rb
govuk_publishing_components-16.23.0 lib/govuk_publishing_components/app_helpers/table_helper.rb