Class: ActiveScaffold::DataStructures::Sorting

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/six-updater-web/vendor/plugins/active_scaffold/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/six-updater-web/vendor/plugins/active_scaffold/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.



33
34
35
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/sorting.rb', line 33

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

#add(column_name, direction = nil) ⇒ Object

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

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/sorting.rb', line 22

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/sorting.rb', line 80

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



44
45
46
47
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/sorting.rb', line 44

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

#direction_of(column) ⇒ Object



54
55
56
57
58
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/sorting.rb', line 54

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

#eachObject

iterate over the clauses



70
71
72
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/sorting.rb', line 70

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

#firstObject

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



75
76
77
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/sorting.rb', line 75

def first
  @clauses.first
end

#set(*args) ⇒ Object

clears the sorting before setting to the given column/direction



38
39
40
41
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/sorting.rb', line 38

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

#set_default_sorting(model) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/sorting.rb', line 11

def set_default_sorting(model)
  last_scope = model.default_scoping.last
  if last_scope.nil?  || last_scope[:find].nil? || last_scope[:find][:order].nil?
    set(model.primary_key, 'ASC') if model.column_names.include?(model.primary_key)
  else
    set_sorting_from_order_clause(last_scope[:find][:order].to_s, model.table_name)
    @default_sorting = true
  end
end

#sorts_by_method?Boolean

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

Returns:

  • (Boolean)


61
62
63
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/sorting.rb', line 61

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)


65
66
67
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/sorting.rb', line 65

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)


50
51
52
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/sorting.rb', line 50

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