Module: DesignSystemHelper

Includes:
ActionView::Helpers::FormHelper
Included in:
DesignSystem::Generic::SummaryCardComponent, DesignSystem::Govuk::TestHelpers::FormBuilderTestable
Defined in:
app/helpers/design_system_helper.rb

Overview

The helpers for the design system

Instance Method Summary collapse

Instance Method Details

#brandObject



7
8
9
# File 'app/helpers/design_system_helper.rb', line 7

def brand
  controller.send(:brand)
end


139
140
141
# File 'app/helpers/design_system_helper.rb', line 139

def ds_action_link(name = nil, options = nil, html_options = nil)
  render DesignSystem::Registry.component(brand, :action_link).new(name, options, html_options)
end

#ds_alert(message = nil, &block) ⇒ Object



86
87
88
89
# File 'app/helpers/design_system_helper.rb', line 86

def ds_alert(message = nil, &block)
  component = DesignSystem::Registry.component(brand, :alert).new(message)
  render(component) { block ? capture(&block) : nil }
end

#ds_button_tag(content_or_options = nil, options = nil, &block) ⇒ Object



65
66
67
68
# File 'app/helpers/design_system_helper.rb', line 65

def ds_button_tag(content_or_options = nil, options = nil, &block)
  component = DesignSystem::Registry.component(brand, :button).new(content_or_options, options)
  render(component) { block ? capture(&block) : nil }
end

#ds_callout(label, body) ⇒ Object



128
129
130
# File 'app/helpers/design_system_helper.rb', line 128

def ds_callout(label, body)
  render DesignSystem::Registry.component(brand, :callout).new(label:, body:)
end

#ds_code(code, language) ⇒ Object



174
175
176
# File 'app/helpers/design_system_helper.rb', line 174

def ds_code(code, language)
  DesignSystem::Registry.builder(brand, 'code', self).render_code(code, language)
end

#ds_details(summary_text, &block) ⇒ Object

Raises:

  • (ArgumentError)


132
133
134
135
136
137
# File 'app/helpers/design_system_helper.rb', line 132

def ds_details(summary_text, &block)
  raise ArgumentError unless block_given?

  component = DesignSystem::Registry.component(brand, :details).new(summary_text)
  render(component) { capture(&block) }
end

#ds_fixed_elementsObject

This method provides access to the current design system adapter



12
13
14
15
16
17
18
19
20
21
22
# File 'app/helpers/design_system_helper.rb', line 12

def ds_fixed_elements
  instance = DesignSystem::Registry.builder(brand, 'fixed_elements', self)

  if block_given?
    yield instance

    instance.render
  else
    instance
  end
end

#ds_form_builderObject



24
25
26
# File 'app/helpers/design_system_helper.rb', line 24

def ds_form_builder
  DesignSystem::Registry.form_builder(brand)
end

#ds_form_with(model: nil, scope: nil, url: nil, format: nil, **options) ⇒ Object



28
29
30
# File 'app/helpers/design_system_helper.rb', line 28

def ds_form_with(model: nil, scope: nil, url: nil, format: nil, **options, &)
  form_with(model:, scope:, url:, format:, builder: ds_form_builder, **options, &)
end

#ds_grid(options = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


143
144
145
146
147
148
149
# File 'app/helpers/design_system_helper.rb', line 143

def ds_grid(options = {}, &block)
  raise ArgumentError unless block_given?

  grid = ::DesignSystem::Components::Grid.new
  block.call(grid)
  render ::DesignSystem::Registry.component(brand, :grid).new(grid:, **options)
end

#ds_heading(text, level: 2, **options) ⇒ Object



100
101
102
# File 'app/helpers/design_system_helper.rb', line 100

def ds_heading(text, level: 2, **options)
  render DesignSystem::Registry.component(brand, :heading).new(text, level:, **options)
end

#ds_inset_text(text = nil, **options, &block) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'app/helpers/design_system_helper.rb', line 164

def ds_inset_text(text = nil, **options, &block)
  component = DesignSystem::Registry.component(brand, :inset_text).new(text:, **options)
  output = if block
             render(component) { capture(&block) }
           else
             render(component)
           end
  output.presence
end


70
71
72
73
74
# File 'app/helpers/design_system_helper.rb', line 70

def ds_link_to(name = nil, options = nil, html_options = nil, &block)
  klass = DesignSystem::Registry.component(brand, :link)
  component = klass.new(name, options, html_options, link_context: @link_context)
  render(component) { block ? capture(&block) : nil }
end

#ds_list(type: :default, **options, &block) ⇒ Object

Raises:

  • (ArgumentError)


156
157
158
159
160
161
162
# File 'app/helpers/design_system_helper.rb', line 156

def ds_list(type: :default, **options, &block)
  raise ArgumentError, 'block required' unless block_given?

  list_data = ::DesignSystem::Components::List.new
  block.call(list_data)
  render ::DesignSystem::Registry.component(brand, :list).new(list: list_data, type:, **options)
end

#ds_notice(message = nil, type: :information, content_heading: { text: nil, tag: :h3 }, &block) ⇒ Object



91
92
93
94
95
96
97
98
# File 'app/helpers/design_system_helper.rb', line 91

def ds_notice(message = nil, type: :information, content_heading: { text: nil, tag: :h3 }, &block)
  previous_link_context = @link_context
  @link_context = :notification_banner
  component = DesignSystem::Registry.component(brand, :notification).new(message:, type:, content_heading:)
  render(component) { block ? capture(&block) : nil }
ensure
  @link_context = previous_link_context
end

#ds_pagination(collection = nil, options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'app/helpers/design_system_helper.rb', line 76

def ds_pagination(collection = nil, options = {})
  defaults = {
    renderer: DesignSystem::Registry.builder(brand, 'pagination_renderer', self),
    previous_label: '« Previous',
    next_label: 'Next »'
  }

  will_paginate(collection, defaults.merge!(options))
end

#ds_panel(title, body) ⇒ Object



119
120
121
# File 'app/helpers/design_system_helper.rb', line 119

def ds_panel(title, body)
  render DesignSystem::Registry.component(brand, :panel).new(title:, body:)
end

#ds_paragraph(text = nil, size: nil, **options, &block) ⇒ Object



151
152
153
154
# File 'app/helpers/design_system_helper.rb', line 151

def ds_paragraph(text = nil, size: nil, **options, &block)
  component = DesignSystem::Registry.component(brand, :paragraph).new(text:, size:, **options)
  render(component) { block ? capture(&block) : nil }
end

#ds_render_template(design_system_layout = 'application') ⇒ Object



32
33
34
35
# File 'app/helpers/design_system_helper.rb', line 32

def ds_render_template(design_system_layout = 'application')
  @design_system_layout = design_system_layout
  render(template: "layouts/#{brand}/#{design_system_layout}")
end

#ds_start_button(text, href = '#', options = {}) ⇒ Object



61
62
63
# File 'app/helpers/design_system_helper.rb', line 61

def ds_start_button(text, href = '#', options = {})
  render DesignSystem::Registry.component(brand, :start_button).new(text, href, options)
end

#ds_summary_card(title:, actions: [], &block) ⇒ Object



123
124
125
126
# File 'app/helpers/design_system_helper.rb', line 123

def ds_summary_card(title:, actions: [], &block)
  component = DesignSystem::Registry.component(brand, :summary_card).new(title:, actions:)
  render(component) { block ? capture(&block) : nil }
end

#ds_summary_list(&block) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
# File 'app/helpers/design_system_helper.rb', line 45

def ds_summary_list(&block)
  raise ArgumentError unless block_given?

  summary_list = ::DesignSystem::Components::SummaryList.new
  block.call(summary_list)
  render ::DesignSystem::Registry.component(brand, :summary_list).new(summary_list:)
end

#ds_tab(&block) ⇒ Object

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
# File 'app/helpers/design_system_helper.rb', line 53

def ds_tab(&block)
  raise ArgumentError unless block_given?

  tab = ::DesignSystem::Components::Tab.new(self)
  block.call(tab)
  render ::DesignSystem::Registry.component(brand, :tab).new(tab:)
end

#ds_table(options = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
# File 'app/helpers/design_system_helper.rb', line 37

def ds_table(options = {}, &block)
  raise ArgumentError unless block_given?

  table = ::DesignSystem::Components::Table.new
  block.call(table)
  render ::DesignSystem::Registry.component(brand, :table).new(table:, **options)
end

#ds_timeago(date, refresh_interval: 60_000, format: :long) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/helpers/design_system_helper.rb', line 104

def ds_timeago(date, refresh_interval: 60_000, format: :long)
  return if date.blank?

  content = I18n.l(date, format:)

  tag.time(content,
           title: content,
           data: {
             controller: 'timeago',
             timeago_datetime_value: date.iso8601,
             timeago_refresh_interval_value: refresh_interval,
             timeago_add_suffix_value: true
           })
end