Skip to content

Instantly share code, notes, and snippets.

@anhkind
Created May 21, 2015 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anhkind/5e9d849ebe4f3a452e31 to your computer and use it in GitHub Desktop.
Save anhkind/5e9d849ebe4f3a452e31 to your computer and use it in GitHub Desktop.
Monkey patching to ActiveAdmin (v0.6.0) to have multiple column sorting (config.sort_order = "column1_desc, column2_asc")
# initializers/active_admin.rb
module ActiveAdmin
class ResourceController
module DataAccess
# monkey patch to have multiple column sorting as "column1_asc, column2_desc"
def apply_sorting(chain)
params[:order] ||= active_admin_config.sort_order
orders = []
params[:order].present? && params[:order].split(/\s*,\s*/).each do |fragment|
fragment =~ /^([\w\_\.]+)_(desc|asc)$/
column = $1
order = $2
table = active_admin_config.resource_column_names.include?(column) ? active_admin_config.resource_table_name : nil
table_column = (column =~ /\./) ? column :
[table, active_admin_config.resource_quoted_column_name(column)].compact.join(".")
orders << "#{table_column} #{order}"
end
if orders.empty?
chain
else
chain.reorder(orders.shift).order(orders)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment