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.



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

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.



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

def active_admin_config
  @active_admin_config
end

#fieldObject (readonly)

Returns the value of attribute field.



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

def field
  @field
end

#orderObject (readonly)

Returns the value of attribute order.



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

def order
  @order
end

Instance Method Details

#apply(chain) ⇒ Object



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

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

#sqlObject



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

def sql
  custom_sql || to_sql
end

#tableObject



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

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

#table_columnObject



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

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

#to_sqlObject



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

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

#valid?Boolean

Returns:

  • (Boolean)


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

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