Class: Carnival::TheadRenderer

Inherits:
Object
  • Object
show all
Defined in:
app/view_objects/carnival/thead_renderer.rb

Instance Method Summary collapse

Constructor Details

#initialize(fields, sort_column, sort_direction) ⇒ TheadRenderer

Returns a new instance of TheadRenderer.



4
5
6
7
8
# File 'app/view_objects/carnival/thead_renderer.rb', line 4

def initialize fields, sort_column, sort_direction
  @fields = fields
  @sort_column = sort_column.to_sym
  @sort_direction = sort_direction
end

Instance Method Details

#columnsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/view_objects/carnival/thead_renderer.rb', line 11

def columns
  columns = []
  @fields.each do |key, field|
    column = {
      field: field,
      name: field.name,
      sort_dir: sort_dir(field),
      sort_function: sort_function(field),
      class: css_class(field)
    }
    columns << column
  end
  columns
end

#css_class(field) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'app/view_objects/carnival/thead_renderer.rb', line 26

def css_class field
  return 'sorting_disabled' if !field.sortable?

  if field.sort_name.to_sym == @sort_column
    return "sorting_#{@sort_direction.to_s}"
  else
    return 'sorting'
  end
end

#sort_dir(field) ⇒ Object



42
43
44
45
46
47
48
# File 'app/view_objects/carnival/thead_renderer.rb', line 42

def sort_dir field
  sort_direction = 'asc'
  if field.sort_name.to_sym == @sort_column
   sort_direction = 'desc' if @sort_direction == 'asc'
  end
  sort_direction
end

#sort_function(field) ⇒ Object



36
37
38
39
40
# File 'app/view_objects/carnival/thead_renderer.rb', line 36

def sort_function field
  return '' if !field.sortable?
  sort_direction = sort_dir field
  "Carnival.sortColumn('#{field.sort_name}', '#{sort_direction}')"
end