Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(accounts): remember transaction page size across account navigation (#3084)

GFRfd01a5b3dcf3
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb
index 67d42d75de..1b84b5416a 100644
--- a/app/controllers/accounts_controller.rb
+++ b/app/controllers/accounts_controller.rb
@@ -67,9 +67,12 @@ class AccountsController < ApplicationController
return render_statement_tab_frame if statement_tab_frame_request?
end
+ per_page = safe_per_page(stored_per_page_default)
+ store_per_page!(per_page) if params[:per_page].present?
+
@pagy, @entries = pagy(
entries,
- limit: safe_per_page,
+ limit: per_page,
params: request.query_parameters.except("tab").merge("tab" => "activity")
)
@@ -275,6 +278,19 @@ class AccountsController < ApplicationController
Current.family
end
+ # Shares the "per page" preference with TransactionsController's
+ # prev_transaction_page_params so the page size the user picks on either
+ # the account activity feed or the global transactions page applies to both.
+ def store_per_page!(value)
+ Current.session.update!(
+ prev_transaction_page_params: Current.session.prev_transaction_page_params.merge("per_page" => value)
+ )
+ end
+
+ def stored_per_page_default
+ Current.session.prev_transaction_page_params["per_page"].presence || 10
+ end
+
def set_account
@account = Current.user.accessible_accounts.find(params[:id])
end
diff --git a/app/controllers/concerns/safe_pagination.rb b/app/controllers/concerns/safe_pagination.rb
index de0f794bc2..1043261ed8 100644
--- a/app/controllers/concerns/safe_pagination.rb
+++ b/app/controllers/concerns/safe_pagination.rb
@@ -6,6 +6,9 @@ module SafePagination
private
def safe_per_page(default = 10)
allowed_values = [ 10, 20, 30, 50, 100 ]
+ default = default.to_i
+ default = 10 unless allowed_values.include?(default)
+
per_page = params[:per_page].to_i
return default if per_page <= 0
diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb
index ef8f3d0002..f6256d9d48 100644
--- a/app/controllers/transactions_controller.rb
+++ b/app/controllers/transactions_controller.rb
@@ -41,7 +41,7 @@ class TransactionsController < ApplicationController
}
)
- @pagy, @transactions = pagy(base_scope, limit: safe_per_page)
+ @pagy, @transactions = pagy(base_scope, limit: safe_per_page(stored_params["per_page"]))
Transaction::ActivitySecurityPreloader.new(@transactions).preload
# Preload split parent data
@@ -577,7 +577,7 @@ class TransactionsController < ApplicationController
prev_transaction_page_params: {
q: search_params,
page: params[:page],
- per_page: params[:per_page]
+ per_page: params[:per_page].presence || stored_params["per_page"]
}
)
end
diff --git a/app/models/session.rb b/app/models/session.rb
index b84d056c24..7e44bd49d6 100644
--- a/app/models/session.rb
+++ b/app/models/session.rb
@@ -14,6 +14,10 @@ class Session < ApplicationRecord
before_create :capture_session_info
+ def prev_transaction_page_params
+ super || {}
+ end
+
def get_preferred_tab(tab_key)
data.dig("tab_preferences", tab_key)
end
diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb
index d62ed2e7d3..0b23c54b9e 100644
--- a/test/controllers/accounts_controller_test.rb
+++ b/test/controllers/accounts_controller_test.rb
@@ -179,6 +179,44 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
assert_select "p", text: I18n.t("UI.account.chart.title.total_gains")
end
+ test "remembers selected per_page across account navigation" do
+ other_account = accounts(:credit_card)
+
+ get account_url(@account, per_page: 50)
+ assert_response :success
+ assert_select "select[name='per_page'] option[value='50'][selected]"
+
+ get account_url(other_account)
+ assert_response :success
+ assert_select "select[name='per_page'] option[value='50'][selected]"
+ end
+
+ test "shares remembered per_page with the global transactions page" do
+ get transactions_url(per_page: 100)
+ assert_response :success
+
+ get account_url(@account)
+ assert_response :success
+ assert_select "select[name='per_page'] option[value='100'][selected]"
+ end
+
+ test "shares account per_page preference with the global transactions page" do
+ get account_url(@account, per_page: 50)
+ assert_response :success
+
+ get transactions_url
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_select "select[name='per_page'] option[value='50'][selected]"
+ end
+
+ test "falls back to default per_page when nothing was stored yet" do
+ get account_url(@account)
+ assert_response :success
+ assert_select "select[name='per_page'] option[value='10'][selected]"
+ end
+
test "activity pagination keeps activity tab when loaded from holdings tab" do
investment = accounts(:investment)
diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rb
index 6de5f9b810..5c5bce520a 100644
--- a/test/controllers/transactions_controller_test.rb
+++ b/test/controllers/transactions_controller_test.rb
@@ -239,6 +239,35 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
assert_operator overflow_count, :>, 0, "Overflow should show some transactions"
end
+ test "filtered requests without per_page keep the stored page size" do
+ family = families(:empty)
+ sign_in users(:empty)
+
+ family.accounts.each { |account| account.entries.delete_all }
+
+ account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
+
+ 25.times do |i|
+ create_transaction(
+ account: account,
+ name: "Transaction #{i + 1}",
+ amount: 100 + i,
+ date: Date.current - i.days
+ )
+ end
+
+ get transactions_url(per_page: 20)
+ assert_response :success
+
+ # A filtered request that omits per_page has query params present, so it
+ # is not eligible for the "restore stored params" redirect - it must fall
+ # back to the previously stored per_page instead of the hardcoded default.
+ get transactions_url(q: { search: "Transaction" })
+ assert_response :success
+ assert_select "select[name='per_page'] option[value='20'][selected]"
+ assert_equal 20, css_select("turbo-frame[id^='entry_']").count
+ end
+
test "pagination does not duplicate or skip transactions with same date and timestamp" do
family = families(:empty)
user = users(:empty)