Sha256: b566b10a670d7662a03cd36763d561f72cfb115d51172c4a4cc556c22e12374c

Contents?: true

Size: 1.04 KB

Versions: 12

Compression:

Stored size: 1.04 KB

Contents

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(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

12 entries across 12 versions & 2 rubygems

Version Path
activeadmin_addons-1.7.1 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/order_clause.rb
activeadmin_addons-1.7.0 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/order_clause.rb
activeadmin_addons-1.6.0 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/order_clause.rb
activeadmin_addons-1.5.0 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/order_clause.rb
activeadmin_addons-1.4.0 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/order_clause.rb
activeadmin_addons-1.3.0 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/order_clause.rb
activeadmin_addons-1.2.0 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/order_clause.rb
activeadmin-1.2.1 lib/active_admin/order_clause.rb
activeadmin-1.2.0 lib/active_admin/order_clause.rb
activeadmin-1.1.0 lib/active_admin/order_clause.rb
activeadmin-1.0.0 lib/active_admin/order_clause.rb
activeadmin-1.0.0.pre5 lib/active_admin/order_clause.rb