-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Preserve order when generating CSV #3575
base: master
Are you sure you want to change the base?
Conversation
end | ||
end | ||
|
||
context 'when objects are ordered' do |
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.
If this is okay to you, please also consider matching whole records. This will make sure of order even for other code changes.
context 'when objects are ordered' do
# or use before block as before
let!(players) do
FactoryBot.create_list :player, 2 do |player, index|
player.name = "Player #{index}"
end
end
let(:objects) { Player.all.order(name: :desc) }
let(:options) { {} }
# modify the subject block to return the array of objects
it 'preserves the ordering' do
expect(subject[2].split("\n")[1]).to eq('Player 1', 'Player 2')
end
end
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.
Thanks. I incorporated some of your suggestions, but matching the whole record is a bit nontrivial because the pk ids are not predictable when randomizing test seed
8f5bf70
to
7dfb730
Compare
I'm still looking for feedback on this. I don't mind keeping it up to date and passing CI, but I would appreciate some guidance on whether the change would be accepted at all. |
Resolves #2580
Generate CSVs using kaminari pagination when available (e.g., when
@objects
is an ActiveRecord_Relation) instead of using the native rails find_each.This allows us to preserve the activerecord ORDER BY clause defined when browsing records before attempting to export them;
find_each
overrides the ORDER BY to use the primary key id.One potential downside to this is that it will make certain large exports much less efficient, as paginating large tables ordered by a column without the correct database indexes will run into algorithmic problems.
Issue reproduction:
EXPECTED:
ACTUAL: