Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

no hidden field on disabled setting

ulferts43f85a9bfc00
diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb
index acf0a3dd330..682b7d888d5 100644
--- a/app/helpers/settings_helper.rb
+++ b/app/helpers/settings_helper.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
diff --git a/spec/helpers/settings_helper_spec.rb b/spec/helpers/settings_helper_spec.rb
index 9b2ad9967f4..61230bf4e17 100644
--- a/spec/helpers/settings_helper_spec.rb
+++ b/spec/helpers/settings_helper_spec.rb
@@ -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