-
-
Notifications
You must be signed in to change notification settings - Fork 593
Alphabetize the Product Drive Participant drop-downs (#5593) #5656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,7 +30,27 @@ class ProductDriveParticipant < ApplicationRecord | |
| validates :business_name, presence: { message: "Must provide a name or a business name" }, if: proc { |pdp| pdp.contact_name.blank? } | ||
| validates :comment, length: { maximum: 500 } | ||
|
|
||
| scope :alphabetized, -> { order(:contact_name) } | ||
| # Orders on the name the drop-downs actually show - `display_name`, which is | ||
| # `business_name` falling back to `contact_name` - rather than on the database | ||
| # collation, which is not the same everywhere: a `C.UTF-8` cluster puts every | ||
| # capitalised name before every lowercase one and the `en_US.utf8` image CI | ||
| # runs does not. Runs of digits are zero padded so that they compare by value | ||
| # and "Store 9" comes before "Store 10". | ||
| # | ||
| # Written as a literal because the schema is maintained as `schema.rb`, which | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like adding a Postgres function with something like this would be more generally useful. We know we want to apply this ordering to other places. You can look at something like fx to handle it.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed that the Postgres solution is better long term, do we want a step in the right direction first? What's the LOE for the optimal shared function in Postgres?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LOE for the two versions is genuinely different, which is why I'd frame this as staged:
Given that gap: this PR as the interim step (Ruby-level, already green at 11/11), then the Postgres function once the scope question in the other thread is settled. I can start the full version now if that's the target, or the leading-digit one if that's enough. |
||
| # carries neither a Postgres function nor an ICU collation - both would | ||
| # disappear on `db:schema:load`. | ||
| DISPLAY_NAME_ORDER = Arel.sql(<<~SQL.squish) | ||
| (SELECT string_agg( | ||
| CASE WHEN chunk[1] ~ '^[0-9]' THEN lpad(chunk[1], 20, '0') ELSE chunk[1] END, | ||
| '' ORDER BY idx) | ||
| FROM regexp_matches( | ||
| lower(coalesce(NULLIF(business_name, ''), contact_name, '')), | ||
| '[0-9]+|[^0-9]+', 'g') | ||
| WITH ORDINALITY AS chunks(chunk, idx)) | ||
| SQL | ||
|
|
||
| scope :alphabetized, -> { order(DISPLAY_NAME_ORDER) } | ||
| scope :by_business_name, ->(business_name) { where("business_name ILIKE ?", "%#{business_name}%") } | ||
| scope :by_contact_name, ->(contact_name) { where("contact_name ILIKE ?", "%#{contact_name}%") } | ||
| scope :with_volumes, -> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that was part of the original requirements (only if the string starts with a number, not if there's a number in the middle of the string). @ruestitch thoughts? That might simplify this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From reviewing this a few times with others, the solution seems like it should work for natural sorting
Agree, store 9 should come before store 10, requiring number and letter sort to be distinct, and to sort numerically in cases of numbers
BUT this is complicated for delivering a sustainable solution across all use cases, which may include alphanumeric and instances of 2nd, 4th type entries, so we should compare this value add to the long view of the Postgres solution you mentioned, @dorner
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That matches what I'd propose: scope this to what the current data actually needs rather than full generality (alphanumeric, "2nd"/"4th", etc.), since there's no evidence any of that appears in
business_nametoday — only the digit-run case ("Store 9" vs "Store 10") is covered by the existing specs. If mid-name digit runs turn out to be the only real case, that's also the simpler of the two Postgres options in the other thread — happy to build to whichever scope you land on.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @ruestitch not sure I understood your comment - are you saying that "Store 9" should come before "Store 10"? Because in that case the number is in the middle of the string, not the beginning, which means the first Postgres solution will not work.