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

reuse errors flash for wp copy

ulfertsebf845375841
diff --git a/app/controllers/work_packages/bulk_controller.rb b/app/controllers/work_packages/bulk_controller.rb
index 1e64088a39a..4d77c70efa8 100644
--- a/app/controllers/work_packages/bulk_controller.rb
+++ b/app/controllers/work_packages/bulk_controller.rb
@@ -52,7 +52,7 @@ class WorkPackages::BulkController < ApplicationController
flash[:notice] = t(:notice_successful_update)
redirect_back_or_default(controller: '/work_packages', action: :index, project_id: @project)
else
- error_flash(@work_packages.count, @call.errors)
+ error_flash(@work_packages, @call)
setup_edit
render action: :edit
end
diff --git a/app/controllers/work_packages/flash_bulk_error.rb b/app/controllers/work_packages/flash_bulk_error.rb
index 900b6b9a9fb..6437582e7a3 100644
--- a/app/controllers/work_packages/flash_bulk_error.rb
+++ b/app/controllers/work_packages/flash_bulk_error.rb
@@ -34,8 +34,10 @@ module WorkPackages::FlashBulkError
included do
private
- def error_flash(total, errored)
- flash[:error] = render_to_string partial: 'work_packages/bulk/errors', locals: { errors: errored, total: total }
+ def error_flash(selected_work_packages, service_result)
+ flash[:error] = render_to_string partial: 'work_packages/bulk/errors',
+ locals: { service_result: service_result,
+ selected_work_packages: selected_work_packages }
end
end
end
diff --git a/app/controllers/work_packages/moves_controller.rb b/app/controllers/work_packages/moves_controller.rb
index a3eafb6e523..092f43f7769 100644
--- a/app/controllers/work_packages/moves_controller.rb
+++ b/app/controllers/work_packages/moves_controller.rb
@@ -44,8 +44,7 @@ class WorkPackages::MovesController < ApplicationController
new_type = params[:new_type_id].blank? ? nil : @target_project.types.find_by(id: params[:new_type_id])
- unsaved_work_packages = {}
- moved_work_packages = []
+ result = ServiceResult.new success: true, result: true
@work_packages.each do |work_package|
work_package.reload
@@ -64,20 +63,16 @@ class WorkPackages::MovesController < ApplicationController
attributes: permitted_create_params,
journal_note: @notes)
- if service_call.success?
- moved_work_packages << service_call.result
- elsif @copy
- unsaved_work_packages.concat dependent_error_ids(work_package.id, service_call)
- else
- unsaved_work_packages[work_package.id] = service_call.errors.full_messages
- end
+ result.add_dependent!(service_call)
end
- set_flash_from_bulk_work_package_save(@work_packages, unsaved_work_packages)
+ result.result = false if result.failure?
+
+ set_flash_from_bulk_work_package_save(@work_packages, result)
if params[:follow]
- if @work_packages.size == 1 && moved_work_packages.size == 1
- redirect_to work_package_path(moved_work_packages.first)
+ if result.success? && @work_packages.size == 1
+ redirect_to work_package_path(@work_packages.first)
else
redirect_to project_work_packages_path(@target_project || @project)
end
@@ -88,28 +83,12 @@ class WorkPackages::MovesController < ApplicationController
private
- def set_flash_from_bulk_work_package_save(work_packages, unsaved_work_packages)
- if unsaved_work_packages.empty? and not work_packages.empty?
+ def set_flash_from_bulk_work_package_save(work_packages, service_result)
+ if service_result.success? && work_packages.any?
flash[:notice] = @copy ? I18n.t(:notice_successful_create) : I18n.t(:notice_successful_update)
else
- error_flash(work_packages.count,
- unsaved_work_packages)
- end
- end
-
- ##
- # When copying, add work package ids that are failing
- def dependent_error_ids(parent_id, service_call)
- ids = service_call
- .results_with_errors(include_self: false)
- .map { |result| result.state.copied_from_work_package_id }
- .compact
-
- if ids.present?
- joined = ids.map { |id| "##{id}" }.join(" ")
- ["#{parent_id} (+ children errors: #{joined})"]
- else
- [parent_id]
+ error_flash(work_packages,
+ service_result)
end
end
@@ -117,8 +96,6 @@ class WorkPackages::MovesController < ApplicationController
I18n.t(:label_move_work_package)
end
- private
-
# Check if project is unique before bulk operations
def check_project_uniqueness
unless @project
diff --git a/app/services/work_packages/bulk/update_service.rb b/app/services/work_packages/bulk/update_service.rb
index 6dd8bfcc07a..a87dc4f4ba0 100644
--- a/app/services/work_packages/bulk/update_service.rb
+++ b/app/services/work_packages/bulk/update_service.rb
@@ -51,8 +51,7 @@ module WorkPackages
private
def bulk_update(params)
- saved = []
- errors = {}
+ result = ServiceResult.new success: true, result: true
work_packages.each do |work_package|
# As updating one work package might have already saved another one,
@@ -60,8 +59,6 @@ module WorkPackages
# we need to reload the work packages to avoid running into stale object errors.
work_package.reload
- work_package.add_journal(user, params[:notes])
-
# filter parameters by whitelist and add defaults
attributes = parse_params_for_bulk_work_package_attributes params, work_package.project
@@ -71,14 +68,12 @@ module WorkPackages
.new(user: user, model: work_package)
.call(**attributes.symbolize_keys)
- if service_call.success?
- saved << work_package.id
- else
- errors[work_package.id] = service_call.errors.full_messages
- end
+ result.add_dependent!(service_call)
end
- ServiceResult.new success: errors.empty?, result: saved, errors: errors
+ result.result = false if result.failure?
+
+ result
end
# TODO: move params transformation out of here as this is not the responsibility of a service
@@ -87,10 +82,10 @@ module WorkPackages
safe_params = permitted_params.update_work_package project: project
attributes = safe_params.reject { |_k, v| v.blank? }
- attributes.keys.each do |k|
+ attributes.each_key do |k|
attributes[k] = '' if attributes[k] == 'none'
end
- attributes[:custom_field_values].reject! { |_k, v| v.blank? } if attributes[:custom_field_values]
+ attributes[:custom_field_values]&.reject! { |_k, v| v.blank? }
attributes.delete :custom_field_values if not attributes.has_key?(:custom_field_values) or attributes[:custom_field_values].empty?
attributes.to_h
end
diff --git a/app/views/work_packages/bulk/_errors.html.erb b/app/views/work_packages/bulk/_errors.html.erb
index a71d1180bba..99040b22f87 100644
--- a/app/views/work_packages/bulk/_errors.html.erb
+++ b/app/views/work_packages/bulk/_errors.html.erb
@@ -1,20 +1,35 @@
-<% if total - errors.count == 0 %>
+<%
+ erroneous_results = service_result.results_with_errors(include_self: false)
+ error_count = erroneous_results.count
+ total_count = service_result.dependent_results.count
+ selected_count = selected_work_packages.count
+%>
+
+<% if total_count - error_count == 0 %>
<%= t('work_packages.bulk.none_could_be_saved',
- total: total) %>
+ total: total_count) %>
<% else %>
<%= t('work_packages.bulk.x_out_of_y_could_be_saved',
- failing: errors.count,
- total: total,
- success: total - errors.count) %>
+ failing: error_count,
+ total: total_count,
+ success: total_count - error_count) %>
<%= t('work_packages.bulk.could_not_be_saved') %>
<% end %>
+<% if selected_count < total_count %>
+ <%= t('work_packages.bulk.selected_because_descendants',
+ total: total_count,
+ selected: selected_count) %>
+<% end %>
+
<ul>
- <% errors.each do |wp_id, messages| %>
+ <% erroneous_results.each do |call| %>
+ <% wp_id = call.state.copied_from_work_package_id || call.result.id %>
+
<li>
- <%= link_to "##{wp_id}", work_package_path(wp_id) %>:
- <%= safe_join messages, ' ' %>
+ <%= link_to "##{wp_id}", work_package_path(wp_id) %><%= selected_work_packages.map(&:id).include?(wp_id) ? '' : " (#{I18n.t('work_packages.bulk.descendant')})" %>:
+ <%= safe_join call.errors.full_messages, ' ' %>
</li>
<% end %>
</ul>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index ceb48339291..dcbaf300774 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -366,8 +366,10 @@ en:
bulk:
could_not_be_saved: "The following work packages could not be saved:"
- none_could_be_saved: "None of the selected %{total} work packages could be updated."
- x_out_of_y_could_be_saved: "%{failing} out of the selected %{total} work packages could not be updated while %{success} could."
+ none_could_be_saved: "None of the %{total} work packages could be updated."
+ x_out_of_y_could_be_saved: "%{failing} out of the %{total} work packages could not be updated while %{success} could."
+ selected_because_descendants: "While %{selected} work packages where selected, in total %{total} work packages are affected which includes descendants."
+ descendant: "descendant of selected"
move:
diff --git a/spec/features/work_packages/bulk/copy_work_package_spec.rb b/spec/features/work_packages/bulk/copy_work_package_spec.rb
index 86ab0f03261..b29e876c021 100644
--- a/spec/features/work_packages/bulk/copy_work_package_spec.rb
+++ b/spec/features/work_packages/bulk/copy_work_package_spec.rb
@@ -2,59 +2,45 @@ require 'spec_helper'
require 'features/page_objects/notification'
describe 'Copy work packages through Rails view', js: true do
- let(:dev_role) do
- FactoryBot.create :role,
- permissions: %i[view_work_packages]
- end
- let(:mover_role) do
- FactoryBot.create :role,
- permissions: %i[view_work_packages copy_work_packages move_work_packages manage_subtasks assign_versions
- add_work_packages]
- end
- let(:dev) do
+ shared_let(:type) { FactoryBot.create :type, name: 'Bug' }
+ shared_let(:type2) { FactoryBot.create :type, name: 'Risk' }
+
+ shared_let(:project) { FactoryBot.create(:project, name: 'Source', types: [type, type2]) }
+ shared_let(:project2) { FactoryBot.create(:project, name: 'Target', types: [type, type2]) }
+
+ shared_let(:dev) do
FactoryBot.create :user,
firstname: 'Dev',
lastname: 'Guy',
member_in_project: project,
- member_through_role: dev_role
+ member_with_permissions: %i[view_work_packages]
end
- let(:mover) do
- FactoryBot.create :admin,
+ shared_let(:mover) do
+ FactoryBot.create :user,
firstname: 'Manager',
lastname: 'Guy',
- member_in_project: project,
- member_through_role: mover_role
+ member_in_projects: [project, project2],
+ member_with_permissions: %i[view_work_packages
+ copy_work_packages
+ move_work_packages
+ manage_subtasks
+ assign_versions
+ add_work_packages]
end
- let(:type) { FactoryBot.create :type, name: 'Bug' }
- let(:type2) { FactoryBot.create :type, name: 'Risk' }
-
- let!(:project) { FactoryBot.create(:project, name: 'Source', types: [type, type2]) }
- let!(:project2) { FactoryBot.create(:project, name: 'Target', types: [type, type2]) }
-
- let!(:work_package) do
+ shared_let(:work_package) do
FactoryBot.create(:work_package,
author: dev,
project: project,
type: type)
end
- let!(:work_package2) do
+ shared_let(:work_package2) do
FactoryBot.create(:work_package,
author: dev,
project: project,
type: type)
end
-
- let(:status) { work_package.status }
- let!(:version) { FactoryBot.create :version, project: project2 }
- let!(:status2) { FactoryBot.create :default_status }
- let!(:workflow) do
- FactoryBot.create :workflow,
- type_id: type2.id,
- old_status: work_package.status,
- new_status: status2,
- role: mover_role
- end
+ shared_let(:version) { FactoryBot.create :version, project: project2 }
let(:wp_table) { ::Pages::WorkPackagesTable.new(project) }
let(:context_menu) { Components::WorkPackages::ContextMenu.new }
@@ -80,7 +66,7 @@ describe 'Copy work packages through Rails view', js: true do
context_menu.choose 'Bulk copy'
expect(page).to have_selector('#new_project_id')
- select 'Target', from: 'new_project_id'
+ select project2.name, from: 'new_project_id'
sleep 1
@@ -104,33 +90,69 @@ describe 'Copy work packages through Rails view', js: true do
expect(copied_wps.map(&:version_id).uniq).to eq([version.id])
end
- describe 'copy and follow' do
- before do
- click_on 'Copy and follow'
- end
+ it 'moves parent and child wp to a new project' do
+ click_on 'Copy and follow'
- it 'moves parent and child wp to a new project' do
- expect_angular_frontend_initialized
- wp_table.expect_work_package_count 2
- expect(page).to have_selector('#projects-menu', text: 'Target')
+ expect_angular_frontend_initialized
+ wp_table.expect_work_package_count 2
+ expect(page).to have_selector('#projects-menu', text: 'Target')
- # Should not move the sources
- work_package2.reload
- work_package.reload
- expect(work_package.project_id).to eq(project.id)
- expect(work_package2.project_id).to eq(project.id)
+ # Should not move the sources
+ work_package2.reload
+ work_package.reload
+ expect(work_package.project_id).to eq(project.id)
+ expect(work_package2.project_id).to eq(project.id)
- # Check project of last two created wps
- copied_wps = WorkPackage.last(2)
- expect(copied_wps.map(&:project_id)).to eq([project2.id, project2.id])
+ # Check project of last two created wps
+ copied_wps = WorkPackage.last(2)
+ expect(copied_wps.map(&:project_id)).to eq([project2.id, project2.id])
+ end
+
+ context 'when the target project does not have the type' do
+ let!(:child) do
+ FactoryBot.create(:work_package,
+ author: dev,
+ project: project,
+ type: type,
+ parent: work_package)
+ end
+
+ before do
+ project2.types = [type2]
end
- context 'when the target project does not have the type' do
- let!(:project2) { FactoryBot.create(:project, name: 'Target', types: [type2]) }
+ it 'fails, informing of the reasons' do
+ click_on 'Copy and follow'
- it 'does moves the work package and changes the type' do
- expect(page).to have_selector('.flash.error', text: "Failed to save 2 work package(s) on 2 selected:")
- end
+ expect(page)
+ .to have_selector(
+ '.flash.error',
+ text: I18n.t('work_packages.bulk.none_could_be_saved', total: 3)
+ )
+
+ expect(page)
+ .to have_selector(
+ '.flash.error',
+ text: I18n.t('work_packages.bulk.selected_because_descendants', total: 3, selected: 2)
+ )
+
+ expect(page)
+ .to have_selector(
+ '.flash.error',
+ text: "#{work_package.id}: Type #{I18n.t('activerecord.errors.messages.inclusion')}"
+ )
+
+ expect(page)
+ .to have_selector(
+ '.flash.error',
+ text: "#{work_package2.id}: Type #{I18n.t('activerecord.errors.messages.inclusion')}"
+ )
+
+ expect(page)
+ .to have_selector(
+ '.flash.error',
+ text: "#{child.id} (descendant of selected): Type #{I18n.t('activerecord.errors.messages.inclusion')}"
+ )
end
end
end