Class: Pdf::Components::Alert

Inherits:
Pdf::Component show all
Defined in:
lib/pdf/components/alert.rb

Constant Summary collapse

COLORS =
{
  "green"  => { bg: "C8E6C9", text: "2E7D32", border: "4CAF50" },
  "orange" => { bg: "FFE0B2", text: "E65100", border: "FF9800" },
  "red"    => { bg: "FFCDD2", text: "C62828", border: "F44336" },
  "blue"   => { bg: "E1F5FE", text: "0277BD", border: "2196F3" }
}.freeze

Instance Attribute Summary

Attributes inherited from Pdf::Component

#pdf

Instance Method Summary collapse

Methods inherited from Pdf::Component

#initialize

Constructor Details

This class inherits a constructor from Pdf::Component

Instance Method Details

#render(title:, description: nil, color: "blue", **_options) ⇒ 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
# File 'lib/pdf/components/alert.rb', line 13

def render(title:, description: nil, color: "blue", **_options)
  scheme = COLORS[color.to_s] || COLORS["blue"]

  title_h = 20
  desc_h = description ? (@pdf.height_of(description.to_s, width: bounds.width - 20, size: 10) + 10) : 0
  total_h = title_h + desc_h + 6

  bounding_box([bounds.left, cursor], width: bounds.width, height: total_h) do
    @pdf.stroke_color scheme[:border]
    @pdf.line_width 0.5
    @pdf.fill_color scheme[:bg]
    @pdf.fill_and_stroke_rounded_rectangle [bounds.left, bounds.top], bounds.width, bounds.height, 5

    @pdf.fill_color scheme[:text]

    y = bounds.top - 10
    @pdf.text_box title.to_s, at: [10, y], width: bounds.width - 20, size: 11, style: :bold

    if description
      y -= 18
      @pdf.text_box description.to_s, at: [10, y], width: bounds.width - 20, size: 10
    end
  end

  @pdf.fill_color "000000"
  move_down 15
end