Skip to content

Commit

Permalink
Follow-up for #3652
Browse files Browse the repository at this point in the history
Add a test case to ensure the table and column names are quoted
  • Loading branch information
mshibuya committed Mar 2, 2024
1 parent 9ecfa42 commit d1ae021
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions spec/controllers/rails_admin/main_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,35 @@ def get(action, params)
expect(controller.send(:get_sort_hash, RailsAdmin.config(Category))).to eq(sort: 'categories.parent_category_id', sort_reverse: true)
end

context 'using mongoid, not supporting joins', mongoid: true do
it 'gives back the remote table with label name' do
context 'using mongoid', mongoid: true do
it 'gives back the remote table with label name, as it does not support joins' do
controller.params = {sort: 'team', model_name: 'players'}
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to match(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
context 'using active_record', active_record: true do
let(:connection_config) do
if ActiveRecord::Base.respond_to?(:connection_db_config)
ActiveRecord::Base.connection_db_config.configuration_hash
else
ActiveRecord::Base.connection_config
end
end

it 'gives back the local column' do
controller.params = {sort: 'team', model_name: 'players'}
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to match(sort: /.?teams.?\..?name.?/, sort_reverse: true)
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to match(sort: /^["`]teams["`]\.["`]name["`]$/, sort_reverse: true)
end

it 'quotes the table and column names it returns as :sort' do
controller.params = {sort: 'team', model_name: 'players'}
case connection_config[:adapter]
when 'mysql2'
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))[:sort]).to eq '`teams`.`name`'
else
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))[:sort]).to eq '"teams"."name"'
end
end
end
end
Expand Down

0 comments on commit d1ae021

Please sign in to comment.