Sha256: 8c9b591009835e45cb26751a6ea5517b655c87917544e1594275d90247299645

Contents?: true

Size: 836 Bytes

Versions: 9

Compression:

Stored size: 836 Bytes

Contents

class Dorsale::BillingMachine::PdfFileGenerator < Dorsale::Service
  attr_reader :document

  def initialize(document)
    super()
    @document = document
  end

  def call
    document.pdf_file = file
    document.save! if document.persisted?
    document
  end

  private

  def pdf_klass
    if document.is_a?(::Dorsale::BillingMachine::Invoice)
      return ::Dorsale::BillingMachine.invoice_pdf_model
    end

    if document.is_a?(::Dorsale::BillingMachine::Quotation)
      return ::Dorsale::BillingMachine.quotation_pdf_model
    end
  end

  def pdf_data
    @pdf_data ||= pdf_klass.new(document).tap(&:build).render_with_attachments
  end

  def file
    @file ||= StringIO.new(pdf_data)
  end

  class StringIO < ::StringIO
    def original_filename
      @original_filename ||= "#{SecureRandom.uuid}.pdf"
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dorsale-4.0.0 app/services/dorsale/billing_machine/pdf_file_generator.rb
dorsale-3.20.0 app/services/dorsale/billing_machine/pdf_file_generator.rb
dorsale-3.19.1 app/services/dorsale/billing_machine/pdf_file_generator.rb
dorsale-3.19.0 app/services/dorsale/billing_machine/pdf_file_generator.rb
dorsale-3.18.0 app/services/dorsale/billing_machine/pdf_file_generator.rb
dorsale-3.17.0 app/services/dorsale/billing_machine/pdf_file_generator.rb
dorsale-3.16.0 app/services/dorsale/billing_machine/pdf_file_generator.rb
dorsale-3.15.0 app/services/dorsale/billing_machine/pdf_file_generator.rb
dorsale-3.14.11 app/services/dorsale/billing_machine/pdf_file_generator.rb