Class: NtiReceiptBuilder::Presenters::Collection
- Inherits:
-
Object
- Object
- NtiReceiptBuilder::Presenters::Collection
- Includes:
- ActionView::Helpers::NumberHelper
- Defined in:
- lib/nti_receipt_builder/presenters/collection.rb
Overview
Presents one order_lines layout element: whitelisted columns, per-row formatted cells,
and the dynamically computed content height that drives overflow detection. An empty
collection renders a single muted "no line items" row rather than collapsing to nothing.
columns, rows and total_column_width_mm are memoised per instance so cell rendering
stays O(rows x columns) rather than recomputing the width sum once per cell. Instances are
short-lived — one per render — so nothing here outlives the response.
Constant Summary collapse
- DEFAULT_ROW_SPACING_MM =
1.0- DEFAULT_FONT_SIZE_PT =
9- MM_PER_PT =
0.3528- LINE_HEIGHT_FACTOR =
Tight leading — receipt rows sit directly under one another. Anything near 1.6 double-spaces the table and pushes the line items apart.
1.25
Instance Method Summary collapse
-
#column_width_pct(column) ⇒ Object
Configured column widths are treated as ratios, not absolutes: normalising them to percentages lets
table-layout: fixedfill the row edge-to-edge whatever the mm happen to sum to. - #columns ⇒ Object
- #content_height_mm ⇒ Object
- #empty? ⇒ Boolean
- #font_size_pt ⇒ Object
-
#header_height_mm ⇒ Object
The header is just a bold row — same typography, same height.
- #id ⇒ Object
-
#initialize(element, rows, definition: nil) ⇒ Collection
constructor
A new instance of Collection.
- #row_height_mm ⇒ Object
- #row_spacing_mm ⇒ Object
- #rows ⇒ Object
- #show_header? ⇒ Boolean
-
#text_line_height_mm ⇒ Object
The line box for one row of text.
- #type ⇒ Object
- #width_mm ⇒ Object
- #x_mm ⇒ Object
- #y_mm ⇒ Object
- #z_index ⇒ Object
Constructor Details
#initialize(element, rows, definition: nil) ⇒ Collection
Returns a new instance of Collection.
24 25 26 27 28 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 24 def initialize(element, rows, definition: nil) @element = element @collection_rows = rows || [] @definition = definition end |
Instance Method Details
#column_width_pct(column) ⇒ Object
Configured column widths are treated as ratios, not absolutes: normalising them to
percentages lets table-layout: fixed fill the row edge-to-edge whatever the mm happen
to sum to. Falls back to even columns when the template configures no widths at all.
44 45 46 47 48 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 44 def column_width_pct(column) return (100.0 / columns.size).round(4) unless total_column_width_mm.positive? (column['width_mm'] / total_column_width_mm * 100).round(4) end |
#columns ⇒ Object
37 38 39 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 37 def columns @columns ||= (config['columns'] || []).map { |column| build_column(column) } end |
#content_height_mm ⇒ Object
72 73 74 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 72 def content_height_mm header_height_mm + (rows.size * row_height_mm) end |
#empty? ⇒ Boolean
66 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 66 def empty? = collection_rows.blank? |
#font_size_pt ⇒ Object
52 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 52 def font_size_pt = config['font_size_pt'] || DEFAULT_FONT_SIZE_PT |
#header_height_mm ⇒ Object
The header is just a bold row — same typography, same height.
64 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 64 def header_height_mm = show_header? ? row_height_mm : 0.0 |
#id ⇒ Object
30 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 30 def id = element['id'] |
#row_height_mm ⇒ Object
61 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 61 def row_height_mm = text_line_height_mm + row_spacing_mm |
#row_spacing_mm ⇒ Object
51 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 51 def row_spacing_mm = config['row_spacing_mm'] || DEFAULT_ROW_SPACING_MM |
#rows ⇒ Object
68 69 70 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 68 def rows @rows ||= build_rows end |
#show_header? ⇒ Boolean
50 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 50 def show_header? = config.fetch('show_header', true) |
#text_line_height_mm ⇒ Object
The line box for one row of text. Cells render at exactly this line-height with row_spacing_mm as padding below, so a row's drawn height always equals row_height_mm and content_height_mm stays honest for overflow detection.
57 58 59 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 57 def text_line_height_mm (font_size_pt * MM_PER_PT * LINE_HEIGHT_FACTOR).round(4) end |
#type ⇒ Object
31 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 31 def type = 'order_lines' |
#width_mm ⇒ Object
34 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 34 def width_mm = element['width_mm'].to_f |
#x_mm ⇒ Object
32 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 32 def x_mm = element['x_mm'].to_f |
#y_mm ⇒ Object
33 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 33 def y_mm = element['y_mm'].to_f |
#z_index ⇒ Object
35 |
# File 'lib/nti_receipt_builder/presenters/collection.rb', line 35 def z_index = element['z_index'] || 0 |