Class: ActiveScaffold::DataStructures::Sorting

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_scaffold/data_structures/sorting.rb

Overview

encapsulates the column sorting configuration for the List view

Instance Method Summary collapse

Constructor Details

#initialize(columns) ⇒ Sorting

Returns a new instance of Sorting.



6
7
8
9
# File 'lib/active_scaffold/data_structures/sorting.rb', line 6

def initialize(columns)
  @columns = columns
  @clauses = []
end

Instance Method Details

#<<(arg) ⇒ Object

an alias for add. must accept its arguments in a slightly different form, though.



42
43
44
# File 'lib/active_scaffold/data_structures/sorting.rb', line 42

def <<(arg)
  add(*arg)
end

#add(column_name, direction = nil) ⇒ Object

add a clause to the sorting, assuming the column is sortable

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
# File 'lib/active_scaffold/data_structures/sorting.rb', line 31

def add(column_name, direction = nil)
  direction ||= 'ASC'
  direction = direction.to_s.upcase
  column = get_column(column_name)
  raise ArgumentError, "Could not find column #{column_name}" if column.nil?
  raise ArgumentError, "Sorting direction unknown" unless [:ASC, :DESC].include? direction.to_sym
  @clauses << [column, direction] if column.sortable?
  raise ArgumentError, "Can't mix :method- and :sql-based sorting" if mixed_sorting?
end

#clauseObject

builds an order-by clause



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/active_scaffold/data_structures/sorting.rb', line 89

def clause
  return nil if sorts_by_method? || default_sorting?

  # unless the sorting is by method, create the sql string
  order = []
  each do |sort_column, sort_direction|
    sql = sort_column.sort[:sql]
    next if sql.nil? or sql.empty?

    order << "#{sql} #{sort_direction}"
  end

  order.join(', ') unless order.empty?
end

#clearObject

clears the sorting



53
54
55
56
# File 'lib/active_scaffold/data_structures/sorting.rb', line 53

def clear
  @default_sorting = false
  @clauses = []
end

#direction_of(column) ⇒ Object



63
64
65
66
67
# File 'lib/active_scaffold/data_structures/sorting.rb', line 63

def direction_of(column)
  clause = get_clause(column)
  return if clause.nil?
  clause[1]
end

#eachObject

iterate over the clauses



79
80
81
# File 'lib/active_scaffold/data_structures/sorting.rb', line 79

def each
  @clauses.each { |clause| yield clause }
end

#firstObject

provides quick access to the first (and sometimes only) clause



84
85
86
# File 'lib/active_scaffold/data_structures/sorting.rb', line 84

def first
  @clauses.first
end

#set(*args) ⇒ Object

clears the sorting before setting to the given column/direction



47
48
49
50
# File 'lib/active_scaffold/data_structures/sorting.rb', line 47

def set(*args)
  clear
  add(*args)
end

#set_default_sorting(model) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/active_scaffold/data_structures/sorting.rb', line 11

def set_default_sorting(model)
  model_scope = model.send(:current_scoped_methods)
  order_clause = model_scope.arel.order_clauses.join(",") if model_scope

  # If an ORDER BY clause is found set default sorting according to it, else
  # fallback to setting primary key ordering
  if order_clause
    set_sorting_from_order_clause(order_clause, model.table_name)
    @default_sorting = true
  else
    set(model.primary_key, 'ASC') if model.column_names.include?(model.primary_key)
  end
end

#set_nested_sorting(table_name, order_clause) ⇒ Object



25
26
27
28
# File 'lib/active_scaffold/data_structures/sorting.rb', line 25

def set_nested_sorting(table_name, order_clause)
  clear
  set_sorting_from_order_clause(order_clause, table_name)
end

#sorts_by_method?Boolean

checks whether any column is configured to sort by method (using a proc)

Returns:

  • (Boolean)


70
71
72
# File 'lib/active_scaffold/data_structures/sorting.rb', line 70

def sorts_by_method?
  @clauses.any? { |sorting| sorting[0].sort.is_a? Hash and sorting[0].sort.has_key? :method }
end

#sorts_by_sql?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/active_scaffold/data_structures/sorting.rb', line 74

def sorts_by_sql?
  @clauses.any? { |sorting| sorting[0].sort.is_a? Hash and sorting[0].sort.has_key? :sql }
end

#sorts_on?(column) ⇒ Boolean

checks whether the given column (a Column object or a column name) is in the sorting

Returns:

  • (Boolean)


59
60
61
# File 'lib/active_scaffold/data_structures/sorting.rb', line 59

def sorts_on?(column)
  !get_clause(column).nil?
end