Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We shouldn't split value strings, since deeper_merge will remove repeated words #279

Merged
merged 2 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions lib/govuk_design_system_formbuilder/traits/html_attributes.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
module GOVUKDesignSystemFormBuilder
module Traits
module HTMLAttributes
# Attributes eases working with default and custom attributes by
# Attributes eases working with default and custom attributes by:
# * deeply merging them so both the default (required) attributes are
# present
# * joins the arrays into strings to maintain Rails 6.0.3 compatibility
class Attributes
# Don't try to deep merge these fields, when we remove duplicates
# whilst merging the resulting values later, repeating words are lost
SKIP = [
%i(id),
%i(value),
%i(title),
%i(alt),
%i(href),
%i(aria label)
].freeze

def initialize(defaults, custom)
@merged = defaults.deeper_merge(deep_split_values(custom))
end
Expand All @@ -16,19 +27,27 @@ def to_h

private

def deep_split_values(hash)
def deep_split_values(hash, parent = nil)
hash.each.with_object({}) do |(key, value), result|
result[key] = case value
when Hash
deep_split_values(value)
deep_split_values(value, key)
when String
value.split
split_list_values(key, value, parent)
else
value
end
end
end

def split_list_values(key, value, parent = nil)
if [parent, key].compact.in?(SKIP)
value
else
value.split
end
end

def deep_join_values(hash)
hash.each.with_object({}) do |(key, value), result|
result[key] = case value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

it_behaves_like 'a field that allows extra HTML attributes to be set' do
let(:described_element) { 'input' }
let(:value) { 'Montgomery Montgomery' }
let(:expected_class) { 'govuk-checkboxes__input' }
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

it_behaves_like 'a field that allows extra HTML attributes to be set' do
let(:described_element) { 'input' }
let(:value) { 'Montgomery Montgomery' }
let(:expected_class) { 'govuk-radios__input' }
end

Expand Down
30 changes: 24 additions & 6 deletions spec/support/shared/shared_html_attribute_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@

context 'with args in the expected formats' do
custom_attributes = {
required: { provided: true, expected: 'required' },
autocomplete: { provided: false, expected: 'false' },
placeholder: { provided: 'Seymour Skinner', expected: 'Seymour Skinner' },
data: { provided: { a: 'b' }, expected: { 'data-a' => 'b' } },
aria: { provided: { c: 'd' }, expected: { 'aria-c' => 'd' } },
class: { provided: %w(red spots), expected: %w(red spots) }
required: { provided: true, expected: 'required' },
autocomplete: { provided: false, expected: 'false' },
placeholder: { provided: 'Seymour Skinner', expected: 'Seymour Skinner' },
data: { provided: { a: 'b' }, expected: { 'data-a' => 'b' } },
class: { provided: %w(red spots), expected: %w(red spots) },
value: { provided: 'Montgomery Montgomery', expected: 'Montgomery Montgomery' },

# aria-label is a special case, along with value it should not be treated
# like a list when deep merging
aria: {
provided: {
c: 'd',
label: 'Burns Burns'
},
expected: { 'aria-c' => 'd', 'aria-label' => 'Burns Burns' }
},
}

let(:custom_attributes) { custom_attributes }
Expand All @@ -22,6 +32,14 @@
describe 'input tag should have the extra attributes:' do
extract_args(custom_attributes, :expected).each do |key, val|
case
when key == :value
specify "#{key} has classes #{val}" do
if described_element == "textarea"
expect(subject).to have_tag(described_element, text: /#{val}/)
else
expect(subject).to have_tag(described_element, with: { key => val })
end
end

# class is dealt with as a special case because we want to ensure that whatever
# extra classes we provide are *added* to the pre-existing one (expected_class)
Expand Down