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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
no hidden field on disabled setting
ulferts43f85a9bfc00
diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rbindex acf0a3dd330..682b7d888d5 100644--- a/app/helpers/settings_helper.rb+++ b/app/helpers/settings_helper.rb@@ -78,19 +78,13 @@ module SettingsHelperdef 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_safeendend@@ -162,7 +156,11 @@ module SettingsHelperdef 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 SettingsHelperend.join.html_safeend+ 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+ endenddiff --git a/spec/helpers/settings_helper_spec.rb b/spec/helpers/settings_helper_spec.rbindex 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 falseend- it 'is disabled' do+ it 'is disabled and has no hidden field' do+ expect(output).not_to have_selector 'input[type="hidden"][value="''"]', visible: :allexpect(output).to have_selector 'input[type="checkbox"][disabled="disabled"].form--check-box', count: 3endend@@ -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: :hiddenexpect(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+ endendcontext '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: :hiddenexpect(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+ endendend