Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Close the confirmation dialog after updating a balance value (#4796)

Yong Cong Sinb248fd1844d8
diff --git a/app/controllers/concerns/stream_extensions.rb b/app/controllers/concerns/stream_extensions.rb
index 4a1a62c61b..4fb02fe252 100644
--- a/app/controllers/concerns/stream_extensions.rb
+++ b/app/controllers/concerns/stream_extensions.rb
@@ -30,7 +30,9 @@ module StreamExtensions
flash[:notice] = notice if notice.present?
flash[:alert] = alert if alert.present?
- redirect_target_url = redirect_back ? request.referer : path
+ # A request without a Referer (a no-referrer policy, a direct POST) goes
+ # to the fallback rather than to a redirect with no target.
+ redirect_target_url = redirect_back ? (request.referer.presence || path) : path
render turbo_stream: turbo_stream.action(:redirect, redirect_target_url)
end
end
diff --git a/app/controllers/valuations_controller.rb b/app/controllers/valuations_controller.rb
index 9dbfdd597b..605b6dfeaa 100644
--- a/app/controllers/valuations_controller.rb
+++ b/app/controllers/valuations_controller.rb
@@ -86,14 +86,22 @@ class ValuationsController < ApplicationController
respond_to do |format|
format.html { redirect_back_or_to account_path(@entry.account), notice: t(".entry_updated") }
format.turbo_stream do
- render turbo_stream: [
- turbo_stream.replace(
- dom_id(@entry, :header),
- partial: "valuations/header",
- locals: { entry: @entry }
- ),
- turbo_stream.replace(@entry)
- ]
+ if result
+ # A new value comes through the confirmation dialog, which nothing
+ # in the drawer would close, and it reshapes the balance history the
+ # page charts. Like a new value, it reloads the page.
+ stream_redirect_back_or_to(account_path(@entry.account), notice: t(".entry_updated"))
+ else
+ # Notes save as they are typed in the open drawer, which stays open.
+ render turbo_stream: [
+ turbo_stream.replace(
+ dom_id(@entry, :header),
+ partial: "valuations/header",
+ locals: { entry: @entry }
+ ),
+ turbo_stream.replace(@entry)
+ ]
+ end
end
end
else
diff --git a/test/controllers/valuations_controller_test.rb b/test/controllers/valuations_controller_test.rb
index b7151e6063..54763fe88b 100644
--- a/test/controllers/valuations_controller_test.rb
+++ b/test/controllers/valuations_controller_test.rb
@@ -51,6 +51,41 @@ class ValuationsControllerTest < ActionDispatch::IntegrationTest
assert_equal "Test notes", @entry.notes
end
+ test "a confirmed value update reloads the page, closing the confirmation dialog" do
+ patch valuation_url(@entry), params: { entry: { amount: 22000, date: @entry.date } }, as: :turbo_stream,
+ headers: { "Referer" => account_url(@entry.account) }
+
+ assert_response :success
+ assert_select "turbo-stream[action=redirect][target=?]", account_url(@entry.account)
+ assert_equal 22000, @entry.reload.amount
+ end
+
+ test "a confirmed date change is saved and reloads the page" do
+ new_date = @entry.date - 3.days
+
+ patch valuation_url(@entry), params: { entry: { amount: @entry.amount, date: new_date } }, as: :turbo_stream,
+ headers: { "Referer" => account_url(@entry.account) }
+
+ assert_select "turbo-stream[action=redirect][target=?]", account_url(@entry.account)
+ assert_equal new_date, @entry.reload.date
+ end
+
+ test "a confirmed value update without a Referer reloads the account page" do
+ patch valuation_url(@entry), params: { entry: { amount: 22000, date: @entry.date } }, as: :turbo_stream
+
+ assert_response :success
+ assert_select "turbo-stream[action=redirect][target=?]", account_path(@entry.account)
+ end
+
+ test "saving notes keeps the drawer open and redraws it in place" do
+ patch valuation_url(@entry), params: { entry: { notes: "Checked against the statement" } }, as: :turbo_stream
+
+ assert_response :success
+ assert_select "turbo-stream[action=replace][target=?]", dom_id(@entry, :header)
+ assert_select "turbo-stream[action=redirect]", count: 0
+ assert_equal "Checked against the statement", @entry.reload.notes
+ end
+
test "confirm_create with blank amount returns unprocessable entity" do
account = accounts(:investment)