|
1 | 1 | class WorkshopLogDecorator < ApplicationDecorator |
2 | 2 | def detail(length: nil) |
3 | | - length ? description&.truncate(length) : description |
| 3 | + description = length ? object.description&.truncate(length) : object.description |
| 4 | + "#{description}<br>#{participants_table}".html_safe |
| 5 | + end |
| 6 | + |
| 7 | + private |
| 8 | + |
| 9 | + def participants_table |
| 10 | + children_first = object.children_first_time.to_i |
| 11 | + children_ongoing = object.children_ongoing.to_i |
| 12 | + |
| 13 | + teens_first = object.teens_first_time.to_i |
| 14 | + teens_ongoing = object.teens_ongoing.to_i |
| 15 | + |
| 16 | + adults_first = object.adults_first_time.to_i |
| 17 | + adults_ongoing = object.adults_ongoing.to_i |
| 18 | + |
| 19 | + totals_first = children_first + teens_first + adults_first |
| 20 | + totals_ongoing = children_ongoing + teens_ongoing + adults_ongoing |
| 21 | + |
| 22 | + <<~HTML.html_safe |
| 23 | + <table width="100%" cellpadding="0" cellspacing="0" |
| 24 | + style="border-collapse:collapse; margin-top:12px;"> |
| 25 | + #{header_row} |
| 26 | + #{data_row("Children", children_first, children_ongoing)} |
| 27 | + #{data_row("Teens", teens_first, teens_ongoing)} |
| 28 | + #{data_row("Adults", adults_first, adults_ongoing)} |
| 29 | + #{total_row(totals_first, totals_ongoing)} |
| 30 | + </table> |
| 31 | + HTML |
| 32 | + end |
| 33 | + |
| 34 | + def header_row |
| 35 | + %w[Group First\ Time Ongoing Total].map do |label| |
| 36 | + "<td style=\"#{cell_style('font-weight:bold;background:#f5f5f5')}\">#{label}</td>" |
| 37 | + end.then { |cells| "<tr>#{cells.join}</tr>" } |
| 38 | + end |
| 39 | + |
| 40 | + def data_row(label, first, ongoing) |
| 41 | + total = first + ongoing |
| 42 | + |
| 43 | + "<tr>" \ |
| 44 | + "<td style=\"#{cell_style}\">#{label}</td>" \ |
| 45 | + "<td style=\"#{cell_style}\">#{first}</td>" \ |
| 46 | + "<td style=\"#{cell_style}\">#{ongoing}</td>" \ |
| 47 | + "<td style=\"#{cell_style}\">#{total}</td>" \ |
| 48 | + "</tr>" |
| 49 | + end |
| 50 | + |
| 51 | + def total_row(first, ongoing) |
| 52 | + total = first + ongoing |
| 53 | + |
| 54 | + "<tr>" \ |
| 55 | + "<td style=\"#{cell_style('font-weight:bold')}\">Total</td>" \ |
| 56 | + "<td style=\"#{cell_style('font-weight:bold')}\">#{first}</td>" \ |
| 57 | + "<td style=\"#{cell_style('font-weight:bold')}\">#{ongoing}</td>" \ |
| 58 | + "<td style=\"#{cell_style('font-weight:bold')}\">#{total}</td>" \ |
| 59 | + "</tr>" |
| 60 | + end |
| 61 | + |
| 62 | + def cell_style(extra = "") |
| 63 | + [ |
| 64 | + "border:1px solid #ddd", |
| 65 | + "padding:6px", |
| 66 | + "display:table-cell", |
| 67 | + "vertical-align:top", |
| 68 | + extra |
| 69 | + ].reject(&:blank?).join("; ") |
4 | 70 | end |
5 | 71 | end |
0 commit comments