Class: GovukPublishingComponents::AppHelpers::TableHelper::TableBuilder

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, ActionView::Helpers::UrlHelper
Defined in:
lib/govuk_publishing_components/app_helpers/table_helper.rb

Constant Summary collapse

ALLOWABLE_COLUMN_WIDTHS =
%w[
  three-quarters
  two-thirds
  one-half
  one-third
  one-quarter
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag) ⇒ TableBuilder

Returns a new instance of TableBuilder.



36
37
38
# File 'lib/govuk_publishing_components/app_helpers/table_helper.rb', line 36

def initialize(tag)
  @tag = tag
end

Instance Attribute Details

#tagObject (readonly)

Returns the value of attribute tag.



34
35
36
# File 'lib/govuk_publishing_components/app_helpers/table_helper.rb', line 34

def tag
  @tag
end

Instance Method Details

#bodyObject



48
49
50
51
52
# File 'lib/govuk_publishing_components/app_helpers/table_helper.rb', line 48

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

#cell(str, opt = {}) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/govuk_publishing_components/app_helpers/table_helper.rb', line 71

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

#headObject



40
41
42
43
44
45
46
# File 'lib/govuk_publishing_components/app_helpers/table_helper.rb', line 40

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

#header(str, opt = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/govuk_publishing_components/app_helpers/table_helper.rb', line 60

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]
  classes << "govuk-!-width-#{opt[:width]}" if ALLOWABLE_COLUMN_WIDTHS.include?(opt[:width])
  link_classes = %w[app-table__sort-link]
  link_classes << "app-table__sort-link--#{opt[:sort_direction]}" if opt[:sort_direction]
  str = link_to str, opt[:href], class: link_classes, data: opt[:data_attributes] if opt[:href]
  tag.th str, class: classes, scope: opt[:scope] || "col"
end

#rowObject



54
55
56
57
58
# File 'lib/govuk_publishing_components/app_helpers/table_helper.rb', line 54

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