Class: Receipts::Base
- Inherits:
-
Prawn::Document
- Object
- Prawn::Document
- Receipts::Base
- Defined in:
- lib/receipts/base.rb
Class Attribute Summary collapse
-
.title ⇒ Object
readonly
Returns the value of attribute title.
Instance Attribute Summary collapse
-
#company ⇒ Object
Returns the value of attribute company.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #default_message(company:) ⇒ Object
- #generate_from(attributes) ⇒ Object
- #header(company: {}, height: 16) ⇒ Object
-
#initialize(attributes = {}) ⇒ Base
constructor
A new instance of Base.
- #load_image(logo) ⇒ Object
- #render_billing_details(company:, recipient:, margin_top: 16, display_values: nil) ⇒ Object
- #render_details(details, margin_top: 16) ⇒ Object
- #render_footer(message, margin_top: 30) ⇒ Object
- #render_line_items(line_items:, margin_top: 30, column_widths: nil) ⇒ Object
- #setup_fonts(custom_font = nil) ⇒ Object
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
.title ⇒ Object (readonly)
Returns the value of attribute title.
6 7 8 |
# File 'lib/receipts/base.rb', line 6 def title @title end |
Instance Attribute Details
#company ⇒ Object
Returns the value of attribute company.
3 4 5 |
# File 'lib/receipts/base.rb', line 3 def company @company end |
#title ⇒ Object
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 (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] ) attributes.fetch(:footer, (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) logo = company[:logo] if logo.nil? text company.fetch(:name), align: :right, style: :bold, size: 16, color: "4b5563" else image load_image(logo), 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(logo) if logo.is_a? String logo.start_with?("http") ? URI.parse(logo).open : File.open(logo) else logo 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 |
#render_footer(message, margin_top: 30) ⇒ Object
100 101 102 103 |
# File 'lib/receipts/base.rb', line 100 def (, margin_top: 30) move_down margin_top text , 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 = { width: bounds.width, cell_style: {border_color: "eeeeee", inline_format: true}, column_widths: column_widths }.compact table(line_items, ) 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 |