Active Admin: sorting on multiple columns

Bastien Léonard

Active Admin doesn't seem to support multiple columns sorting yet (i.e. pass multiple values to the config.sortable option). I saw an old monkey patch here but it doesn't seem to work with my version (1.0.0.pre from Github).

Is there any way to get multiple sortable columns on the latest Active Admin version?

nistvan

This is also a monkey patch:

Create a new file in config/initializers or in the lib folder: multiple_columns_sorting.rb

module ActiveAdmin
  class ResourceController < BaseController
    module DataAccess
      def apply_sorting(chain)
        params[:order] ||= active_admin_config.sort_order

        orders = []
        params[:order].split('_and_').each do |fragment|
          order_clause = OrderClause.new fragment
          if order_clause.valid?
            orders << order_clause.to_sql(active_admin_config)
          end
        end

        if orders.empty?
          chain
        else
          chain.reorder(orders.shift).order(orders)
        end
      end
    end
  end
end

Restart the server. Now you can use several columns name separeted by "_and_". For example:

config.sort_order = 'first_name_desc_and_last_name_asc'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related