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.



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

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

Instance Method Details

#columnsObject



9
10
11
12
13
14
15
16
17
18
19
# File 'app/view_objects/carnival/thead_renderer.rb', line 9

def columns
  @fields.values.map do |field|
    {
      field: field,
      name: field.name,
      sort_dir: sort_dir(field),
      sort_function: sort_function(field),
      class: css_class(field)
    }
  end
end

#css_class(field) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'app/view_objects/carnival/thead_renderer.rb', line 21

def css_class(field)
  return 'sorting_disabled' unless field.sortable?

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

#sort_dir(field) ⇒ Object



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

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



31
32
33
34
35
# File 'app/view_objects/carnival/thead_renderer.rb', line 31

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