Created
May 21, 2015 13:04
-
-
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")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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