Skip to content
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

Quote table_ and column_name for sorting #3652

Merged
merged 5 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines -76 to +83
Copy link
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
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

end
end
end
Expand Down