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 bulk edit and move

ulferts2539b6060372
diff --git a/app/controllers/work_packages/bulk_controller.rb b/app/controllers/work_packages/bulk_controller.rb
index 7e58c2798fe..1e64088a39a 100644
--- a/app/controllers/work_packages/bulk_controller.rb
+++ b/app/controllers/work_packages/bulk_controller.rb
@@ -37,6 +37,8 @@ class WorkPackages::BulkController < ApplicationController
include RelationsHelper
include QueriesHelper
+ include WorkPackages::FlashBulkError
+
def edit
setup_edit
end
@@ -50,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
- @bulk_errors = @call.errors
+ error_flash(@work_packages.count, @call.errors)
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
new file mode 100644
index 00000000000..900b6b9a9fb
--- /dev/null
+++ b/app/controllers/work_packages/flash_bulk_error.rb
@@ -0,0 +1,41 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module WorkPackages::FlashBulkError
+ extend ActiveSupport::Concern
+
+ included do
+ private
+
+ def error_flash(total, errored)
+ flash[:error] = render_to_string partial: 'work_packages/bulk/errors', locals: { errors: errored, total: total }
+ end
+ end
+end
diff --git a/app/controllers/work_packages/moves_controller.rb b/app/controllers/work_packages/moves_controller.rb
index f0260d2fedf..a3eafb6e523 100644
--- a/app/controllers/work_packages/moves_controller.rb
+++ b/app/controllers/work_packages/moves_controller.rb
@@ -29,6 +29,8 @@
#++
class WorkPackages::MovesController < ApplicationController
+ include WorkPackages::FlashBulkError
+
default_search_scope :work_packages
before_action :find_work_packages, :check_project_uniqueness
before_action :authorize
@@ -42,7 +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_package_ids = []
+ unsaved_work_packages = {}
moved_work_packages = []
@work_packages.each do |work_package|
@@ -65,13 +67,13 @@ class WorkPackages::MovesController < ApplicationController
if service_call.success?
moved_work_packages << service_call.result
elsif @copy
- unsaved_work_package_ids.concat dependent_error_ids(work_package.id, service_call)
+ unsaved_work_packages.concat dependent_error_ids(work_package.id, service_call)
else
- unsaved_work_package_ids << work_package.id
+ unsaved_work_packages[work_package.id] = service_call.errors.full_messages
end
end
- set_flash_from_bulk_work_package_save(@work_packages, unsaved_work_package_ids)
+ set_flash_from_bulk_work_package_save(@work_packages, unsaved_work_packages)
if params[:follow]
if @work_packages.size == 1 && moved_work_packages.size == 1
@@ -84,14 +86,14 @@ class WorkPackages::MovesController < ApplicationController
end
end
- def set_flash_from_bulk_work_package_save(work_packages, unsaved_work_package_ids)
- if unsaved_work_package_ids.empty? and not work_packages.empty?
+ private
+
+ def set_flash_from_bulk_work_package_save(work_packages, unsaved_work_packages)
+ if unsaved_work_packages.empty? and not work_packages.empty?
flash[:notice] = @copy ? I18n.t(:notice_successful_create) : I18n.t(:notice_successful_update)
else
- flash[:error] = I18n.t(:notice_failed_to_save_work_packages,
- count: unsaved_work_package_ids.size,
- total: work_packages.size,
- ids: '#' + unsaved_work_package_ids.join(', #'))
+ error_flash(work_packages.count,
+ unsaved_work_packages)
end
end
diff --git a/app/views/work_packages/bulk/_errors.html.erb b/app/views/work_packages/bulk/_errors.html.erb
new file mode 100644
index 00000000000..a71d1180bba
--- /dev/null
+++ b/app/views/work_packages/bulk/_errors.html.erb
@@ -0,0 +1,20 @@
+<% if total - errors.count == 0 %>
+ <%= t('work_packages.bulk.none_could_be_saved',
+ total: total) %>
+<% else %>
+ <%= t('work_packages.bulk.x_out_of_y_could_be_saved',
+ failing: errors.count,
+ total: total,
+ success: total - errors.count) %>
+
+ <%= t('work_packages.bulk.could_not_be_saved') %>
+<% end %>
+
+<ul>
+ <% errors.each do |wp_id, messages| %>
+ <li>
+ <%= link_to "##{wp_id}", work_package_path(wp_id) %>:
+ <%= safe_join messages, ' ' %>
+ </li>
+ <% end %>
+</ul>
diff --git a/app/views/work_packages/bulk/edit.html.erb b/app/views/work_packages/bulk/edit.html.erb
index b5c46c0db80..baddb27f532 100644
--- a/app/views/work_packages/bulk/edit.html.erb
+++ b/app/views/work_packages/bulk/edit.html.erb
@@ -29,24 +29,6 @@ See COPYRIGHT and LICENSE files for more details.
<% html_title t(:label_bulk_edit_selected_work_packages) %>
-<% if @bulk_errors.present? %>
- <div class="op-toast -error">
- <a title="close" class="op-toast--close icon-context icon-close"></a>
- <div class="op-toast--content">
- <p><strong><%= t('work_packages.bulk.could_not_be_saved') %></strong></p>
-
- <ul>
- <% @bulk_errors.each do |wpid, messages| %>
- <li>
- <%= link_to "##{wpid}", work_package_path(wpid) %>:
- <%= safe_join messages, ' ' %>
- </li>
- <% end %>
- </ul>
- </div>
- </div>
-<% end %>
-
<h2><%= t(:label_bulk_edit_selected_work_packages) %></h2>
<ul>
<% @work_packages.each do |wp| %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 2338f22ff4c..ceb48339291 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -366,6 +366,8 @@ 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."
move:
diff --git a/spec/features/work_packages/bulk/move_work_package_spec.rb b/spec/features/work_packages/bulk/move_work_package_spec.rb
index b42df0763fb..676f6882bc9 100644
--- a/spec/features/work_packages/bulk/move_work_package_spec.rb
+++ b/spec/features/work_packages/bulk/move_work_package_spec.rb
@@ -1,6 +1,7 @@
require 'spec_helper'
require 'features/page_objects/notification'
+# rubocop:disable RSpec/MultipleMemoizedHelpers
describe 'Moving a work package through Rails view', js: true do
let(:dev_role) do
FactoryBot.create :role,
@@ -31,45 +32,48 @@ describe 'Moving a work package through Rails view', js: true do
let!(:project) { FactoryBot.create(:project, name: 'Source', types: [type, type2]) }
let!(:project2) { FactoryBot.create(:project, name: 'Target', types: [type, type2]) }
- let!(:work_package) do
+ let(:work_package) do
FactoryBot.create(:work_package,
author: dev,
project: project,
- type: type)
+ type: type,
+ status: status)
end
- let!(:child_wp) do
+ let(:work_package2) do
FactoryBot.create(:work_package,
author: dev,
- parent: work_package,
project: project,
- type: type)
- end
-
- let(:status) { work_package.status }
- 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
+ type: type,
+ status: work_package2_status)
end
+ let(:status) { FactoryBot.create(:status) }
+ let(:work_package2_status) { status }
let(:wp_table) { ::Pages::WorkPackagesTable.new(project) }
let(:context_menu) { Components::WorkPackages::ContextMenu.new }
let(:display_representation) { ::Components::WorkPackages::DisplayRepresentation.new }
+ let(:current_user) { mover }
+ let(:work_packages) { [work_package, work_package2] }
before do
+ work_packages
login_as current_user
wp_table.visit!
expect_angular_frontend_initialized
- wp_table.expect_work_package_listed work_package, child_wp
end
describe 'moving a work package and its children' do
- context 'with permission' do
- let(:current_user) { mover }
+ let(:work_packages) { [work_package, child_wp] }
+ let(:child_wp) do
+ FactoryBot.create(:work_package,
+ author: dev,
+ parent: work_package,
+ project: project,
+ type: type,
+ status: status)
+ end
+ context 'with permission' do
before do
expect(child_wp.project_id).to eq(project.id)
@@ -121,6 +125,43 @@ describe 'Moving a work package through Rails view', js: true do
end
end
+ describe 'moving an unmovable (e.g. readonly status) and a movable work package', with_ee: %i[readonly_work_packages] do
+ let(:work_packages) { [work_package, work_package2] }
+ let(:work_package2_status) { FactoryBot.create(:status, is_readonly: true) }
+
+ before do
+ loading_indicator_saveguard
+ # Select all work packages
+ find('body').send_keys [:control, 'a']
+
+ context_menu.open_for work_package2
+ context_menu.choose 'Bulk change of project'
+
+ # On work packages move page
+ select project2.name, from: 'new_project_id'
+ click_on 'Move and follow'
+ end
+
+ it 'displays an error message explaining which work package could not be moved and why' do
+ expect(page)
+ .to have_selector('.flash.error',
+ text: I18n.t('work_packages.bulk.could_not_be_saved'))
+ expect(page)
+ .to have_selector('.flash.error',
+ text: I18n.t('activerecord.errors.messages.error_readonly'))
+
+ expect(page)
+ .to have_selector('.flash.error',
+ text: I18n.t('work_packages.bulk.x_out_of_y_could_be_saved',
+ failing: 1,
+ total: 2,
+ success: 1))
+
+ expect(work_package.reload.project_id).to eq(project2.id)
+ expect(work_package2.reload.project_id).to eq(project.id)
+ end
+ end
+
describe 'accessing the bulk move from the card view' do
before do
display_representation.switch_to_card_layout
@@ -129,8 +170,6 @@ describe 'Moving a work package through Rails view', js: true do
end
context 'with permissions' do
- let(:current_user) { mover }
-
it 'does allow to move' do
context_menu.open_for work_package
context_menu.expect_options ['Bulk change of project']
@@ -147,3 +186,4 @@ describe 'Moving a work package through Rails view', js: true do
end
end
end
+# rubocop:enable RSpec/MultipleMemoizedHelpers
diff --git a/spec/features/work_packages/bulk/update_work_package_spec.rb b/spec/features/work_packages/bulk/update_work_package_spec.rb
index 6116d2a95df..3ff2d87a5c1 100644
--- a/spec/features/work_packages/bulk/update_work_package_spec.rb
+++ b/spec/features/work_packages/bulk/update_work_package_spec.rb
@@ -104,21 +104,25 @@ describe 'Bulk update work packages through Rails view', js: true do
fill_in 'work_package_start_date', with: '123'
click_on 'Submit'
- wp_table
- .expect_toast(message: I18n.t('work_packages.bulk.could_not_be_saved'), type: :error)
- wp_table
- .expect_toast(message: work_package.id, type: :error)
- wp_table
- .expect_toast(message: work_package2.id, type: :error)
- wp_table
- .expect_toast(
- message: I18n.t('activerecord.errors.messages.not_a_date'),
- type: :error
+ expect(page)
+ .to have_selector(
+ '.flash.error',
+ text: I18n.t('work_packages.bulk.none_could_be_saved', total: 2)
)
- wp_table
- .expect_toast(
- message: I18n.t('activerecord.errors.models.work_package.attributes.status_id.status_transition_invalid'),
- type: :error
+
+ expect(page)
+ .to have_selector(
+ '.flash.error',
+ text: "#{work_package.id}: Start date #{I18n.t('activerecord.errors.messages.not_a_date')}"
+ )
+
+ expect(page)
+ .to have_selector(
+ '.flash.error',
+ text: <<~MSG.squish
+ #{work_package2.id}:
+ Status #{I18n.t('activerecord.errors.models.work_package.attributes.status_id.status_transition_invalid')}
+ MSG
)
# Should not update the status