From 1fedd70fe59da3ab7fc93f09a597d018a75d8d46 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Wed, 27 Nov 2024 19:54:50 +0100 Subject: [PATCH] Fix defaults listing Base symbol was displayed twice when it existed as default and non-default and both of them had at least one subunit. Also: sorting by base_id yielded non-alphabetic order in such case. --- app/models/unit.rb | 13 +++++++++---- app/views/default/units/_unit.html.erb | 3 +-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/models/unit.rb b/app/models/unit.rb index 82fcd7f..fbb2c8e 100644 --- a/app/models/unit.rb +++ b/app/models/unit.rb @@ -61,18 +61,23 @@ class Unit < ApplicationRecord ) ).as('portable') ), - # NOTE: turn off ONLY_FULL_GROUP_BY or is it incompatible with other DBs? # Fill base Units to display proper hierarchy. Duplicates will be removed # by final group() - can't be deduplicated with UNION due to 'portable' field. arel_table.join(actionable_units).on(actionable_units[:base_id].eq(arel_table[:id])) .project(arel_table[Arel.star], Arel::Nodes.build_quoted(nil).as('portable')) - ]).select(units: column_names).select(units[:portable].minimum.as('portable')) - .from(units).group(Unit.column_names) + ]).select(units: [:base_id, :symbol]) + .select( + units[:id].minimum.as('id'), # can be ANY_VALUE() + units[:user_id].minimum.as('user_id'), # prefer non-default + Arel::Nodes.build_quoted(1).as('multiplier'), # disregard multiplier when sorting + units[:portable].minimum.as('portable') + ) + .from(units).group(:base_id, :symbol) } scope :ordered, ->{ left_outer_joins(:base) .order(arel_table.coalesce(Arel::Table.new(:bases_units)[:symbol], arel_table[:symbol]), - arel_table[:base_id].asc.nulls_first, :multiplier, :symbol) + arel_table[:base_id].not_eq(nil), :multiplier, :symbol) } before_destroy do diff --git a/app/views/default/units/_unit.html.erb b/app/views/default/units/_unit.html.erb index 8a4a6df..13c2d9e 100644 --- a/app/views/default/units/_unit.html.erb +++ b/app/views/default/units/_unit.html.erb @@ -1,6 +1,5 @@ <%= tag.tr do %> - + <%= unit %>