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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag) ⇒ TableBuilder

Returns a new instance of TableBuilder.



27
28
29
# File 'lib/govuk_publishing_components/app_helpers/table_helper.rb', line 27

def initialize(tag)
  @tag = tag
end

Instance Attribute Details

#tagObject (readonly)

Returns the value of attribute tag.



25
26
27
# File 'lib/govuk_publishing_components/app_helpers/table_helper.rb', line 25

def tag
  @tag
end

Instance Method Details

#bodyObject



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

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

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



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

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



31
32
33
34
35
36
37
# File 'lib/govuk_publishing_components/app_helpers/table_helper.rb', line 31

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



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

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_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



45
46
47
48
49
# File 'lib/govuk_publishing_components/app_helpers/table_helper.rb', line 45

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