govuk-design-system-rails 0.10.1 → 0.10.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,33 +7,32 @@
7
7
  <% radios_html = capture do %>
8
8
  <% if local_assigns[:hint] %>
9
9
  <%
10
- hint_id = id_prefix + '-hint'
11
- described_by = described_by.present? ? described_by + ' ' + hint_id : hint_id
10
+ hint_id = "#{id_prefix}-hint"
11
+ described_by = described_by.present? ? "#{described_by} #{hint_id}" : hint_id
12
12
  hint_classes = class_names(
13
13
  "govuk-hint",
14
14
  local_assigns[:hint][:classes]
15
15
  )
16
16
  %>
17
17
  <%= govukHint({
18
- id: hint_id,
19
- classes: hint_classes,
20
- attributes: local_assigns[:hint][:attributes],
21
- html: local_assigns[:hint][:html],
22
- text: local_assigns[:hint][:text]
23
- }) %>
18
+ id: hint_id,
19
+ classes: hint_classes,
20
+ attributes: local_assigns[:hint][:attributes],
21
+ html: local_assigns[:hint][:html],
22
+ text: local_assigns[:hint][:text]
23
+ }) %>
24
24
  <% end %>
25
25
 
26
26
  <% radio_classes = class_names(
27
- "govuk-radios",
28
- { "govuk-radios--conditional" => is_conditional },
29
- local_assigns[:classes]
30
- )
31
- %>
27
+ "govuk-radios",
28
+ { "govuk-radios--conditional" => is_conditional },
29
+ local_assigns[:classes]
30
+ ) %>
32
31
  <% if local_assigns[:errorMessage] %>
33
32
  <%
34
- error_id = id_prefix + '-error'
33
+ error_id = "#{id_prefix}-error"
35
34
  described_by = described_by.blank? ? error_id : "#{described_by} #{error_id}"
36
- error_attributes = (local_assigns[:errorMessage][:attributes] || {}).merge(id: error_id)
35
+ error_attributes = (local_assigns[:errorMessage][:attributes] || {}).merge(id: error_id)
37
36
  %>
38
37
  <%=
39
38
  govukErrorMessage({
@@ -47,15 +46,15 @@
47
46
  %>
48
47
  <% end %>
49
48
  <%
50
- radio_attributes = {class: radio_classes}.merge!(local_assigns[:attributes] || {})
51
- .merge!({"data-module" => "govuk-radios"})
49
+ radio_attributes = { class: radio_classes }.merge!(local_assigns[:attributes] || {})
50
+ .merge!({ "data-module" => "govuk-radios" })
52
51
  %>
53
52
 
54
53
  <%= tag.div(**radio_attributes) do %>
55
54
  <% local_assigns[:items].each_with_index do |item, index| %>
56
55
  <%
57
56
  id = item[:id] || "#{id_prefix}-#{index + 1}"
58
- conditional_id = 'conditional-' + id
57
+ conditional_id = "conditional-#{id}"
59
58
  %>
60
59
  <% if item[:divider] %>
61
60
  <div class="govuk-radios__divider">
@@ -63,7 +62,7 @@
63
62
  <% else %>
64
63
  <%
65
64
  has_hint = item[:hint] && (item[:hint][:text] || item[:hint][:html])
66
- item_hint_id = id + '-item-hint'
65
+ item_hint_id = "#{id}-item-hint"
67
66
  disabled = item
68
67
  is_checked = item[:checked] || (!item[:value].nil? && item[:value] == local_assigns[:value])
69
68
  input_attributes = {
@@ -74,31 +73,31 @@
74
73
  class: "govuk-radios__input"
75
74
  }
76
75
 
77
- input_attributes.merge!({disabled: "disabled"}) if item[:disabled]
78
- input_attributes.merge!({checked: "checked"}) if is_checked
79
- input_attributes.merge!({"data-aria-controls" => conditional_id}) if item[:conditional]
80
- input_attributes.merge!({"aria-describedby" => item_hint_id}) if has_hint
76
+ input_attributes[:disabled] = "disabled" if item[:disabled]
77
+ input_attributes[:checked] = "checked" if is_checked
78
+ input_attributes["data-aria-controls"] = conditional_id if item[:conditional]
79
+ input_attributes["aria-describedby"] = item_hint_id if has_hint
81
80
  %>
82
81
  <%
83
82
  label_classes = class_names(
84
- 'govuk-radios__label',
85
- { item[:label].try(:[], :classes) => item[:label] }
83
+ "govuk-radios__label",
84
+ { item[:label].try(:[], :classes) => item[:label] }
86
85
  )
87
86
  %>
88
87
  <div class="govuk-radios__item">
89
88
  <%= tag.input(**input_attributes) %>
90
89
  <%=
91
90
  govukLabel({
92
- html: item[:html],
93
- text: item[:text],
94
- classes: label_classes,
95
- attributes: item[:label].try(:[], :attributes),
96
- for: id
91
+ html: item[:html],
92
+ text: item[:text],
93
+ classes: label_classes,
94
+ attributes: item[:label].try(:[], :attributes),
95
+ for: id
97
96
  })
98
97
  %>
99
98
 
100
99
  <% if has_hint %>
101
- <% item_hint_classes = class_names('govuk-radios__hint', item.dig(:hint, :classes)) %>
100
+ <% item_hint_classes = class_names("govuk-radios__hint", item.dig(:hint, :classes)) %>
102
101
  <%=
103
102
  govukHint({
104
103
  id: item_hint_id,
@@ -114,9 +113,9 @@
114
113
 
115
114
  <% if item[:conditional] %>
116
115
  <% conditional_classes = class_names(
117
- "govuk-radios__conditional",
118
- "govuk-radios__conditional--hidden" => !is_checked
119
- ) %>
116
+ "govuk-radios__conditional",
117
+ "govuk-radios__conditional--hidden" => !is_checked
118
+ ) %>
120
119
 
121
120
  <%= tag.div(class: conditional_classes, id: conditional_id) do %>
122
121
  <%= item[:conditional][:html] %>
@@ -127,10 +126,10 @@
127
126
  <% end %>
128
127
 
129
128
  <% form_group_classes = class_names(
130
- "govuk-form-group",
131
- local_assigns.dig(:formGroup, :classes),
132
- "govuk-form-group--error" => local_assigns[:errorMessage]
133
- ) %>
129
+ "govuk-form-group",
130
+ local_assigns.dig(:formGroup, :classes),
131
+ "govuk-form-group--error" => local_assigns[:errorMessage]
132
+ ) %>
134
133
 
135
134
  <%= tag.div(class: form_group_classes) do %>
136
135
  <% if local_assigns[:fieldset].present? %>
@@ -28,32 +28,33 @@
28
28
  text: local_assigns[:label].try(:[], :text),
29
29
  classes: local_assigns[:label].try(:[], :classes),
30
30
  attributes: local_assigns[:label].try(:[], :attributes),
31
- for: local_assigns[:id])
31
+ for: local_assigns[:id]
32
+ )
32
33
  %>
33
34
 
34
35
  <% if local_assigns[:hint] %>
35
36
  <%
36
- hint_id = local_assigns.try(:[], :name) + '-hint'
37
- described_by = described_by.present? ? described_by + ' ' + hint_id : hint_id
37
+ hint_id = "#{local_assigns.try(:[], :name)}-hint"
38
+ described_by = described_by.present? ? "#{described_by} #{hint_id}" : hint_id
38
39
  hint_classes = class_names(
39
40
  "govuk-hint",
40
41
  local_assigns[:hint][:classes]
41
42
  )
42
43
  %>
43
44
  <%= govukHint({
44
- id: hint_id,
45
- classes: hint_classes,
46
- attributes: local_assigns[:hint][:attributes],
47
- html: local_assigns[:hint][:html],
48
- text: local_assigns[:hint][:text]
49
- }) %>
45
+ id: hint_id,
46
+ classes: hint_classes,
47
+ attributes: local_assigns[:hint][:attributes],
48
+ html: local_assigns[:hint][:html],
49
+ text: local_assigns[:hint][:text]
50
+ }) %>
50
51
  <% end %>
51
52
 
52
53
  <% if local_assigns[:errorMessage] %>
53
54
  <%
54
- error_id = local_assigns.try(:[], :name) + '-error'
55
+ error_id = "#{local_assigns.try(:[], :name)}-error"
55
56
  described_by = described_by.blank? ? error_id : "#{described_by} #{error_id}"
56
- error_attributes = (local_assigns[:errorMessage][:attributes] || {}).merge(id: error_id)
57
+ error_attributes = (local_assigns[:errorMessage][:attributes] || {}).merge(id: error_id)
57
58
  %>
58
59
  <%=
59
60
  govukErrorMessage({
@@ -95,9 +96,11 @@
95
96
  </svg>
96
97
  </button>
97
98
  <% end %>
98
- <%= javascript_tag nonce: true do -%>
99
- window.callAutocompleteWhenReady("<%= local_assigns[:id] %>", {showAllValues: "<%= local_assigns[:show_all_values] %>"});
100
- <% end -%>
99
+ <script nonce="true">
100
+ //<![CDATA[
101
+ window.callAutocompleteWhenReady(<%= local_assigns[:id].to_json %>, { showAllValues: <%= local_assigns[:show_all_values].to_json %> });
102
+ //]]>
103
+ </script>
101
104
  <% else %>
102
105
  <%= select %>
103
106
  <% end %>
@@ -4,7 +4,7 @@
4
4
  class: class_names("govuk-summary-list", local_assigns[:classes]),
5
5
  }.merge!(local_assigns[:attributes] || {})
6
6
  %>
7
- <%= tag.dl **dl_attributes do %>
7
+ <%= tag.dl(**dl_attributes) do %>
8
8
  <% local_assigns[:rows].each do |row| %>
9
9
  <% if row %>
10
10
  <%
@@ -5,7 +5,7 @@
5
5
  data: { module: "govuk-tabs" }
6
6
  }.merge!(local_assigns[:attributes] || {})
7
7
  %>
8
- <%= tag.div **attributes do %>
8
+ <%= tag.div(**attributes) do %>
9
9
  <h2 class="govuk-tabs__title">
10
10
  <%= local_assigns[:title] || "Contents" %>
11
11
  </h2>
@@ -14,7 +14,7 @@
14
14
  <% items.each_with_index do |item, i| %>
15
15
  <% if item %>
16
16
  <%
17
- item_id = item[:id].presence || ([id_prefix, i].join("-"))
17
+ item_id = item[:id].presence || [id_prefix, i].join("-")
18
18
  item_classes = class_names("govuk-tabs__list-item", { "govuk-tabs__list-item--selected" => i.zero? })
19
19
  link_attributes = item[:attributes] || {}
20
20
  link_attributes[:class] = class_names("govuk-tabs__tab", link_attributes[:class])
@@ -28,7 +28,7 @@
28
28
  <% items.each_with_index do |item, i| %>
29
29
  <% if item %>
30
30
  <%
31
- item_id = item[:id].presence || ([id_prefix, i].join("-"))
31
+ item_id = item[:id].presence || [id_prefix, i].join("-")
32
32
  panel_attributes = {
33
33
  class: class_names("govuk-tabs__panel", { "govuk-tabs__panel--hidden" => i.positive? })
34
34
  }.merge!(item.dig(:panel, :attributes) || {})
@@ -9,48 +9,47 @@
9
9
  %>
10
10
 
11
11
  <% text_area_class = class_names(
12
- "govuk-textarea",
13
- { "govuk-textarea--error" => local_assigns[:errorMessage] },
14
- local_assigns[:classes]
15
- )
16
- %>
12
+ "govuk-textarea",
13
+ { "govuk-textarea--error" => local_assigns[:errorMessage] },
14
+ local_assigns[:classes]
15
+ ) %>
17
16
 
18
17
  <%= tag.div(class: form_classes) do %>
19
18
  <%=
20
19
  govukLabel({
21
- html: local_assigns[:label][:html],
22
- text: local_assigns[:label][:text],
23
- classes: local_assigns[:label][:classes],
24
- isPageHeading: local_assigns[:label][:isPageHeading],
25
- attributes: local_assigns[:label][:attributes],
26
- for: local_assigns[:id]
20
+ html: local_assigns[:label][:html],
21
+ text: local_assigns[:label][:text],
22
+ classes: local_assigns[:label][:classes],
23
+ isPageHeading: local_assigns[:label][:isPageHeading],
24
+ attributes: local_assigns[:label][:attributes],
25
+ for: local_assigns[:id]
27
26
  })
28
27
  %>
29
28
 
30
29
  <% if local_assigns[:hint] %>
31
- <% hintId = local_assigns[:id].to_s + '-hint' %>
32
- <% described_by = described_by.blank? ? hintId : described_by + ' ' + hintId %>
30
+ <% hint_id = "#{local_assigns[:id]}-hint" %>
31
+ <% described_by = described_by.blank? ? hint_id : "#{described_by} #{hint_id}" %>
33
32
  <%= govukHint({
34
- id: hintId,
35
- classes: local_assigns[:hint][:classes],
36
- attributes: local_assigns[:hint][:attributes],
37
- html: local_assigns[:hint][:html],
38
- text: local_assigns[:hint][:text]
39
- }) %>
33
+ id: hint_id,
34
+ classes: local_assigns[:hint][:classes],
35
+ attributes: local_assigns[:hint][:attributes],
36
+ html: local_assigns[:hint][:html],
37
+ text: local_assigns[:hint][:text]
38
+ }) %>
40
39
  <% end %>
41
40
 
42
41
  <% if local_assigns[:errorMessage] %>
43
- <% errorId = local_assigns[:id].to_s + '-error' %>
44
- <% described_by = described_by.blank? ? errorId : described_by + ' ' + errorId %>
42
+ <% error_id = "#{local_assigns[:id]}-error" %>
43
+ <% described_by = described_by.blank? ? error_id : "#{described_by} #{error_id}" %>
45
44
  <% attributes = local_assigns[:errorMessage][:attributes] || {} %>
46
45
  <%= govukErrorMessage({
47
- id: errorId,
48
- classes: local_assigns[:errorMessage][:classes],
49
- attributes: attributes.merge({id: errorId}),
50
- html: local_assigns[:errorMessage][:html],
51
- text: local_assigns[:errorMessage][:text],
52
- visuallyHiddenText: local_assigns[:errorMessage][:visuallyHiddenText]
53
- }) %>
46
+ id: error_id,
47
+ classes: local_assigns[:errorMessage][:classes],
48
+ attributes: attributes.merge({ id: error_id }),
49
+ html: local_assigns[:errorMessage][:html],
50
+ text: local_assigns[:errorMessage][:text],
51
+ visuallyHiddenText: local_assigns[:errorMessage][:visuallyHiddenText]
52
+ }) %>
54
53
  <% end %>
55
54
 
56
55
  <%
@@ -7,24 +7,24 @@
7
7
  month_errors = errors.full_messages_for("#{key}_month".to_sym)
8
8
  year_errors = errors.full_messages_for("#{key}_year".to_sym)
9
9
  local_assigns[:items] = [
10
- {
11
- label: "day",
12
- classes: "govuk-input--width-2 #{'govuk-input--error' if day_errors.any? || date_errors.any?}",
13
- value: form.object.send(:get_day, key),
14
- name: "#{get_attribute_name(form, key)}[day]"
15
- },
16
- {
17
- label: "month",
18
- classes: "govuk-input--width-2 #{'govuk-input--error' if month_errors.any? || date_errors.any?}",
19
- value: form.object.send(:get_month, key),
20
- name: "#{get_attribute_name(form, key)}[month]"
21
- },
22
- {
23
- label: "year",
24
- classes: "govuk-input--width-4 #{'govuk-input--error' if year_errors.any? || date_errors.any?}",
25
- value: form.object.send(:get_year, key),
26
- name: "#{get_attribute_name(form, key)}[year]"
27
- }
10
+ {
11
+ label: "day",
12
+ classes: "govuk-input--width-2 #{'govuk-input--error' if day_errors.any? || date_errors.any?}",
13
+ value: form.object.send(:get_day, key),
14
+ name: "#{get_attribute_name(form, key)}[day]"
15
+ },
16
+ {
17
+ label: "month",
18
+ classes: "govuk-input--width-2 #{'govuk-input--error' if month_errors.any? || date_errors.any?}",
19
+ value: form.object.send(:get_month, key),
20
+ name: "#{get_attribute_name(form, key)}[month]"
21
+ },
22
+ {
23
+ label: "year",
24
+ classes: "govuk-input--width-4 #{'govuk-input--error' if year_errors.any? || date_errors.any?}",
25
+ value: form.object.send(:get_year, key),
26
+ name: "#{get_attribute_name(form, key)}[year]"
27
+ }
28
28
  ]
29
29
  error_text = errors.full_messages_for(key).first.presence
30
30
  local_assigns[:errorMessage] ||= { text: error_text } if error_text
@@ -2,9 +2,9 @@
2
2
  # Infer name and id_prefix from attribute key
3
3
  local_assigns[:name] ||= get_attribute_name(form, key)
4
4
  local_assigns[:idPrefix] ||= get_attribute_id_prefix(form, key)
5
- #Give items ids based on rails' convention
5
+ # Give items ids based on rails' convention
6
6
  local_assigns[:items].each do |item|
7
- #sanitization taken from https://github.com/rails/rails/
7
+ # sanitization taken from https://github.com/rails/rails/
8
8
  # blob/7caea98e189c02721b2e944a074c405b033852eb/actionview/lib/action_view/helpers/tags/base.rb#L140
9
9
  value = item[:value].to_s.gsub(/\s/, "_").gsub(/[^-[[:word:]]]/, "").mb_chars.downcase.to_s
10
10
  item[:id] ||= "#{local_assigns[:idPrefix]}_#{value}"
@@ -10,8 +10,8 @@
10
10
  if local_assigns[:selected]
11
11
  item_matching_selected = local_assigns[:items].find { |item| item[:value].to_s == local_assigns[:selected] }
12
12
  item_matching_selected[:selected] = true if item_matching_selected
13
- elsif form.object&.respond_to?(key)
14
- # Make item selected based on attribute value
13
+ elsif form.object.respond_to?(key)
14
+ # Make item selected based on attribute value
15
15
  item_matching_state = local_assigns[:items].find { |item| item[:value].to_s == form.object.send(key).to_s }
16
16
  item_matching_state[:selected] = true if item_matching_state
17
17
  end
@@ -0,0 +1,32 @@
1
+ $LOAD_PATH.push File.expand_path("lib", __dir__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "govuk-design-system-rails"
5
+ s.version = "0.10.2"
6
+ s.authors = %w[govuk-ruby]
7
+ s.summary = "An implementation of the govuk-frontend macros in Ruby on Rails"
8
+ s.homepage = "https://github.com/govuk-ruby/govuk-design-system-rails"
9
+ s.license = "MIT"
10
+ s.metadata = {
11
+ "bug_tracker_uri" => "https://github.com/govuk-ruby/govuk-design-system-rails/issues",
12
+ "changelog_uri" => "https://github.com/govuk-ruby/govuk-design-system-rails/releases",
13
+ "homepage_uri" => "https://github.com/govuk-ruby/govuk-design-system-rails",
14
+ "source_code_uri" => "https://github.com/govuk-ruby/govuk-design-system-rails",
15
+ "wiki_uri" => "https://github.com/govuk-ruby/govuk-design-system-rails/wiki"
16
+ }
17
+ s.files = %w[govuk-design-system-rails.gemspec README.md LICENSE] + Dir["{app,lib}/**/*"]
18
+ s.test_files = Dir["spec/**/*"]
19
+ s.required_ruby_version = ">= 2.7.0"
20
+
21
+ s.add_development_dependency "compare-xml", "~> 0.66"
22
+ s.add_development_dependency "erb_lint", "~> 0.3.1"
23
+ s.add_development_dependency "nokogiri", "~> 1.14.2"
24
+ s.add_development_dependency "rails", "~> 6.1.6", ">= 6.1.6.1"
25
+ s.add_development_dependency "rspec-rails", "~> 6.0.1"
26
+ s.add_development_dependency "rubocop", "~> 1.44.1"
27
+ s.add_development_dependency "rubocop-govuk", "~> 4.10.0"
28
+ s.add_development_dependency "rubocop-performance", "~> 1.16.0"
29
+ s.add_development_dependency "rubocop-rspec", "~> 2.18.1"
30
+ s.add_development_dependency "sqlite3", "~> 1.6.1"
31
+ s.add_development_dependency "super_diff", "~> 0.9.0"
32
+ end
@@ -142,5 +142,76 @@ RSpec.describe GovukDesignSystem::CheckboxesHelper, type: :helper do
142
142
  </div>
143
143
  HTML
144
144
  end
145
+
146
+ it "returns the correct HTML when there is a fieldset css class" do
147
+ html = helper.govukCheckboxes({
148
+ idPrefix: "waste",
149
+ name: "waste",
150
+ fieldset: {
151
+ legend: {
152
+ text: "Which types of waste do you transport?",
153
+ isPageHeading: true,
154
+ classes: "govuk-fieldset__legend--l"
155
+ },
156
+ classes: "js-mutually-exclusive"
157
+ },
158
+ hint: {
159
+ text: "Select all that apply."
160
+ },
161
+ items: [
162
+ {
163
+ value: "carcasses",
164
+ text: "Waste from animal carcasses",
165
+ disable_ghost: true
166
+ },
167
+ {
168
+ value: "mines",
169
+ text: "Waste from mines or quarries",
170
+ disable_ghost: true
171
+ },
172
+ {
173
+ value: "farm",
174
+ text: "Farm or agricultural waste",
175
+ disable_ghost: true
176
+ }
177
+ ]
178
+ })
179
+
180
+ expect(html).to match_html(<<~HTML)
181
+ <div class="govuk-form-group">
182
+ <fieldset class="govuk-fieldset js-mutually-exclusive" aria-describedby="waste-hint">
183
+ <legend class="govuk-fieldset__legend govuk-fieldset__legend--l">
184
+ <h1 class="govuk-fieldset__heading">
185
+ Which types of waste do you transport?
186
+ </h1>
187
+ </legend>
188
+ <div id="waste-hint" class="govuk-hint">
189
+ Select all that apply.
190
+ </div>
191
+ <div class="govuk-checkboxes" data-module="govuk-checkboxes">
192
+ <div class="govuk-checkboxes__item">
193
+ <input class="govuk-checkboxes__input" id="waste" name="waste" type="checkbox" value="carcasses">
194
+ <label class="govuk-label govuk-checkboxes__label" for="waste">
195
+ Waste from animal carcasses
196
+ </label>
197
+ </div>
198
+ <div class="govuk-checkboxes__item">
199
+ <input class="govuk-checkboxes__input" id="waste-2" name="waste" type="checkbox" value="mines">
200
+ <label class="govuk-label govuk-checkboxes__label" for="waste-2">
201
+ Waste from mines or quarries
202
+ </label>
203
+ </div>
204
+ <div class="govuk-checkboxes__item">
205
+ <input class="govuk-checkboxes__input" id="waste-3" name="waste" type="checkbox" value="farm">
206
+ <label class="govuk-label govuk-checkboxes__label" for="waste-3">
207
+ Farm or agricultural waste
208
+ </label>
209
+ </div>
210
+ </div>
211
+
212
+ </fieldset>
213
+ </div>
214
+ HTML
215
+ end
145
216
  end
146
217
  end