Skip to content
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

#4593 Fix distribution export, item columns in alphabetically order #4617

Conversation

auliafaizahr
Copy link
Contributor

Checklist:

  • - I have performed a self-review of my own code,
  • - I have commented my code, particularly in hard-to-understand areas,
  • - I have made corresponding changes to the documentation,
  • - I have added tests that prove my fix is effective or that my feature works,
  • - New and existing unit tests pass locally with my changes ("bundle exec rake")

Resolves #4593

Description

Reorder item-related columns in the distribution export to be in lower-case alphabetical order.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Automated tests have been added to confirm that item-related columns are now exported in lower-case alphabetical order.
  • Manual testing was conducted by exporting distributions and verifying the order of columns.

Screenshots

before
image
image
image

after
image
image
image

as can we see in before image the last column was "Kit" after "Wipes" which "wipes" should be at last column if we order it alphabetically, which I try to fix to reorder it alphabetically and the result was in after images ("wipes" was in the last order)

At first I was confused because the title of the issue is "items columns are no longer in alphabetical order", but after dig deeper it was never in alphabetical order since first (found the discussion here #4070, it is said that it sorted by "created_at" column).

Look forward for feedback, thankyou.

@cielf
Copy link
Collaborator

cielf commented Aug 29, 2024

FYI The complaint from the business was that they were no longer in alpha order. That may be because the original items everyone starts out with happen to be in alpha order, but any they added would be showing at the end if they were in created order.

@auliafaizahr
Copy link
Contributor Author

ah I see, after seeing the header at first I also think it was already in alphabetical order unless seeing the last column, and after dig deeper I kinda confused bcs it was order by created_at, sorry for some inconvenience in my wording, thankyou.

@@ -118,7 +118,7 @@ def base_headers
def item_headers
return @item_headers if @item_headers

@item_headers = @organization.items.order(:created_at).distinct.select([:created_at, :name]).map(&:name)
@item_headers = @organization.items.select("DISTINCT ON (LOWER(name)) items.name").order("LOWER(name) ASC").map(&:name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure there's a more rails-y way to do this. I haven't tried it out but it might be:
@organization.items.order('lower(name)').distinct.select([:created_at, :name]).map(&:name)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try by put binding.pry on it, but my terminal said this, pls enlighten me if this is bcs of my machine env

image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok - so maybe I was wrong g . @awwaiid -- do you have a quick take on this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked a robot (Claude) and it suggested

@item_headers = @organization.items
  .select(:name)
  .group('LOWER(name)')
  .order('LOWER(name) ASC')
  .pluck('MIN(name)')

which isn't a direct translation but is actually sounds pretty good (it doesn't instantiate Item). mmm. OK, I asked it for something more direct and it gave

@item_headers = @organization.items
  .select('DISTINCT ON (LOWER(items.name)) items.name')
  .order('LOWER(items.name) ASC')
  .pluck(:name)

which is back to basically what you have now.

I think since there isn't really a direct ActiveRecord thing for distinct on like this that what you have is fine, though you should consider the .pluck(:name) instead of the .map(&:name)

Copy link
Contributor Author

@auliafaizahr auliafaizahr Sep 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

After running a benchmark (screenshot), it turns out that map is actually a bit faster than pluck, though the difference isn't huge. Is it still better to stick with pluck, or should we consider using map? Both perform well. Let me know what you think!

Copy link
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good!

@dorner dorner merged commit 5d854fe into rubyforgood:main Sep 2, 2024
19 checks passed
ewoknock pushed a commit to ewoknock/human-essentials that referenced this pull request Sep 3, 2024
Copy link
Contributor

github-actions bot commented Sep 8, 2024

@auliafaizahr: Your PR #4593 Fix distribution export, item columns in alphabetically order is part of today's Human Essentials production release: 2024.09.08.
Thank you very much for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Distribution export -- item columns are no longer in alphbetical order. Fix it.
4 participants