Module: Cheveret::Table::Sorting

Included in:
Base
Defined in:
lib/cheveret/table/sorting.rb

Defined Under Namespace

Modules: ClassMethods, Sortable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sort_columnObject

Returns the value of attribute sort_column.



37
38
39
# File 'lib/cheveret/table/sorting.rb', line 37

def sort_column
  @sort_column
end

#sort_directionObject

Returns the value of attribute sort_direction.



37
38
39
# File 'lib/cheveret/table/sorting.rb', line 37

def sort_direction
  @sort_direction
end

#sort_paramObject



82
83
84
# File 'lib/cheveret/table/sorting.rb', line 82

def sort_param
  @sort_param
end

#sort_urlObject

Returns the value of attribute sort_url.



37
38
39
# File 'lib/cheveret/table/sorting.rb', line 37

def sort_url
  @sort_url
end

Class Method Details

.included(base) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/cheveret/table/sorting.rb', line 28

def self.included(base)
  base.module_eval do
    extend ClassMethods
  end

  # make sure our column objects can be configured as sortable
  ::Cheveret::Column.send :include, Sortable
end

Instance Method Details

#render_th(column, options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/cheveret/table/sorting.rb', line 89

def render_th(column, options={})
  return super unless column.sortable?

  options[:class] = [ 'sortable', *options[:class] ]
  options[:class] << 'sorted' if column.name == sort_column
  options[:class].flatten.join(' ').strip

  super
end

#table_header(column) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cheveret/table/sorting.rb', line 102

def table_header(column)
  # wrap unsortable columns in a <span> tag
  return template.(:span, super) unless column.sortable?

  column_key = sort_param % 'sort_column'
  direction_key = sort_param % 'sort_direction'

  attrs = {}
  query = { column_key => column.name,
            direction_key => column.default_sort_direction }

  if column.name == sort_column
    query[direction_key] = ( sort_direction == :asc ? :desc : :asc )
    attrs[:class] = "#{sort_direction}"
  end

  attrs[:href] = template.url_for(sort_url.merge(query))
  template.(:a, super, attrs)
end