Skip to content

Commit

Permalink
Follow-up for #3509
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed May 4, 2022
1 parent 2bece45 commit 9959925
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
14 changes: 2 additions & 12 deletions app/controllers/rails_admin/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def back_or_index
params[:return_to].presence && params[:return_to].include?(request.host) && (params[:return_to] != request.fullpath) ? params[:return_to] : index_path
end

# rubocop:disable Metrics/CyclomaticComplexity
def get_sort_hash(model_config)
abstract_model = model_config.abstract_model
field = model_config.list.fields.detect { |f| f.name.to_s == params[:sort] }
Expand All @@ -69,22 +68,13 @@ def get_sort_hash(model_config)
column =
if field.nil? || field.sortable == false # use default sort, asked field does not exist or is not sortable
"#{abstract_model.table_name}.#{model_config.list.sort_by}"
elsif field.sortable == true # use the given field
"#{abstract_model.table_name}.#{field.name}"
elsif (field.sortable.is_a?(String) || field.sortable.is_a?(Symbol)) && field.sortable.to_s.include?('.') # just provide sortable, don't do anything smart
field.sortable
elsif field.sortable.is_a?(Hash) # just join sortable hash, don't do anything smart
"#{field.sortable.keys.first}.#{field.sortable.values.first}"
elsif field.association? # use column on target table
"#{field.associated_model_config.abstract_model.table_name}.#{field.sortable}"
else # use described column in the field conf.
"#{abstract_model.table_name}.#{field.sortable}"
else
field.sort_column
end

params[:sort_reverse] ||= 'false'
{sort: column, sort_reverse: (params[:sort_reverse] == (field&.sort_reverse&.to_s || 'true'))}
end
# rubocop:enable Metrics/CyclomaticComplexity

def redirect_to_on_success
notice = I18n.t('admin.flash.successful', name: @model_config.label, action: I18n.t("admin.actions.#{@action.key}.done"))
Expand Down
14 changes: 14 additions & 0 deletions lib/rails_admin/config/fields/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ def virtual?
!virtual? || children_fields.first || false
end

def sort_column
if sortable == true
"#{abstract_model.table_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}"
else # use described column in the field conf.
"#{abstract_model.table_name}.#{sortable}"
end
end

register_instance_option :searchable do
!virtual? || children_fields.first || false
end
Expand Down

0 comments on commit 9959925

Please sign in to comment.