def has_sort_scope(config = {})
      @@_sort_scope_id ||= 0
      default = config[:default] || :id
      default = default.is_a?(Hash) ? default.stringify_keys : default.to_s
      allowed = Array.wrap(config[:by]).map(&:to_s)
      only_actions = config.fetch(:only, :index)
      order_method = config.fetch(:order_method, :order)
      # Counter added into scope name to allow to define multiple scopes in same controller.
      has_scope("sort_#{@@_sort_scope_id += 1}",
        as:           :sort,
        default:      nil,
        allow_blank:  true,
        only:         only_actions,
        type:         :any,
      ) do |c, scope, val|
        sort_args = SortScope.filter_param(val, c.params, allowed, default)
        c.instance_variable_set(:@current_sort_scope, sort_args)
        scope.public_send(order_method, sort_args)
      end
    end
def has_sort_scope(config = {})
      @@_sort_scope_id ||= 0
      default = config[:default] || :id
      default = default.is_a?(Hash) ? default.stringify_keys : default.to_s
      allowed = Array.wrap(config[:by]).map(&:to_s)
      only_actions = config.fetch(:only, :index)
      order_method = config.fetch(:order_method, :order)
      # Counter added into scope name to allow to define multiple scopes in same controller.
      has_scope("sort_#{@@_sort_scope_id += 1}",
        as:           :sort,
        default:      nil,
        allow_blank:  true,
        only:         only_actions,
        type:         :any,
      ) do |c, scope, val|
        sort_args = SortScope.filter_param(val, c.params, allowed, default)
        c.instance_variable_set(:@current_sort_scope, sort_args)
        scope.public_send(order_method, sort_args)
      end
    end
Setups has_scope to order collection by allowed columns.
 Sort column is filtered by SortScope.filter_param method.
 Accepts params:

 - `sort=name`
 - `sort=name&sort_desc=true`
 - `sort[name]&sort[order]`
 - `sort[name]&sort[order]=desc

 #### Options

 - `by` - array of available fields to sort by,
 - `default` - default sort expression,
 - `only` - bypassed to `has_scope` to limit actions (default to `:index`),
 - `order_method` - use custom method to sort instead of `.order`.

 rubocop:disable ClassVars