class EasyInvoices::Generator

Public Class Methods

generate(invoice_params, file_name) click to toggle source
# File lib/easy_invoices/generator.rb, line 6
def self.generate(invoice_params, file_name)
  self.new(invoice_params, file_name).process
end
new(invoice_params, file_name) click to toggle source
# File lib/easy_invoices/generator.rb, line 14
def initialize(invoice_params, file_name)
  @invoice_params = invoice_params
  @file_name = file_name.nil? ? "#{SecureRandom.hex}.pdf" : "#{file_name}.pdf"
end
storage_dir() click to toggle source
# File lib/easy_invoices/generator.rb, line 10
def self.storage_dir
  EasyInvoices.configuration.send(:"#{self.name.demodulize.downcase}_dir") || self::DIR
end

Public Instance Methods

process() click to toggle source
# File lib/easy_invoices/generator.rb, line 19
def process
  check_dir

  File.open(target_file, 'wb') do |file|
    file << rendered_pdf
  end

  target_file
end

Protected Instance Methods

check_dir() click to toggle source
# File lib/easy_invoices/generator.rb, line 31
def check_dir
  FileUtils.mkdir_p(target_dir) unless File.directory?(target_dir)
end
current_dir() click to toggle source
# File lib/easy_invoices/generator.rb, line 43
def current_dir
  File.expand_path(File.dirname(__FILE__))
end
rendered_pdf() click to toggle source
# File lib/easy_invoices/generator.rb, line 51
def rendered_pdf
  WickedPdf.config = {
    'no-background': false,
    image: true
  }
  WickedPdf.new.pdf_from_string( rendered_view )
end
rendered_view() click to toggle source
# File lib/easy_invoices/generator.rb, line 59
def rendered_view
  ApplicationController.render(
    file: template_path,
    layout: false,
    locals: @invoice_params
  )
end
target_dir() click to toggle source
# File lib/easy_invoices/generator.rb, line 35
def target_dir
  Rails.root.join(*self.class.storage_dir)
end
target_file() click to toggle source
# File lib/easy_invoices/generator.rb, line 39
def target_file
  target_dir.join @file_name
end
template_path() click to toggle source
# File lib/easy_invoices/generator.rb, line 47
def template_path
  "#{current_dir}/templates/#{self.class.name.demodulize.downcase}"
end