Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: experiment with MailerPreview #2045

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/mailers/invoice_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions spec/factories/invoices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions spec/mailer_previews/base_preview.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions spec/mailer_previews/invoice_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -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
Loading