Skip to content

Commit b8155f8

Browse files
authored
Quote table_ and column_name for sorting (#3652)
* Quote table_ and column_name for sorting * Use quoted_table_name quote_column_name * Fix specs for sqlite3 and mysql2 * Only quote table/column names for active record * Replace conditional with specific code in active_record adapter
1 parent c6a4036 commit b8155f8

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

lib/rails_admin/abstract_model.rb

+8
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ def model
6060
@model_name.constantize
6161
end
6262

63+
def quoted_table_name
64+
table_name
65+
end
66+
67+
def quote_column_name(name)
68+
name
69+
end
70+
6371
def to_s
6472
model.to_s
6573
end

lib/rails_admin/adapters/active_record.rb

+8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ def base_class
7272

7373
delegate :primary_key, :table_name, to: :model, prefix: false
7474

75+
def quoted_table_name
76+
model.quoted_table_name
77+
end
78+
79+
def quote_column_name(name)
80+
model.connection.quote_column_name(name)
81+
end
82+
7583
def encoding
7684
adapter =
7785
if ::ActiveRecord::Base.respond_to?(:connection_db_config)

lib/rails_admin/config/fields/base.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ def virtual?
6262

6363
def sort_column
6464
if sortable == true
65-
"#{abstract_model.table_name}.#{name}"
65+
"#{abstract_model.quoted_table_name}.#{abstract_model.quote_column_name(name)}"
6666
elsif (sortable.is_a?(String) || sortable.is_a?(Symbol)) && sortable.to_s.include?('.') # just provide sortable, don't do anything smart
6767
sortable
6868
elsif sortable.is_a?(Hash) # just join sortable hash, don't do anything smart
6969
"#{sortable.keys.first}.#{sortable.values.first}"
7070
elsif association # use column on target table
71-
"#{associated_model_config.abstract_model.table_name}.#{sortable}"
71+
"#{associated_model_config.abstract_model.quoted_table_name}.#{abstract_model.quote_column_name(sortable)}"
7272
else # use described column in the field conf.
73-
"#{abstract_model.table_name}.#{sortable}"
73+
"#{abstract_model.quoted_table_name}.#{abstract_model.quote_column_name(sortable)}"
7474
end
7575
end
7676

spec/controllers/rails_admin/main_controller_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ def get(action, params)
7373
context 'using mongoid, not supporting joins', mongoid: true do
7474
it 'gives back the remote table with label name' do
7575
controller.params = {sort: 'team', model_name: 'players'}
76-
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to eq(sort: 'players.team_id', sort_reverse: true)
76+
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to match(sort: /.?players.?\..?team_id.?/, sort_reverse: true)
7777
end
7878
end
7979

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

0 commit comments

Comments
 (0)