Skip to content

Commit

Permalink
Quote table_ and column_name for sorting (#3652)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rnestler committed Feb 26, 2024
1 parent c6a4036 commit b8155f8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
8 changes: 8 additions & 0 deletions lib/rails_admin/abstract_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def model
@model_name.constantize
end

def quoted_table_name
table_name
end

def quote_column_name(name)
name
end

def to_s
model.to_s
end
Expand Down
8 changes: 8 additions & 0 deletions lib/rails_admin/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ def base_class

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

def quoted_table_name
model.quoted_table_name
end

def quote_column_name(name)
model.connection.quote_column_name(name)
end

def encoding
adapter =
if ::ActiveRecord::Base.respond_to?(:connection_db_config)
Expand Down
6 changes: 3 additions & 3 deletions lib/rails_admin/config/fields/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ def virtual?

def sort_column
if sortable == true
"#{abstract_model.table_name}.#{name}"
"#{abstract_model.quoted_table_name}.#{abstract_model.quote_column_name(name)}"
elsif (sortable.is_a?(String) || sortable.is_a?(Symbol)) && sortable.to_s.include?('.') # just provide sortable, don't do anything smart
sortable
elsif sortable.is_a?(Hash) # just join sortable hash, don't do anything smart
"#{sortable.keys.first}.#{sortable.values.first}"
elsif association # use column on target table
"#{associated_model_config.abstract_model.table_name}.#{sortable}"
"#{associated_model_config.abstract_model.quoted_table_name}.#{abstract_model.quote_column_name(sortable)}"
else # use described column in the field conf.
"#{abstract_model.table_name}.#{sortable}"
"#{abstract_model.quoted_table_name}.#{abstract_model.quote_column_name(sortable)}"
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/rails_admin/main_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ def get(action, params)
context 'using mongoid, not supporting joins', mongoid: true do
it 'gives back the remote table with label name' do
controller.params = {sort: 'team', model_name: 'players'}
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)
end
end
end
Expand Down

0 comments on commit b8155f8

Please sign in to comment.