diff --git a/app/mailers/invoice_mailer.rb b/app/mailers/invoice_mailer.rb index 142f3ab8f6b..5c503eba84c 100644 --- a/app/mailers/invoice_mailer.rb +++ b/app/mailers/invoice_mailer.rb @@ -15,6 +15,7 @@ def finalized I18n.locale = @customer.preferred_document_locale @invoice.file.open do |file| + pp file attachments['invoice.pdf'] = file.read end diff --git a/config/environments/development.rb b/config/environments/development.rb index 2a07f6ad954..383da6c113a 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -63,6 +63,8 @@ address: 'mailhog', port: 1025, } + config.action_mailer.default_url_options = { host: 'app.lago.dev' } + config.action_mailer.preview_path = "#{Rails.root}/spec/mailer_previews" Dotenv.load end diff --git a/spec/factories/invoices.rb b/spec/factories/invoices.rb index fc82b84c681..e9292c35289 100644 --- a/spec/factories/invoices.rb +++ b/spec/factories/invoices.rb @@ -12,6 +12,16 @@ organization_sequential_id { rand(1_000_000) } + trait :with_file do + after(:build) do |model| + model.file.attach( + io: File.open(Rails.root.join('spec/fixtures/blank.pdf')), + filename: 'blank.pdf', + content_type: 'application/pdf', + ) + end + end + trait :draft do status { :draft } end diff --git a/spec/mailer_previews/base_preview.rb b/spec/mailer_previews/base_preview.rb new file mode 100644 index 00000000000..6c134866591 --- /dev/null +++ b/spec/mailer_previews/base_preview.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true +class BasePreview < ActionMailer::Preview + def self.call(...) + message = nil + ActiveRecord::Base.transaction do + message = super(...) + raise ActiveRecord::Rollback + end + message + end +end diff --git a/spec/mailer_previews/invoice_mailer_preview.rb b/spec/mailer_previews/invoice_mailer_preview.rb new file mode 100644 index 00000000000..bab0fb1f3a6 --- /dev/null +++ b/spec/mailer_previews/invoice_mailer_preview.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class InvoiceMailerPreview < BasePreview + def finalized + invoice = FactoryBot.create(:invoice, :with_file, { + fees_amount_cents: 121_49, + total_amount_cents: 1000_00, + }) + + InvoiceMailer.with(invoice:).finalized + end +end