Class: AdminListPage

Inherits:
RecordProxyPage
  • Object
show all
Defined in:
lib/models/admin_list_page.rb

Instance Method Summary collapse

Instance Method Details

#format_record(record) ⇒ Object



118
119
120
# File 'lib/models/admin_list_page.rb', line 118

def format_record(record)
  {record: record, name: record.name, '_row_html' => render_record(record)}
end

#parser_for_column(column) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/models/admin_list_page.rb', line 86

def parser_for_column(column)
  case column.style
  when 'Text'
    'text'
  when 'Boolean'
    'text'
  when 'Integer'
    'digit'
  when 'Float'
    'digit'
  when 'Currency'
    'digit'
  when 'Percentage'
    'digit'
  when 'Date'
    'isoDate'
  when 'Time'
    'time'
  when 'DateTime'
    'isoDate'
  when 'Label'
    'text'
  end
end

#render_record(record) ⇒ Object



81
82
83
84
# File 'lib/models/admin_list_page.rb', line 81

def render_record(record)
  cells = columns.collect {|column| render_value(record, column)}.join
  "<tr data-record-id='#{record.id}'>#{cells}</tr>"
end

#render_value(record, column) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/models/admin_list_page.rb', line 13

def render_value(record, column)
  if column.name.include?('.')
    parts = column.name.split('.')
    raw_value = record
    until parts.empty?
      raw_value = raw_value.try(parts.shift)
    end
  else
    raw_value = record.get(column.name)
  end
  value = ''
  
  case column.style
  when 'Text'
    value = raw_value
  
  when 'Boolean'
    raw_value = raw_value ? column.true_text : column.false_text
    value = raw_value
    
  when 'Integer'
    raw_value = raw_value.to_i
    value = raw_value
    
  when 'Float'
    raw_value = raw_value.to_f
    value = raw_value.round(column.decimals)
    
  when 'Currency'
    raw_value = raw_value.to_f
    value = "#{symbol}#{raw_value.round(column.decimals)}"
    
  when 'Percentage'
    raw_value = raw_value.to_f
    value = "#{raw_value.round(column.decimals)}%"
    
  when 'Date'
    if column.format
      value = raw_value.strftime(column.format)
    else
      value = raw_value.strftime('%F')
    end
    raw_value = raw_value.strftime('%F %T')
    
  when 'Time'
    if column.format
      value = raw_value.strftime(column.format)
    else
      value = raw_value.strftime('%R')
    end
    raw_value = raw_value.strftime('%T')
    
  when 'DateTime'
    if column.format
      value = raw_value.strftime(column.format)
    else
      value = raw_value.strftime('%F %R')
    end
    raw_value = raw_value.strftime('%F %T')
    
  when 'Label'
    label_color = column.colors.find {|label_color| label_color.label == raw_value}
    value = "<span style='background-color: ##{label_color.try(:color)}'>#{raw_value}</span>"
  end
  
  "<td data-value='#{raw_value}'>#{value}</td>"
end

#sort_listObject



111
112
113
114
115
116
# File 'lib/models/admin_list_page.rb', line 111

def sort_list
  columns.each_with_index do |column, index|
    return "[[#{index}, 0]]" if column.default_sort
  end
  "[[0, 0]]"
end