Commit c2736823 authored by peterhegman's avatar peterhegman

Refactor cascading settings HAML partials

Add a partial for fieldsets and refactor existing partials to be more
flexible
parent 71910be8
......@@ -83,6 +83,15 @@ module NamespacesHelper
}
end
def cascading_namespace_setting_locked?(attribute, group, **args)
return false if group.nil?
method_name = "#{attribute}_locked?"
return false unless group.namespace_settings.respond_to?(method_name)
group.namespace_settings.public_send(method_name, **args) # rubocop:disable GitlabSecurity/PublicSend
end
private
# Many importers create a temporary Group, so use the real
......
- attribute = local_assigns.fetch(:attribute, nil)
- group = local_assigns.fetch(:group, nil)
- form = local_assigns.fetch(:form, nil)
- setting_locked = local_assigns.fetch(:setting_locked, false)
- help_text = local_assigns.fetch(:help_text, s_('CascadingSettings|Subgroups cannot change this setting.'))
- return unless attribute && group && form && cascading_namespace_settings_enabled?
- return if group.namespace_settings.public_send("#{attribute}_locked?")
- return if setting_locked
- lock_attribute = "lock_#{attribute}"
.gl-form-checkbox.custom-control.custom-checkbox
= form.check_box lock_attribute, checked: group.namespace_settings.public_send(lock_attribute), class: 'custom-control-input', data: { testid: 'enforce-for-all-subgroups-checkbox' }
= form.label lock_attribute, class: 'custom-control-label' do
%span= s_('CascadingSettings|Enforce for all subgroups')
%p.help-text= s_('CascadingSettings|Subgroups cannot change this setting.')
%span
= yield.presence || s_('CascadingSettings|Enforce for all subgroups')
%p.help-text
= help_text
%button.position-absolute.gl-top-3.gl-right-0.gl-translate-y-n50.gl-cursor-default.btn.btn-default.btn-sm.gl-button.btn-default-tertiary.js-cascading-settings-lock-popover-target{ class: 'gl-p-1! gl-text-gray-600! gl-bg-transparent!',
type: 'button',
data: cascading_namespace_settings_popover_data(attribute, group, settings_path_helper) }
= sprite_icon('lock', size: 16)
- attribute = local_assigns.fetch(:attribute, nil)
- group = local_assigns.fetch(:group, nil)
- form = local_assigns.fetch(:form, nil)
- settings_path_helper = local_assigns.fetch(:settings_path_helper, nil)
- form = local_assigns.fetch(:form, nil)
- setting_locked = local_assigns.fetch(:setting_locked, false)
- help_text = local_assigns.fetch(:help_text, nil)
- return unless attribute && group && form && settings_path_helper
- setting_locked = group.namespace_settings.public_send("#{attribute}_locked?")
- return unless attribute && form && settings_path_helper
= form.label attribute, class: 'custom-control-label', aria: { disabled: setting_locked } do
%span.position-relative.gl-pr-6.gl-display-inline-flex
= render 'shared/namespaces/cascading_settings/setting_label_container' do
= yield
- if setting_locked
%button.position-absolute.gl-top-3.gl-right-0.gl-translate-y-n50.gl-cursor-default.btn.btn-default.btn-sm.gl-button.btn-default-tertiary.js-cascading-settings-lock-popover-target{ class: 'gl-p-1! gl-text-gray-600! gl-bg-transparent!',
type: 'button',
data: cascading_namespace_settings_popover_data(attribute, group, settings_path_helper) }
= sprite_icon('lock', size: 16)
= render 'shared/namespaces/cascading_settings/lock_icon', local_assigns
- if help_text
%p.help-text
= help_text
%span.position-relative.gl-pr-6.gl-display-inline-flex
= yield
- attribute = local_assigns.fetch(:attribute, nil)
- settings_path_helper = local_assigns.fetch(:settings_path_helper, nil)
- setting_locked = local_assigns.fetch(:setting_locked, false)
- help_text = local_assigns.fetch(:help_text, nil)
- return unless attribute && settings_path_helper
%legend.h5.gl-border-none.gl-m-0
= render 'shared/namespaces/cascading_settings/setting_label_container' do
= yield
- if setting_locked
= render 'shared/namespaces/cascading_settings/lock_icon', local_assigns
- if help_text
%p.gl-text-gray-500
= help_text
- return unless show_delayed_project_removal_setting?(group)
- setting_locked = cascading_namespace_setting_locked?(:delayed_project_removal, group)
.form-group{ data: { testid: 'delayed-project-removal-form-group' } }
.gl-form-checkbox.custom-control.custom-checkbox
= f.check_box :delayed_project_removal, checked: group.namespace_settings.delayed_project_removal?, disabled: group.namespace_settings.delayed_project_removal_locked?, class: 'custom-control-input', data: { testid: 'delayed-project-removal-checkbox' }
= render 'shared/namespaces/cascading_settings/setting_label', attribute: :delayed_project_removal,
= f.check_box :delayed_project_removal, checked: group.namespace_settings.delayed_project_removal?, disabled: setting_locked, class: 'custom-control-input', data: { testid: 'delayed-project-removal-checkbox' }
= render 'shared/namespaces/cascading_settings/setting_label_checkbox', attribute: :delayed_project_removal,
group: group,
form: f,
setting_locked: setting_locked,
settings_path_helper: -> (locked_ancestor) { edit_group_path(locked_ancestor, anchor: 'js-permissions-settings') },
help_text: delayed_project_removal_help_text do
= s_('GroupSettings|Enable delayed project removal')
= render 'shared/namespaces/cascading_settings/enforcement_checkbox', attribute: :delayed_project_removal, group: group, form: f
= render 'shared/namespaces/cascading_settings/enforcement_checkbox',
attribute: :delayed_project_removal,
group: group,
form: f,
setting_locked: setting_locked
......@@ -265,4 +265,32 @@ RSpec.describe NamespacesHelper do
end
end
end
describe '#cascading_namespace_setting_locked?' do
let(:attribute) { :delayed_project_removal }
context 'when `group` argument is `nil`' do
it 'returns `false`' do
expect(helper.cascading_namespace_setting_locked?(attribute, nil)).to eq(false)
end
end
context 'when `*_locked?` method does not exist' do
it 'returns `false`' do
expect(helper.cascading_namespace_setting_locked?(:attribute_that_does_not_exist, admin_group)).to eq(false)
end
end
context 'when `*_locked?` method does exist' do
before do
allow(admin_group.namespace_settings).to receive(:delayed_project_removal_locked?).and_return(true)
end
it 'calls corresponding `*_locked?` method' do
helper.cascading_namespace_setting_locked?(attribute, admin_group, include_self: true)
expect(admin_group.namespace_settings).to have_received(:delayed_project_removal_locked?).with(include_self: true)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment