Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

List split rows in the order they were entered (#5169)

GFR5f71737c51de
diff --git a/app/controllers/splits_controller.rb b/app/controllers/splits_controller.rb
index d2cf5f3978..45ae4510f7 100644
--- a/app/controllers/splits_controller.rb
+++ b/app/controllers/splits_controller.rb
@@ -34,7 +34,7 @@ class SplitsController < ApplicationController
end
set_form_options
- @children = @entry.child_entries.includes(entryable: :tags)
+ @children = @entry.child_entries.in_split_order.includes(entryable: :tags)
end
def update
diff --git a/app/models/entry.rb b/app/models/entry.rb
index f0a7f04d4f..ae8d783046 100644
--- a/app/models/entry.rb
+++ b/app/models/entry.rb
@@ -86,6 +86,10 @@ class Entry < ApplicationRecord
before_destroy :prevent_individual_child_deletion, if: :split_child?
after_save :track_earliest_saved_date, if: :saved_change_to_date?
+ # Split rows in the order they were entered. Without an ORDER BY PostgreSQL
+ # returns them in physical order, which shifts as rows are rewritten.
+ scope :in_split_order, -> { order(:created_at, :id) }
+
scope :visible, -> {
joins(:account).where(accounts: { status: [ "draft", "active" ] })
}
diff --git a/app/models/family/data_exporter.rb b/app/models/family/data_exporter.rb
index 548bfad5e3..044ceed642 100644
--- a/app/models/family/data_exporter.rb
+++ b/app/models/family/data_exporter.rb
@@ -597,7 +597,7 @@ class Family::DataExporter
if parent_entry.association(:child_entries).loaded?
parent_entry.child_entries.sort_by { |entry| [ entry.created_at, entry.id ] }
else
- parent_entry.child_entries.order(:created_at, :id).to_a
+ parent_entry.child_entries.in_split_order.to_a
end
end
diff --git a/app/views/transactions/show.html.erb b/app/views/transactions/show.html.erb
index c121f709cd..a70bb5a594 100644
--- a/app/views/transactions/show.html.erb
+++ b/app/views/transactions/show.html.erb
@@ -204,7 +204,7 @@
<% dialog.with_section(title: t("splits.show.title"), open: true) do %>
<div class="px-3 py-2 space-y-2">
<p class="text-xs text-secondary"><%= t("splits.show.description") %></p>
- <% @entry.child_entries.includes(:entryable).each do |child| %>
+ <% @entry.child_entries.in_split_order.includes(:entryable).each do |child| %>
<div class="flex items-center justify-between p-2 rounded-lg border border-primary">
<div>
<p class="text-sm font-medium text-primary"><%= child.name %></p>
diff --git a/test/controllers/splits_controller_test.rb b/test/controllers/splits_controller_test.rb
index b639047236..f1d7d6fa54 100644
--- a/test/controllers/splits_controller_test.rb
+++ b/test/controllers/splits_controller_test.rb
@@ -227,6 +227,24 @@ class SplitsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end
+ test "edit lists split rows in the order they were entered" do
+ @entry.split!([
+ { name: "Part 1", amount: 60, category_id: nil },
+ { name: "Part 2", amount: 40, category_id: nil }
+ ])
+
+ queries = sql_queries { get edit_transaction_split_path(@entry) }
+ assert_response :success
+ # Without an ORDER BY PostgreSQL returns the rows in physical order.
+ row_reads = queries.select { |sql| sql.include?('"entries"."parent_entry_id" =') && !sql.include?("SELECT 1 AS one") }
+ assert row_reads.any?, "expected the split rows to be read"
+ assert row_reads.all? { |sql| sql.include?('ORDER BY "entries"."created_at" ASC, "entries"."id" ASC') }, row_reads.join("\n")
+
+ doc = Nokogiri::HTML::Document.parse(response.body)
+ names = doc.css("[data-split-transaction-target='rowsContainer'] input[name$='[name]']").map { |input| input["value"] }
+ assert_equal [ "Part 1", "Part 2" ], names
+ end
+
test "edit on a child redirects to parent edit" do
@entry.split!([
{ name: "Part 1", amount: 60, category_id: nil },
@@ -346,4 +364,11 @@ class SplitsControllerTest < ActionDispatch::IntegrationTest
assert_equal I18n.t("splits.destroy.success"), flash[:notice]
refute @entry.reload.excluded?
end
+
+ private
+ def sql_queries(&block)
+ queries = []
+ ActiveSupport::Notifications.subscribed(->(*, payload) { queries << payload[:sql] }, "sql.active_record", &block)
+ queries
+ end
end
diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rb
index 29e58c15da..2292db5da0 100644
--- a/test/controllers/transactions_controller_test.rb
+++ b/test/controllers/transactions_controller_test.rb
@@ -22,6 +22,24 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
assert_select "input[type=checkbox][name='q[merchants][]']", count: 0
end
+ test "show lists split rows in the order they were entered" do
+ entry = create_transaction(amount: 100, name: "Split parent", account: accounts(:depository))
+ entry.split!([ { name: "Part 1", amount: 60 }, { name: "Part 2", amount: 40 } ])
+
+ queries = []
+ ActiveSupport::Notifications.subscribed(->(*, payload) { queries << payload[:sql] }, "sql.active_record") do
+ get transaction_url(entry), headers: { "Turbo-Frame" => "drawer" }
+ end
+
+ assert_response :success
+ # Without an ORDER BY PostgreSQL returns the rows in physical order.
+ row_reads = queries.select { |sql| sql.include?('"entries"."parent_entry_id" =') && !sql.include?("SELECT 1 AS one") }
+ assert row_reads.any?, "expected the split rows to be read"
+ assert row_reads.all? { |sql| sql.include?('ORDER BY "entries"."created_at" ASC, "entries"."id" ASC') }, row_reads.join("\n")
+ names = css_select("p.font-medium").map(&:text).select { |text| text.start_with?("Part ") }
+ assert_equal [ "Part 1", "Part 2" ], names
+ end
+
test "a German transaction shows the bill it paid with localized copy" do
@user.update!(locale: "de")
@user.update!(preferences: (@user.preferences || {}).merge("preview_features_enabled" => true))