Class: ActiveAdmin::OrderClause

Inherits:
Object
  • Object
show all
Defined in:
lib/active_admin/order_clause.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(active_admin_config, clause) ⇒ OrderClause

Returns a new instance of OrderClause.


5
6
7
8
9
10
11
12
# File 'lib/active_admin/order_clause.rb', line 5

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

Instance Attribute Details

#active_admin_configObject (readonly)

Returns the value of attribute active_admin_config


3
4
5
# File 'lib/active_admin/order_clause.rb', line 3

def active_admin_config
  @active_admin_config
end

#fieldObject (readonly)

Returns the value of attribute field


3
4
5
# File 'lib/active_admin/order_clause.rb', line 3

def field
  @field
end

#orderObject (readonly)

Returns the value of attribute order


3
4
5
# File 'lib/active_admin/order_clause.rb', line 3

def order
  @order
end

Instance Method Details

#apply(chain) ⇒ Object


18
19
20
# File 'lib/active_admin/order_clause.rb', line 18

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

#custom_sqlObject (protected)


41
42
43
44
45
# File 'lib/active_admin/order_clause.rb', line 41

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

#sqlObject


35
36
37
# File 'lib/active_admin/order_clause.rb', line 35

def sql
  custom_sql || to_sql
end

#tableObject


26
27
28
# File 'lib/active_admin/order_clause.rb', line 26

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

#table_columnObject


30
31
32
33
# File 'lib/active_admin/order_clause.rb', line 30

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

#to_sqlObject


22
23
24
# File 'lib/active_admin/order_clause.rb', line 22

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

#valid?Boolean

Returns:

  • (Boolean)

14
15
16
# File 'lib/active_admin/order_clause.rb', line 14

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