Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix: Transaction Pagination Skipping Entries (#2179)

Jonathan Changb9716a0485d5
diff --git a/app/models/entry.rb b/app/models/entry.rb
index 1599f4ad47..a29b3b2bd7 100644
--- a/app/models/entry.rb
+++ b/app/models/entry.rb
@@ -36,7 +36,8 @@ class Entry < ApplicationRecord
order(
date: :asc,
Arel.sql("CASE WHEN entries.entryable_type = 'Valuation' THEN 1 ELSE 0 END") => :asc,
- created_at: :asc
+ created_at: :asc,
+ id: :asc
)
}
@@ -44,7 +45,8 @@ class Entry < ApplicationRecord
order(
date: :desc,
Arel.sql("CASE WHEN entries.entryable_type = 'Valuation' THEN 1 ELSE 0 END") => :desc,
- created_at: :desc
+ created_at: :desc,
+ id: :desc
)
}
diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rb
index 73158ecd18..b533d89c72 100644
--- a/test/controllers/transactions_controller_test.rb
+++ b/test/controllers/transactions_controller_test.rb
@@ -239,6 +239,43 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
assert_operator overflow_count, :>, 0, "Overflow should show some transactions"
end
+ test "pagination does not duplicate or skip transactions with same date and timestamp" do
+ family = families(:empty)
+ user = users(:empty)
+ sign_in user
+
+ family.accounts.each { |account| account.entries.delete_all }
+
+ account = family.accounts.create! name: "Same day", balance: 0, currency: "USD", accountable: Depository.new
+ timestamp = Time.zone.parse("2026-05-05 12:00:00")
+
+ entries = 13.times.map do |index|
+ create_transaction(
+ account: account,
+ name: "May 05 Transaction #{index + 1}",
+ amount: 100 + index,
+ date: Date.new(2026, 5, 5),
+ created_at: timestamp,
+ updated_at: timestamp
+ )
+ end
+
+ expected_entry_ids = Entry.where(id: entries.map(&:id)).reverse_chronological.pluck(:id).map(&:to_s)
+
+ get transactions_url(page: 1, per_page: 10)
+ assert_response :success
+ page_1_entry_ids = rendered_entry_ids
+
+ get transactions_url(page: 2, per_page: 10)
+ assert_response :success
+ page_2_entry_ids = rendered_entry_ids
+
+ assert_equal expected_entry_ids.first(10), page_1_entry_ids
+ assert_equal expected_entry_ids.drop(10), page_2_entry_ids
+ assert_empty page_1_entry_ids & page_2_entry_ids
+ assert_equal expected_entry_ids, page_1_entry_ids + page_2_entry_ids
+ end
+
test "calls Transaction::Search totals method with correct search parameters" do
family = families(:empty)
sign_in users(:empty)
@@ -686,6 +723,10 @@ end
end
private
+ def rendered_entry_ids
+ css_select("turbo-frame[id^='entry_']").map { |node| node["id"].delete_prefix("entry_") }
+ end
+
def capture_sql_queries
queries = []
callback = lambda do |_name, _started, _finished, _unique_id, payload|
diff --git a/test/models/entry_test.rb b/test/models/entry_test.rb
new file mode 100644
index 0000000000..bcf820d4db
--- /dev/null
+++ b/test/models/entry_test.rb
@@ -0,0 +1,25 @@
+require "test_helper"
+
+class EntryTest < ActiveSupport::TestCase
+ include EntriesTestHelper
+
+ test "chronological ordering uses id as final tie breaker" do
+ account = accounts(:depository)
+ timestamp = Time.zone.parse("2026-05-05 12:00:00")
+
+ entries = 3.times.map do |index|
+ create_transaction(
+ account: account,
+ name: "Same timestamp transaction #{index}",
+ date: Date.new(2026, 5, 5),
+ created_at: timestamp,
+ updated_at: timestamp
+ )
+ end
+
+ entry_ids = entries.map(&:id)
+
+ assert_equal entry_ids.sort, Entry.where(id: entry_ids).chronological.pluck(:id)
+ assert_equal entry_ids.sort.reverse, Entry.where(id: entry_ids).reverse_chronological.pluck(:id)
+ end
+end