@@ -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 , -> {
0 commit comments