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

[65303] Display non-progress errors in a top banner

Christophe Bliardab4688e47e21
diff --git a/app/controllers/concerns/op_turbo/component_stream.rb b/app/controllers/concerns/op_turbo/component_stream.rb
index ccec531c515..03b88603753 100644
--- a/app/controllers/concerns/op_turbo/component_stream.rb
+++ b/app/controllers/concerns/op_turbo/component_stream.rb
@@ -96,6 +96,8 @@ module OpTurbo
end
def render_flash_message_via_turbo_stream(message:, component: OpPrimer::FlashComponent, **)
+ return if message.blank?
+
instance = component.new(**).with_content(message)
turbo_streams << instance.render_as_turbo_stream(view_context:, action: :flash)
end
diff --git a/app/controllers/work_packages/progress_controller.rb b/app/controllers/work_packages/progress_controller.rb
index 127ce1d74d7..61a29a9b8ee 100644
--- a/app/controllers/work_packages/progress_controller.rb
+++ b/app/controllers/work_packages/progress_controller.rb
@@ -30,6 +30,7 @@
class WorkPackages::ProgressController < ApplicationController
include OpTurbo::ComponentStream
+ include FlashMessagesHelper
ERROR_PRONE_ATTRIBUTES = %i[status_id
estimated_hours
@@ -104,6 +105,9 @@ class WorkPackages::ProgressController < ApplicationController
else
respond_to do |format|
format.turbo_stream do
+ # errors not visible from progress modal fields are rendered in a flash message
+ render_error_flash_message_via_turbo_stream(message: extra_error_messages(service_call))
+
update_via_turbo_stream(
component: progress_modal_component,
method: "morph"
@@ -207,4 +211,12 @@ class WorkPackages::ProgressController < ApplicationController
def formatted_duration(hours)
API::V3::Utilities::DateTimeFormatter.format_duration_from_hours(hours, allow_nil: true)
end
+
+ def extra_error_messages(service_call)
+ errors_not_handled_by_progress_modal = service_call.errors.reject do |error|
+ ERROR_PRONE_ATTRIBUTES.include?(error.attribute)
+ end
+
+ join_flash_messages(errors_not_handled_by_progress_modal.map(&:full_message))
+ end
end
diff --git a/spec/controllers/work_packages/progress_controller_spec.rb b/spec/controllers/work_packages/progress_controller_spec.rb
index 448c0584d40..1cb91be9ddf 100644
--- a/spec/controllers/work_packages/progress_controller_spec.rb
+++ b/spec/controllers/work_packages/progress_controller_spec.rb
@@ -45,6 +45,12 @@ RSpec.describe WorkPackages::ProgressController do
work_package.errors.group_by_attribute.slice(:estimated_hours, :remaining_hours, :done_ratio)
end
+ # Gets the html content of the template of the first turbo-stream with the
+ # given action.
+ def turbo_stream_template(action:)
+ Nokogiri("<response>#{response.body}</response>").css("turbo-stream[action=#{action}] template").first.inner_html
+ end
+
describe "GET /work_packages/progress/new" do
let(:params) do
{
@@ -236,6 +242,39 @@ RSpec.describe WorkPackages::ProgressController do
expect(work_package.remaining_hours).to eq(4)
expect(work_package.done_ratio).to eq(90)
end
+
+ context "when the user enters invalid values" do
+ it "replies with a 422 status and displays an error message related to the erroneous field in the modal" do
+ params["work_package"]["estimated_hours_touched"] = "true"
+ params["work_package"]["estimated_hours"] = "-1"
+ patch("update", params:, as: :turbo_stream)
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(response).to have_turbo_stream action: "update"
+ # the flash is not rendered with the progress field errors, as they are already in the progress modal
+ expect(response).not_to have_turbo_stream action: "flash"
+ template = Capybara.string(turbo_stream_template(action: "update"))
+ expect(template).to have_css("primer-text-field:has(label:contains('Work')) .FormControl-inlineValidation",
+ text: "Must be greater than or equal to 0.")
+ end
+ end
+
+ context "when the work package cannot be saved due to other errors" do
+ before do
+ # update the work package in an invalid state
+ work_package.update_column(:subject, "")
+ end
+
+ it "replies with a 422 status and displays an error message in a flash" do
+ params["work_package"]["estimated_hours_touched"] = "true"
+ params["work_package"]["estimated_hours"] = "0"
+ patch("update", params:, as: :turbo_stream)
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(response).to have_turbo_stream action: "flash"
+ expect(turbo_stream_template(action: "flash")).to include("Subject can't be blank.")
+ end
+ end
end
# Used on new work package creation form