Sha256: 7509a9858b8d6dc3d67b5c9dd4078c6477fc23fe8bd0705a8b4141ae0c5edb03

Contents?: true

Size: 1.07 KB

Versions: 17

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true
module ActiveAdmin
  class OrderClause
    attr_reader :field, :order, :active_admin_config

    def initialize(active_admin_config, clause)
      clause =~ /^([\w\.]+)(->'\w+')?_(desc|asc)$/
      @column = $1
      @op = $2
      @order = $3
      @active_admin_config = active_admin_config
      @field = [@column, @op].compact.join
    end

    def valid?
      @field.present? && @order.present?
    end

    def apply(chain)
      chain.reorder(Arel.sql sql)
    end

    def to_sql
      [table_column, @op, " ", @order].compact.join
    end

    def table
      active_admin_config.resource_column_names.include?(@column) ? active_admin_config.resource_table_name : nil
    end

    def table_column
      (@column =~ /\./) ? @column :
        [table, active_admin_config.resource_quoted_column_name(@column)].compact.join(".")
    end

    def sql
      custom_sql || to_sql
    end

    protected

    def custom_sql
      if active_admin_config.ordering[@column].present?
        active_admin_config.ordering[@column].call(self)
      end
    end

  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
activeadmin-3.2.5 lib/active_admin/order_clause.rb
activeadmin-3.2.4 lib/active_admin/order_clause.rb
activeadmin-3.2.3 lib/active_admin/order_clause.rb
activeadmin-3.2.2 lib/active_admin/order_clause.rb
activeadmin-3.2.1 lib/active_admin/order_clause.rb
activeadmin-3.2.0 lib/active_admin/order_clause.rb
activeadmin-3.1.0 lib/active_admin/order_clause.rb
activeadmin-3.0.0 lib/active_admin/order_clause.rb
activeadmin-2.14.0 lib/active_admin/order_clause.rb
activeadmin-2.13.1 lib/active_admin/order_clause.rb
activeadmin-2.13.0 lib/active_admin/order_clause.rb
activeadmin-2.12.0 lib/active_admin/order_clause.rb
activeadmin-2.11.2 lib/active_admin/order_clause.rb
activeadmin-2.11.1 lib/active_admin/order_clause.rb
activeadmin-2.11.0 lib/active_admin/order_clause.rb
activeadmin-2.10.1 lib/active_admin/order_clause.rb
activeadmin-2.10.0 lib/active_admin/order_clause.rb