Module: Dbviewer::FormattingHelper

Included in:
ApplicationHelper
Defined in:
app/helpers/dbviewer/formatting_helper.rb

Instance Method Summary collapse

Instance Method Details

#format_cell_value(value) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/dbviewer/formatting_helper.rb', line 3

def format_cell_value(value)
  return "NULL" if value.nil?
  return value.to_s.truncate(100) unless value.is_a?(String)

  case value
  when /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/
    # ISO 8601 datetime
    begin
      Time.parse(value).strftime("%Y-%m-%d %H:%M:%S")
    rescue
      value.to_s.truncate(100)
    end
  when /\A\d{4}-\d{2}-\d{2}\z/
    # Date
    value
  when /\A{.+}\z/, /\A\[.+\]\z/
    # JSON
    begin
      JSON.pretty_generate(JSON.parse(value)).truncate(100)
    rescue
      value.to_s.truncate(100)
    end
  else
    value.to_s.truncate(100)
  end
end