Skip to content

Commit 6b5bc26

Browse files
Merge pull request #5656 from pcbeingused333/5593-alphabetize-participant-dropdowns
Alphabetize the Product Drive Participant drop-downs (#5593)
2 parents d6f8290 + 6361fe3 commit 6b5bc26

10 files changed

Lines changed: 87 additions & 5 deletions

File tree

‎Gemfile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ gem "paper_trail"
3333
gem "rolify", "~> 6.0"
3434
# Enforces "safe" migrations.
3535
gem "strong_migrations"
36+
# Manages Postgres functions and triggers as versioned files.
37+
gem "fx"
3638
# used in events
3739
gem 'dry-struct'
3840
# Use solid_cache as a cache store

‎Gemfile.lock‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ GEM
303303
foreman (0.90.0)
304304
thor (~> 1.4)
305305
formatador (1.1.0)
306+
fx (0.11.0)
307+
activerecord (>= 7.2)
308+
railties (>= 7.2)
306309
geocoder (1.8.6)
307310
base64 (>= 0.1.0)
308311
csv (>= 3.0.0)
@@ -804,6 +807,7 @@ DEPENDENCIES
804807
flipper-active_record
805808
flipper-ui
806809
foreman
810+
fx
807811
geocoder
808812
guard-rspec
809813
icalendar

‎app/models/product_drive_participant.rb‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,22 @@ class ProductDriveParticipant < ApplicationRecord
3030
validates :business_name, presence: { message: "Must provide a name or a business name" }, if: proc { |pdp| pdp.contact_name.blank? }
3131
validates :comment, length: { maximum: 500 }
3232

33-
scope :alphabetized, -> { order(:contact_name) }
33+
# Orders on the name the drop-downs actually show - `display_name`, which is
34+
# `business_name` falling back to `contact_name` - rather than on the database
35+
# collation, which is not the same everywhere: a `C.UTF-8` cluster puts every
36+
# capitalised name before every lowercase one and the `en_US.utf8` image CI
37+
# runs does not.
38+
#
39+
# `leading_digit_sort_key` (db/functions/leading_digit_sort_key_v01.sql, added
40+
# via the `fx` gem) zero-pads a leading run of digits so "2" sorts before
41+
# "10" - it does not natural-sort a digit run in the middle of the name. That
42+
# scope was the deliberate tradeoff, over a fuller tokenizing version, agreed
43+
# on in https://github.com/rubyforgood/human-essentials/pull/5656.
44+
DISPLAY_NAME_ORDER = Arel.sql(
45+
"leading_digit_sort_key(coalesce(NULLIF(business_name, ''), contact_name, ''))"
46+
)
47+
48+
scope :alphabetized, -> { order(DISPLAY_NAME_ORDER) }
3449
scope :by_business_name, ->(business_name) { where("business_name ILIKE ?", "%#{business_name}%") }
3550
scope :by_contact_name, ->(contact_name) { where("contact_name ILIKE ?", "%#{contact_name}%") }
3651
scope :with_volumes, -> {

‎app/views/donations/_donation_form.html.erb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
collection: @product_drive_participants,
4444
selected: donation_form.product_drive_participant_id,
4545
include_blank: true,
46-
label_method: lambda { |x| "#{x.try(:business_name).presence || x.try(:contact_name)}" },
46+
label_method: :display_name,
4747
label: "Product Drive Participant",
4848
error: "Which product drive participant was this from?",
4949
wrapper: :input_group %>

‎app/views/donations/index.html.erb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<div class="form-group col-lg-3 col-md-4 col-sm-6 col-xs-12">
6666
<%= filter_select(scope: :by_product_drive_participant,
6767
collection: @donation_info.product_drive_participants,
68-
value: :business_name,
68+
value: :display_name,
6969
selected: @donation_info.selected_product_drive_participant) %>
7070
</div>
7171
<% end %>

‎app/views/product_drive_participants/create.js.erb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
$("#modal_new").modal("hide");
33
$("#donation_product_drive_participant_id").empty();
44
$("#donation_product_drive_participant_id").
5-
html('<%= j options_from_collection_for_select(current_organization.product_drive_participants, :id, lambda { |p| p.business_name.present? ? p.business_name : p.contact_name }) %>');
5+
html('<%= j options_from_collection_for_select(current_organization.product_drive_participants.alphabetized, :id, :display_name) %>');
66
$("#donation_product_drive_participant_id").append('<option value="">---Create new Participant---</option>');
77
$("#donation_product_drive_participant_id").val('<%= @product_drive_participant[:id] %>');
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Sort key that treats a run of digits *at the start* of the string as a
2+
-- number instead of a sequence of characters, so "2" sorts before "10".
3+
-- Anything past the leading digits (or the whole string, if it doesn't start
4+
-- with a digit) is compared as plain lowercased text - a digit run elsewhere
5+
-- in the string (e.g. "Store 9" vs. "Store 10") is not natural-sorted. That
6+
-- narrower scope, instead of full natural sort, was the deliberate tradeoff
7+
-- settled on in https://github.com/rubyforgood/human-essentials/pull/5656.
8+
CREATE FUNCTION leading_digit_sort_key(value text) RETURNS text AS $$
9+
SELECT CASE
10+
WHEN lower(coalesce(value, '')) ~ '^[0-9]+'
11+
THEN lpad(substring(lower(value) from '^[0-9]+'), 20, '0')
12+
|| substring(lower(value) from '^[0-9]+(.*)$')
13+
ELSE lower(coalesce(value, ''))
14+
END;
15+
$$ LANGUAGE sql IMMUTABLE PARALLEL SAFE;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class CreateFunctionLeadingDigitSortKey < ActiveRecord::Migration[8.1]
2+
def change
3+
create_function :leading_digit_sort_key
4+
end
5+
end

‎db/schema.rb‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.1].define(version: 2026_08_29_112930) do
13+
ActiveRecord::Schema[8.1].define(version: 2026_09_21_234706) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "pg_catalog.plpgsql"
1616

@@ -898,4 +898,19 @@
898898
add_foreign_key "tags", "organizations"
899899
add_foreign_key "units", "organizations"
900900
add_foreign_key "users", "users_roles", column: "last_role_id", on_delete: :nullify
901+
902+
create_function :leading_digit_sort_key, sql_definition: <<-'SQL'
903+
CREATE OR REPLACE FUNCTION public.leading_digit_sort_key(value text)
904+
RETURNS text
905+
LANGUAGE sql
906+
IMMUTABLE PARALLEL SAFE
907+
AS $function$
908+
SELECT CASE
909+
WHEN lower(coalesce(value, '')) ~ '^[0-9]+'
910+
THEN lpad(substring(lower(value) from '^[0-9]+'), 20, '0')
911+
|| substring(lower(value) from '^[0-9]+(.*)$')
912+
ELSE lower(coalesce(value, ''))
913+
END;
914+
$function$
915+
SQL
901916
end

‎spec/models/product_drive_participant_spec.rb‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@
7171
expect(ProductDriveParticipant.by_contact_name("Shellstrop")).to match_array([eleanor, donna])
7272
end
7373
end
74+
75+
describe ".alphabetized" do
76+
it "orders by the name that is displayed, falling back to contact name" do
77+
zebra = create(:product_drive_participant, business_name: "Zebra Foods", contact_name: "adam")
78+
no_business = create(:product_drive_participant, business_name: nil, contact_name: "molly")
79+
aardvark = create(:product_drive_participant, business_name: "Aardvark Supplies", contact_name: "zoe")
80+
81+
expect(ProductDriveParticipant.alphabetized).to eq([aardvark, no_business, zebra])
82+
end
83+
84+
it "orders a leading number by value rather than by digit" do
85+
tenth = create(:product_drive_participant, business_name: "10 Warehouse Way")
86+
second = create(:product_drive_participant, business_name: "2 Warehouse Way")
87+
ninth = create(:product_drive_participant, business_name: "9 Warehouse Way")
88+
89+
expect(ProductDriveParticipant.alphabetized).to eq([second, ninth, tenth])
90+
end
91+
92+
it "does not natural-sort a number in the middle of the name (leading-digit only, by design - see PR #5656)" do
93+
tenth = create(:product_drive_participant, business_name: "Store 10")
94+
second = create(:product_drive_participant, business_name: "Store 2")
95+
ninth = create(:product_drive_participant, business_name: "Store 9")
96+
97+
expect(ProductDriveParticipant.alphabetized).to eq([tenth, second, ninth])
98+
end
99+
end
74100
end
75101

76102
context "Methods" do

0 commit comments

Comments
 (0)