Skip to content

Quote table_ and column_name for sorting#3652

Merged
mshibuya merged 5 commits into
railsadminteam:masterfrom
rnestler:fix/1631-quote-column-names
Feb 26, 2024
Merged

Quote table_ and column_name for sorting#3652
mshibuya merged 5 commits into
railsadminteam:masterfrom
rnestler:fix/1631-quote-column-names

Conversation

@rnestler

Copy link
Copy Markdown
Contributor

Fixes #1631.

@rnestler rnestler force-pushed the fix/1631-quote-column-names branch 4 times, most recently from f5f7d8a to 51cf5a4 Compare October 13, 2023 13:52
Comment on lines -76 to +83
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to eq(sort: 'players.team_id', sort_reverse: true)
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to match(sort: /.?players.?\..?team_id.?/, sort_reverse: true)
end
end

context 'using active_record, supporting joins', active_record: true do
it 'gives back the local column' do
controller.params = {sort: 'team', model_name: 'players'}
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to eq(sort: 'teams.name', sort_reverse: true)
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to match(sort: /.?teams.?\..?name.?/, sort_reverse: true)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe I should also add a regression spec with a model with capitalized fields? Where would I put it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Somewhere around here will be good, as this fix is about #get_sort_hash.
Also you can create RDBMS-specific test cases like:

case connection_config[:adapter]
when 'postgresql'
@connection = ActiveRecord::Base.connection.instance_variable_get(:@connection)
@connection.set_client_encoding('latin1')
when 'mysql2'
ActiveRecord::Base.connection.execute('SET NAMES latin1;')
end

@rnestler

Copy link
Copy Markdown
Contributor Author

Any update on this?

@mshibuya mshibuya left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR! I've made some comments.

Comment thread lib/rails_admin/abstract_model.rb Outdated
Comment on lines -76 to +83
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to eq(sort: 'players.team_id', sort_reverse: true)
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to match(sort: /.?players.?\..?team_id.?/, sort_reverse: true)
end
end

context 'using active_record, supporting joins', active_record: true do
it 'gives back the local column' do
controller.params = {sort: 'team', model_name: 'players'}
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to eq(sort: 'teams.name', sort_reverse: true)
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to match(sort: /.?teams.?\..?name.?/, sort_reverse: true)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Somewhere around here will be good, as this fix is about #get_sort_hash.
Also you can create RDBMS-specific test cases like:

case connection_config[:adapter]
when 'postgresql'
@connection = ActiveRecord::Base.connection.instance_variable_get(:@connection)
@connection.set_client_encoding('latin1')
when 'mysql2'
ActiveRecord::Base.connection.execute('SET NAMES latin1;')
end

@rnestler rnestler force-pushed the fix/1631-quote-column-names branch from 51cf5a4 to 68299c0 Compare October 30, 2023 17:00
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 96.021% (-0.02%) from 96.037% when pulling 68299c0 on rnestler:fix/1631-quote-column-names into 13c90fd on railsadminteam:master.

@mshibuya mshibuya merged commit b8155f8 into railsadminteam:master Feb 26, 2024
@mshibuya

Copy link
Copy Markdown
Member

Great job, thank you!

@rnestler rnestler deleted the fix/1631-quote-column-names branch February 26, 2024 09:51
@rnestler

Copy link
Copy Markdown
Contributor Author

Ah sorry I forgot about that. I think I didn't add the regression test or RDBMS-specific test cases.

@mshibuya

Copy link
Copy Markdown
Member

I'll handle that when I have time 👍

mshibuya added a commit that referenced this pull request Mar 2, 2024
Add a test case to ensure the table and column names are quoted
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.

Table names don't always get properly quoted

3 participants