Class: ActiveScaffold::DataStructures::Sorting

Inherits:
Object
  • Object
show all
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.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/active_scaffold/data_structures/sorting.rb', line 4

def initialize(columns)
  # a ActiveScaffold::DataStructures::Columns instance
  @columns = columns

  # @clauses is an array of pairs: [[column, params], ...]
  # where 'column' is a ActiveScaffold::DataStructures::Column instance and
  # 'params' is a hash with :table, :column, :descending keys
  @clauses = []

  # hash: 'column name'.to_sym => 'index to @clauses array'.to_i
  @cindex = {}

  # synchronize access to @cindex and @clauses
  @mutex = Mutex.new
end

Instance Method Details

#add(order, params = nil) ⇒ Object

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

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/active_scaffold/data_structures/sorting.rb', line 40

def add(order, params = nil)
  params = extract_order_params(order) unless params
  column = get_column(params[:column])
  raise ArgumentError, "Could not find column #{params[:column]} for #{order.inspect}" if column.nil?
  if column.sortable?
    @mutex.synchronize do
      @clauses << [column, params]
      @cindex[params[:column]] = @clauses.count - 1
    end
  end
  raise ArgumentError, "Can't mix :method- and :sql-based sorting" if mixed_sorting?
end

#clauseObject

builds an order-by clause



97
98
99
100
101
102
103
104
105
106
# File 'lib/active_scaffold/data_structures/sorting.rb', line 97

def clause
  return nil if sorts_by_method? || default_sorting?
  @clauses.collect do |column,params|
    if column.sort[:sql]
      order = *column.sort[:sql]
      order = order.collect {|o| o.respond_to?(:invert) ? o.invert : o.desc} if params[:descending]
      order
    end
  end.flatten.compact
end

#clearObject

clears the sorting



59
60
61
62
63
64
65
# File 'lib/active_scaffold/data_structures/sorting.rb', line 59

def clear
  @mutex.synchronize do
    @default_sorting = false
    @clauses = []
    @cindex = {}
  end
end

#direction_of(column) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/active_scaffold/data_structures/sorting.rb', line 72

def direction_of(column)
  c = @mutex.synchronize do
    i = @cindex[(column.respond_to?(:name) ? column.name : column)]
    @clauses[i] if i
  end
  if c
    c[1][:descending] ? 'DESC' : 'ASC'
  end
end

#firstObject

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



92
93
94
# File 'lib/active_scaffold/data_structures/sorting.rb', line 92

def first
  @clauses.first
end

#set(*args) ⇒ Object



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

def set(*args)
  clear
  args.each {|a| add(a)}
end

#set_default_sorting(model) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_scaffold/data_structures/sorting.rb', line 20

def set_default_sorting(model)
  order_clauses = model.dataset.opts[:order]

  # If an ORDER BY clause is found set default sorting according to it, else
  # fallback to setting primary key ordering
  if order_clauses
    # we are going to return nil from 'self.clause', but the @clauses need to be set for determining the sorting properties for view
    set_sorting_from_order_clause(order_clauses, model.table_name)
    @default_sorting = true
  else
    set(model.primary_key) if model.primary_key
  end
end

#set_nested_sorting(table_name, order_clause) ⇒ Object



34
35
36
37
# File 'lib/active_scaffold/data_structures/sorting.rb', line 34

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)


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

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)


87
88
89
# File 'lib/active_scaffold/data_structures/sorting.rb', line 87

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)


68
69
70
# File 'lib/active_scaffold/data_structures/sorting.rb', line 68

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