Class: ActiveScaffold::DataStructures::Sorting
- Includes:
- Enumerable
- Defined in:
- lib/active_scaffold/data_structures/sorting.rb
Overview
encapsulates the column sorting configuration for the List view
Constant Summary collapse
- SORTING_STAGES =
Hash[%w(reset ASC DESC reset).each_cons(2).map{|a|a}].freeze
- DEFAULT_SORTING_STAGES =
Hash[%w(ASC DESC ASC).each_cons(2).map{|a|a}].freeze
Instance Attribute Summary collapse
-
#constraint_columns ⇒ Object
Returns the value of attribute constraint_columns.
-
#sorting_by_primary_key ⇒ Object
Returns the value of attribute sorting_by_primary_key.
Instance Method Summary collapse
-
#<<(arg) ⇒ Object
an alias for
add. -
#add(column_name, direction = nil) ⇒ Object
add a clause to the sorting, assuming the column is sortable.
-
#clause ⇒ Object
builds an order-by clause.
-
#clear ⇒ Object
clears the sorting.
- #direction_of(column) ⇒ Object
-
#each ⇒ Object
iterate over the clauses.
-
#first ⇒ Object
provides quick access to the first (and sometimes only) clause.
-
#initialize(columns) ⇒ Sorting
constructor
A new instance of Sorting.
- #next_sorting_of(column, sorted_by_default) ⇒ Object
-
#set(*args) ⇒ Object
clears the sorting before setting to the given column/direction set(column, direction) set(=> direction, column => direction) set(=> direction, => direction) set([column, direction], [column, direction]).
- #set_default_sorting(model) ⇒ Object
- #set_nested_sorting(table_name, order_clause) ⇒ Object
-
#sorts_by_method? ⇒ Boolean
checks whether any column is configured to sort by method (using a proc).
- #sorts_by_sql? ⇒ Boolean
-
#sorts_on?(column) ⇒ Boolean
checks whether the given column (a Column object or a column name) is in the sorting.
Constructor Details
#initialize(columns) ⇒ Sorting
Returns a new instance of Sorting.
9 10 11 12 13 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 9 def initialize(columns) @columns = columns @clauses = [] @constraint_columns = [] end |
Instance Attribute Details
#constraint_columns ⇒ Object
Returns the value of attribute constraint_columns.
6 7 8 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 6 def constraint_columns @constraint_columns end |
#sorting_by_primary_key ⇒ Object
Returns the value of attribute sorting_by_primary_key.
7 8 9 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 7 def sorting_by_primary_key @sorting_by_primary_key end |
Instance Method Details
#<<(arg) ⇒ Object
an alias for add. must accept its arguments in a slightly different form, though.
49 50 51 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 49 def <<(arg) add(*arg) end |
#add(column_name, direction = nil) ⇒ Object
add a clause to the sorting, assuming the column is sortable
38 39 40 41 42 43 44 45 46 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 38 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.untaint] if column.sortable? raise ArgumentError, "Can't mix :method- and :sql-based sorting" if mixed_sorting? end |
#clause ⇒ Object
builds an order-by clause
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 113 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| next if constraint_columns.include? sort_column.name sql = sort_column.sort[:sql] next if sql.nil? or sql.empty? order << Array(sql).map {|column| "#{column} #{sort_direction}"}.join(', ') end order << @primary_key_clause if @sorting_by_primary_key order unless order.empty? end |
#clear ⇒ Object
clears the sorting
70 71 72 73 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 70 def clear @default_sorting = false @clauses = [] end |
#direction_of(column) ⇒ Object
80 81 82 83 84 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 80 def direction_of(column) clause = get_clause(column) return if clause.nil? clause[1] end |
#each ⇒ Object
iterate over the clauses
103 104 105 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 103 def each @clauses.each { |clause| yield clause } end |
#first ⇒ Object
provides quick access to the first (and sometimes only) clause
108 109 110 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 108 def first @clauses.first end |
#next_sorting_of(column, sorted_by_default) ⇒ Object
88 89 90 91 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 88 def next_sorting_of(column, sorted_by_default) stages = sorted_by_default ? DEFAULT_SORTING_STAGES : SORTING_STAGES stages[direction_of(column)] || 'ASC' end |
#set(*args) ⇒ Object
clears the sorting before setting to the given column/direction set(column, direction) set(=> direction, column => direction) set(=> direction, => direction) set([column, direction], [column, direction])
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 58 def set(*args) clear if args.first.is_a?(Enumerable) args.each do |h| h.is_a?(Hash) ? h.each { |c,d| add(c,d) } : add(*h) end else add(*args) end end |
#set_default_sorting(model) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 15 def set_default_sorting(model) model_scope = model.send(:build_default_scope) order_clause = model_scope.order_values.join(",") if model_scope # fallback to setting primary key ordering if model.column_names.include?(model.primary_key) set([model.primary_key, 'ASC']) @primary_key_clause = clause @sorting_by_primary_key = model.connection.try(:adapter_name) == 'PostgreSQL' # mandatory for postgres, so enabled by default end # If an ORDER BY clause is found set default sorting according to it if order_clause set_sorting_from_order_clause(order_clause, model.table_name) @default_sorting = true end end |
#set_nested_sorting(table_name, order_clause) ⇒ Object
32 33 34 35 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 32 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)
94 95 96 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 94 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
98 99 100 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 98 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
76 77 78 |
# File 'lib/active_scaffold/data_structures/sorting.rb', line 76 def sorts_on?(column) !get_clause(column).nil? end |