Class: Receipts::Base

Inherits:
Prawn::Document
  • Object
show all
Defined in:
lib/receipts/base.rb

Direct Known Subclasses

Invoice, Receipt, Statement

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
# File 'lib/receipts/base.rb', line 9

def initialize(attributes = {})
  super(page_size: attributes.delete(:page_size) || "LETTER")
  setup_fonts attributes.fetch(:font, Receipts.default_font)

  @title = attributes.fetch(:title, self.class.title)

  generate_from(attributes)
end

Class Attribute Details

.titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/receipts/base.rb', line 6

def title
  @title
end

Instance Attribute Details

#companyObject

Returns the value of attribute company.



3
4
5
# File 'lib/receipts/base.rb', line 3

def company
  @company
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/receipts/base.rb', line 3

def title
  @title
end

Instance Method Details

#default_message(company:) ⇒ Object



105
106
107
# File 'lib/receipts/base.rb', line 105

def default_message(company:)
  "For questions, contact us anytime at <color rgb='326d92'><link href='mailto:#{company.fetch(:email)}?subject=Question about my receipt'><b>#{company.fetch(:email)}</b></link></color>."
end

#generate_from(attributes) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/receipts/base.rb', line 18

def generate_from(attributes)
  return if attributes.empty?

  company = attributes.fetch(:company)
  header company: company, height: attributes.fetch(:logo_height, 16)
  render_details attributes.fetch(:details)
  render_billing_details company: company, recipient: attributes.fetch(:recipient)
  render_line_items(
    line_items: attributes.fetch(:line_items),
    column_widths: attributes[:column_widths]
  )
  render_footer attributes.fetch(:footer, default_message(company: company))
end

#header(company: {}, height: 16) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/receipts/base.rb', line 49

def header(company: {}, height: 16)
   = company[:logo]

  if .nil?
    text company.fetch(:name), align: :right, style: :bold, size: 16, color: "4b5563"
  else
    image load_image(), height: height, position: :right
  end

  move_up height
  text title, style: :bold, size: 16
end

#load_image(logo) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/receipts/base.rb', line 41

def load_image()
  if .is_a? String
    .start_with?("http") ? URI.parse().open : File.open()
  else
    
  end
end

#render_billing_details(company:, recipient:, margin_top: 16, display_values: nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/receipts/base.rb', line 67

def render_billing_details(company:, recipient:, margin_top: 16, display_values: nil)
  move_down margin_top

  display_values ||= company.fetch(:display, [:address, :phone, :email])
  company_details = company.values_at(*display_values).compact.join("\n")

  line_items = [
    [
      {content: "<b>#{company.fetch(:name)}</b>\n#{company_details}", padding: [0, 12, 0, 0]},
      {content: Array(recipient).join("\n"), padding: [0, 12, 0, 0]}
    ]
  ]
  table(line_items, width: bounds.width, cell_style: {borders: [], inline_format: true, overflow: :expand})
end

#render_details(details, margin_top: 16) ⇒ Object



62
63
64
65
# File 'lib/receipts/base.rb', line 62

def render_details(details, margin_top: 16)
  move_down margin_top
  table(details, cell_style: {borders: [], inline_format: true, padding: [0, 8, 2, 0]})
end


100
101
102
103
# File 'lib/receipts/base.rb', line 100

def render_footer(message, margin_top: 30)
  move_down margin_top
  text message, inline_format: true
end

#render_line_items(line_items:, margin_top: 30, column_widths: nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/receipts/base.rb', line 82

def render_line_items(line_items:, margin_top: 30, column_widths: nil)
  move_down margin_top

  borders = line_items.length - 2

  table_options = {
    width: bounds.width,
    cell_style: {border_color: "eeeeee", inline_format: true},
    column_widths: column_widths
  }.compact

  table(line_items, table_options) do
    cells.padding = 6
    cells.borders = []
    row(0..borders).borders = [:bottom]
  end
end

#setup_fonts(custom_font = nil) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/receipts/base.rb', line 32

def setup_fonts(custom_font = nil)
  if !!custom_font
    font_families.update "Primary" => custom_font
    font "Primary"
  end

  font_size 8
end