Skip to content

Commit

Permalink
Merge pull request #14880 from opf/bug/32940-unsetting-a-user-value-i…
Browse files Browse the repository at this point in the history
…n-bulk-is-not-possible

[#32940] Unsetting a user value in bulk is not possible
  • Loading branch information
aaron-contreras authored Mar 4, 2024
2 parents 4a2c27a + 5dc12a8 commit c92daa1
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 9 deletions.
15 changes: 9 additions & 6 deletions app/controllers/work_packages/bulk_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,9 @@ def destroy_work_packages(work_packages)
def attributes_for_update
return {} unless params.has_key? :work_package

permitted_params
.update_work_package
.tap { |attributes| attributes[:custom_field_values]&.reject! { |_k, v| v.blank? } }
.compact_blank
.transform_values { |v| v == 'none' ? '' : v }
.to_h
attributes = permitted_params.update_work_package
attributes[:custom_field_values] = transform_attributes(attributes[:custom_field_values])
transform_attributes(attributes)
end

def user
Expand All @@ -124,4 +121,10 @@ def user
def default_breadcrumb
I18n.t(:label_work_package_plural)
end

def transform_attributes(attributes)
Hash(attributes)
.compact_blank
.transform_values { |v| Array(v).include?('none') ? '' : v }
end
end
12 changes: 9 additions & 3 deletions app/helpers/custom_fields_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def custom_field_tag_for_bulk_edit(name, custom_field, project = nil)
field_name = "#{name}[custom_field_values][#{custom_field.id}]"
field_id = "#{name}_custom_field_values_#{custom_field.id}"
field_format = OpenProject::CustomFieldFormat.find_by_name(custom_field.field_format)

case field_format.try(:edit_as)
when 'date'
angular_component_tag 'op-modal-single-date-picker',
Expand All @@ -164,12 +165,17 @@ def custom_field_tag_for_bulk_edit(name, custom_field, project = nil)
styled_text_area_tag(field_name, '', id: field_id, rows: 3, with_text_formatting: true)
when 'bool'
styled_select_tag(field_name, options_for_select([[I18n.t(:label_no_change_option), ''],
([I18n.t(:label_none), 'none'] unless custom_field.required?),
[I18n.t(:general_text_yes), '1'],
[I18n.t(:general_text_no), '0']]), id: field_id)
[I18n.t(:general_text_no), '0']].compact), id: field_id)
when 'list'
base_options = [[I18n.t(:label_no_change_option), '']]
unless custom_field.required?
unset_label = custom_field.field_format == 'user' ? :label_nobody : :label_none
base_options << [I18n.t(unset_label), 'none']
end
styled_select_tag(field_name,
options_for_select([[I18n.t(:label_no_change_option),
'']] + custom_field.possible_values_options(project)),
options_for_select(base_options + custom_field.possible_values_options(project)),
id: field_id,
multiple: custom_field.multi_value?)
else
Expand Down
95 changes: 95 additions & 0 deletions spec/features/work_packages/bulk/update_work_package_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,101 @@
end
end

describe 'unsetting values for different fields' do
let(:boolean_cf) do
create(:boolean_wp_custom_field,
name: 'Bool CF',
types: [type],
projects: [project])
end
let(:required_boolean_cf) do
create(:boolean_wp_custom_field,
name: 'Required Bool CF',
types: [type],
projects: [project],
is_required: true)
end
let(:list_cf) do
create(:list_wp_custom_field,
name: 'List CF',
types: [type],
projects: [project],
possible_values: %w[A B C])
end
let(:required_list_cf) do
create(:list_wp_custom_field,
name: 'Required List CF',
types: [type],
projects: [project],
possible_values: %w[A B C],
is_required: true)
end
let(:multi_list_cf) do
create(:list_wp_custom_field, :multi_list,
name: 'Multi select List CF',
types: [type],
projects: [project],
possible_values: %w[A B C])
end
let(:user_cf) do
create(:user_wp_custom_field,
name: 'User CF',
types: [type],
projects: [project])
end
let(:multi_user_cf) do
create(:user_wp_custom_field, :multi_user,
name: 'Multi user CF',
types: [type],
projects: [project])
end

let(:default_cf_values) do
{
boolean_cf.id => true,
required_boolean_cf.id => false,
list_cf.id => list_cf.custom_options.find_by(value: "B"),
required_list_cf.id => required_list_cf.custom_options.find_by(value: "B"),
multi_list_cf.id => multi_list_cf.custom_options.find_by(value: "B"),
user_cf.id => dev,
multi_user_cf.id => [dev, mover]
}
end

before do
[work_package, work_package2].each do |wp|
wp.update!(custom_field_values: default_cf_values)
end

refresh
wait_for_reload
end

it 'clears the chosen values' do
# Required fields should not have a 'none' option
expect(page).to have_no_select(required_boolean_cf.name, with_options: ['none'])
expect(page).to have_no_select(required_list_cf.name, with_options: ['none'])

# Unset any non-required fields
select 'none', from: boolean_cf.name
select 'none', from: list_cf.name
select 'none', from: multi_list_cf.name
select 'nobody', from: user_cf.name
select 'nobody', from: multi_user_cf.name

click_on 'Submit'

expect_angular_frontend_initialized
wp_table.expect_work_package_count 2

# It clears all the values except the required fields
expect(work_package.reload.custom_field_values.pluck(:value).compact)
.to eq(['f', required_list_cf.custom_options.find_by(value: "B").id.to_s])
expect(work_package2.reload.custom_field_values.pluck(:value).compact)
.to eq(['f', required_list_cf.custom_options.find_by(value: "B").id.to_s])
end
end

context 'when custom fields are removed from types' do
it 'does not display them on the form' do
expect(page).to have_field custom_field_removed.name
Expand Down

0 comments on commit c92daa1

Please sign in to comment.