Module: BatchKit::Helpers::Html

Included in:
Email
Defined in:
lib/batch-kit/helpers/html.rb

Overview

Defines a number of methods to help with generating simple HTML documents

Instance Method Summary collapse

Instance Method Details

#add_table_cells(body, row, cols, cell_type = :td) ⇒ Object

Adds a row of cells to a table.

Parameters:

  • body (Array<String>)

    An array of lines containing the body of the HTML message to which this table row should be added.

  • row (Array, Hash, Object)

    An Array, Hash, or Object from which a row of data shall be retrieved to populate the table.

  • cols (Array<Hash>)

    An Array of Hashes, each containing details for a single column. Each Hash can contain the following options:

    - :class: The CSS class with which to style the column cells.
    - :show: A boolean value indicating whether the column should
      be displayed or skipped.
    - :prefix: Text to appear before the content of the cell.
    - :suffix: Text to appear after the content of the cell.
    - :value: A setting controlling how values are retrieved from
      +row+. By default, this is by index, but this setting can
      override that, and either supply a name or method to call
      on +row+, or a Proc object to invoke on row.
    
  • cell_type (Symbol) (defaults to: :td)

    Either :td (the default) or :th.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/batch-kit/helpers/html.rb', line 137

def add_table_cells(body, row, cols, cell_type = :td)
    cols.each_with_index do |col, i|
        cls = col[:class]
        show = col.fetch(:show, true)
        prefix = col.fetch(:prefix, '')
        suffix = col.fetch(:suffix, '')
        next if !show
        val = case
        when col[:value]
            col[:value].call(row)
        when row.is_a?(Array)
            row[i]
        when row.is_a?(Hash)
            row[col[:name]]
        when row.respond_to?(col[:name])
            row.send(col[:name])
        when row.respond_to?(:[])
            row[col[:name]]
        else
            row
        end
        case val
        when Numeric
            val = val.with_commas
            cls = 'right' unless cls
        when Date, Time, DateTime
            val = val.strftime('%H:%M:%S')
            cls = 'right' unless cls
        end
        td = %Q{<#{cell_type}#{cls ? " class='#{cls}'" : ''}>#{prefix}#{val}#{suffix}</#{cell_type}>}
        body << td
    end
end

#create_head_tag(opts = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/batch-kit/helpers/html.rb', line 34

def create_head_tag(opts = {})
    head_tag = <<-EOS.gsub(/^\w+\n$/, '')
        <head>
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
        #{opts[:title] ? "<title>#{opts[:title]}</title>" : ''}
        #{create_style_tag(opts)}
        </head>
    EOS
end

#create_html_document(body_text = nil, opts = {}) {|body| ... } ⇒ Object

Creates a new HTML document with a pre-defined set of styles

Yields:

  • (body)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/batch-kit/helpers/html.rb', line 13

def create_html_document(body_text = nil, opts = {})
    if body_text.is_a?(Hash)
        opts = body_text
        body_text = nil
    end

    hdr = <<-EOS.gsub(/\s{20}/, '')
        <html>
        #{create_head_tag(opts)}
        <body>
    EOS
    body = [hdr]
    body << body_text if body_text
    yield body if block_given?
    body << <<-EOS.gsub(/\s{20}/, '')
        </body>
        </html>
    EOS
end

#create_html_table(body, data, *cols) ⇒ Object

Creates an HTML table from data.

Parameters:

  • body (Array)

    The HTML body to which this table will be appended.

  • data (Array|Hash)

    The data to be added to the table.

  • cols (Array<Symbol|String|Hash>)

    An array of symbols, strings, or Hashes. String and symbols output as-is, while the hash can contain various options that control the display of the column and the content below it, as follows:

    - :name is the name of the property. It will be used to access
      data values if +data+ is a Hash, and will be used as the
      column header unless a :label property is passed.
    - :label is the label to use as the column header.
    - :class specifies a CSS class name to assign to each cell in
      the column.
    - :show is a boolean that determines whether the column is
      output or suppressed.
    - :prefix is any text that should appear before the content.
    - :suffix is any text that should appear after the content.
    


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/batch-kit/helpers/html.rb', line 98

def create_html_table(body, data, *cols)
    cols.map!{ |col| col.is_a?(Symbol) || col.is_a?(String) ? {name: col.intern} : col }
    body << "<table>"
    body << "<thead><tr>"
    add_table_cells(body,
                    cols.map{ |col| (col[:label] || col[:name]).to_s.titleize },
                    cols.map{ |col| {show: col.fetch(:show, true)} },
                    :th)
    body << "</tr></thead>"
    body << "<tbody>"
    data.each do |row|
        body << "<tr>"
        add_table_cells(body, row, cols)
        body << "</tr>"
    end
    body << "</tbody>"
    body << "</table>"
end

#create_style_tag(opts = {}) ⇒ Object



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
# File 'lib/batch-kit/helpers/html.rb', line 45

def create_style_tag(opts = {})
    font = opts.fetch(:font, 'Calibri')
    style_tag = <<-EOS
        <style>
            @font-face {font-family: #{font};}

            h1      {font-family: #{font}; font-size: 16pt;}
            h2      {font-family: #{font}; font-size: 14pt; margin: 1em 0em .2em;}
            h3      {font-family: #{font}; font-size: 12pt; margin: 1em 0em .2em;}
            body    {font-family: #{font}; font-size: 11pt;}
            p       {margin: .2em 0em;}
            table   {font-family: #{font}; font-size: 10pt;
                     line-height: 12pt; border-collapse: collapse;}
            th      {background-color: #00205B; color: white;
                     font-size: 11pt; font-weight: bold; text-align: left;
                     border: 1px solid #DDDDFF; padding: 1px 5px;}
            td      {border: 1px solid #DDDDFF; padding: 1px 5px;}

            .summary    {font-size: 13pt;}
            .red        {background-color: white; color: #FF0000;}
            .amber      {background-color: white; color: #FFA500;}
            .green      {background-color: white; color: #33A000;}
            .blue       {background-color: white; color: #0000A0;}
            .bold       {font-weight: bold;}
            .center     {text-align: center;}
            .right      {text-align: right;}
            .separator  {width: 200px; border-bottom: 1px gray solid;}
        </style>
        </head>
        <body>
    EOS
end