no hidden field on disabled setting

pull/10460/head
ulferts 3 years ago
parent 27b05b3a56
commit 43f85a9bfc
No known key found for this signature in database
GPG Key ID: A205708DE1284017
  1. 46
      app/helpers/settings_helper.rb
  2. 29
      spec/helpers/settings_helper_spec.rb

@ -78,19 +78,13 @@ module SettingsHelper
def setting_multiselect(setting, choices, options = {})
setting_label(setting, options) +
content_tag(:span, class: 'form--field-container -vertical') do
hidden_field_tag("settings[#{setting}][]", '') +
choices.map do |choice|
text, value, choice_options = (choice.is_a?(Array) ? choice : [choice, choice])
choice_options = disabled_setting_option(setting)
.merge(choice_options || {})
.merge(options.except(:id))
choice_options[:id] = "#{setting}_#{value}"
content_tag(:label, class: 'form--label-with-check-box') do
styled_check_box_tag("settings[#{setting}][]", value,
Setting.send(setting).include?(value), choice_options) + text.to_s
hidden = with_empty_unless_writable(setting) do
hidden_field_tag("settings[#{setting}][]", '')
end
end
hidden +
choices.map do |choice|
setting_multiselect_choice(setting, choice, options)
end.join.html_safe
end
end
@ -162,7 +156,11 @@ module SettingsHelper
def setting_check_box(setting, options = {})
setting_label(setting, options) +
wrap_field_outer(options) do
tag(:input, type: 'hidden', name: "settings[#{setting}]", value: 0, id: "settings_#{setting}_hidden") +
hidden = with_empty_unless_writable(setting) do
tag(:input, type: 'hidden', name: "settings[#{setting}]", value: 0, id: "settings_#{setting}_hidden")
end
hidden +
styled_check_box_tag("settings[#{setting}]",
1,
Setting.send("#{setting}?"),
@ -236,7 +234,29 @@ module SettingsHelper
end.join.html_safe
end
def setting_multiselect_choice(setting, choice, options)
text, value, choice_options = (choice.is_a?(Array) ? choice : [choice, choice])
choice_options = disabled_setting_option(setting)
.merge(choice_options || {})
.merge(options.except(:id))
choice_options[:id] = "#{setting}_#{value}"
content_tag(:label, class: 'form--label-with-check-box') do
styled_check_box_tag("settings[#{setting}][]", value,
Setting.send(setting).include?(value), choice_options) + text.to_s
end
end
def disabled_setting_option(setting)
{ disabled: !Setting.send(:"#{setting}_writable?") }
end
def with_empty_unless_writable(setting)
if Setting.send(:"#{setting}_writable?")
yield
else
''.html_safe
end
end
end

@ -114,7 +114,8 @@ describe SettingsHelper, type: :helper do
.and_return false
end
it 'is disabled' do
it 'is disabled and has no hidden field' do
expect(output).not_to have_selector 'input[type="hidden"][value="''"]', visible: :all
expect(output).to have_selector 'input[type="checkbox"][disabled="disabled"].form--check-box', count: 3
end
end
@ -275,9 +276,22 @@ important text</textarea>
it_behaves_like 'field disabled if non writable'
it 'outputs element' do
expect(output).to have_selector 'input[type="hidden"][value=0]', visible: :hidden
expect(output).to have_selector 'input[type="checkbox"].custom-class.form--check-box'
expect(output).to have_checked_field 'settings_field'
end
context 'when the setting isn`t writable' do
before do
allow(Setting)
.to receive(:field_writable?)
.and_return false
end
it 'does not output a hidden field' do
expect(output).not_to have_selector 'input[type="hidden"][value=0]', visible: :hidden
end
end
end
context 'when setting is false' do
@ -291,9 +305,22 @@ important text</textarea>
it_behaves_like 'field disabled if non writable'
it 'outputs element' do
expect(output).to have_selector 'input[type="hidden"][value=0]', visible: :hidden
expect(output).to have_selector 'input[type="checkbox"].custom-class.form--check-box'
expect(output).to have_unchecked_field 'settings_field'
end
context 'when the setting isn`t writable' do
before do
allow(Setting)
.to receive(:field_writable?)
.and_return false
end
it 'does not output a hidden field' do
expect(output).not_to have_selector 'input[type="hidden"][value=0]', visible: :hidden
end
end
end
end

Loading…
Cancel
Save