Sha256: 86f6fdf4f06ac2c278891f8ed516bbed746e90515f2436ef27a7cf80b5af3493

Contents?: true

Size: 1.11 KB

Versions: 14

Compression:

Stored size: 1.11 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
      if (@column.include?('.'))
        @column
      else
        [table, active_admin_config.resource_quoted_column_name(@column)].compact.join(".")
      end
    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

14 entries across 14 versions & 1 rubygems

Version Path
activeadmin-4.0.0.beta14 lib/active_admin/order_clause.rb
activeadmin-4.0.0.beta13 lib/active_admin/order_clause.rb
activeadmin-4.0.0.beta12 lib/active_admin/order_clause.rb
activeadmin-4.0.0.beta11 lib/active_admin/order_clause.rb
activeadmin-4.0.0.beta10 lib/active_admin/order_clause.rb
activeadmin-4.0.0.beta9 lib/active_admin/order_clause.rb
activeadmin-4.0.0.beta8 lib/active_admin/order_clause.rb
activeadmin-4.0.0.beta7 lib/active_admin/order_clause.rb
activeadmin-4.0.0.beta6 lib/active_admin/order_clause.rb
activeadmin-4.0.0.beta5 lib/active_admin/order_clause.rb
activeadmin-4.0.0.beta4 lib/active_admin/order_clause.rb
activeadmin-4.0.0.beta3 lib/active_admin/order_clause.rb
activeadmin-4.0.0.beta2 lib/active_admin/order_clause.rb
activeadmin-4.0.0.beta1 lib/active_admin/order_clause.rb