-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor invoice PDF generation, download and delivery
- Remove `Que::Mailer` (#895) - Extract controllers - Extract translations - Convert HAML to ERB - Add mailer preview - Improve UI - Remove unused routes - Add tests
- Loading branch information
Artur Beljajev
committed
Apr 10, 2019
1 parent
c192cc0
commit 97dd5b4
Showing
30 changed files
with
288 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Admin | ||
module Invoices | ||
class DeliveryController < BaseController | ||
include Deliverable | ||
|
||
private | ||
|
||
def redirect_url | ||
admin_invoice_path(@invoice) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Deliverable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :find_invoice | ||
end | ||
|
||
def new | ||
authorize! :manage, @invoice | ||
@recipient = @invoice.buyer.billing_email | ||
end | ||
|
||
def create | ||
authorize! :manage, @invoice | ||
|
||
InvoiceMailer.invoice_email(invoice: @invoice, recipient: params[:recipient]).deliver_now | ||
|
||
redirect_to redirect_url, notice: t('.delivered') | ||
end | ||
|
||
private | ||
|
||
def find_invoice | ||
@invoice = Invoice.find(params[:invoice_id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Registrar | ||
module Invoices | ||
class DeliveryController < BaseController | ||
include Deliverable | ||
|
||
private | ||
|
||
def redirect_url | ||
registrar_invoice_path(@invoice) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,9 @@ | ||
class InvoiceMailer < ApplicationMailer | ||
include Que::Mailer | ||
def invoice_email(invoice:, recipient:) | ||
@invoice = invoice | ||
|
||
def invoice_email(invoice_id, html, billing_email) | ||
@invoice = Invoice.find_by(id: invoice_id) | ||
billing_email ||= @invoice.billing_email | ||
return unless @invoice | ||
return if whitelist_blocked?(billing_email) | ||
|
||
kit = PDFKit.new(html) | ||
pdf = kit.to_pdf | ||
invoice = @invoice | ||
|
||
attachments[invoice.pdf_name] = pdf | ||
mail(to: format(billing_email), subject: invoice) | ||
subject = default_i18n_subject(invoice_number: invoice.number) | ||
attachments["invoice-#{invoice.number}.pdf"] = invoice.as_pdf | ||
mail(to: recipient, subject: subject) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Invoice | ||
class PdfGenerator | ||
attr_reader :invoice | ||
|
||
def initialize(invoice) | ||
@invoice = invoice | ||
end | ||
|
||
def as_pdf | ||
generator = PDFKit.new(invoice_html) | ||
generator.to_pdf | ||
end | ||
|
||
private | ||
|
||
def invoice_html | ||
view = ActionView::Base.new(ActionController::Base.view_paths, invoice: invoice) | ||
view.class_eval { include ApplicationHelper } | ||
view.render(file: 'invoice/pdf', layout: false) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<ol class="breadcrumb"> | ||
<li><%= link_to t('admin.invoices.index.header'), admin_invoices_path %></li> | ||
<li><%= link_to @invoice, admin_invoice_path(@invoice) %></li> | ||
</ol> | ||
|
||
<div class="page-header"> | ||
<h1><%= t '.header' %></h1> | ||
</div> | ||
|
||
<%= form_tag(admin_invoice_delivery_path(@invoice)) do %> | ||
<div class="row"> | ||
<div class="col-md-4 col-md-offset-4"> | ||
<div class="form-group"> | ||
<%= label_tag :recipient %> | ||
<%= email_field_tag :recipient, @recipient, required: true, autofocus: true, | ||
class: 'form-control' %> | ||
</div> | ||
<div class="row"> | ||
<div class="col-md-12 text-right"> | ||
<%= submit_tag t('.submit_btn'), class: 'btn btn-warning' %> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<% end %> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<ol class="breadcrumb"> | ||
<li><%= link_to t('registrar.invoices.index.header'), registrar_invoices_path %></li> | ||
<li><%= link_to @invoice, registrar_invoice_path(@invoice) %></li> | ||
</ol> | ||
|
||
<div class="page-header"> | ||
<h1><%= t '.header' %></h1> | ||
</div> | ||
|
||
<%= form_tag(registrar_invoice_delivery_path(@invoice)) do %> | ||
<div class="row"> | ||
<div class="col-md-4 col-md-offset-4"> | ||
<div class="form-group"> | ||
<%= label_tag :recipient %> | ||
<%= email_field_tag :recipient, @recipient, required: true, autofocus: true, | ||
class: 'form-control' %> | ||
</div> | ||
<div class="row"> | ||
<div class="col-md-12 text-right"> | ||
<%= submit_tag t('.submit_btn'), class: 'btn btn-warning' %> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<% end %> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
en: | ||
admin: | ||
invoices: | ||
index: | ||
header: Invoices | ||
|
||
show: | ||
download_btn: Download | ||
deliver_btn: Send | ||
|
||
cancel: | ||
cancelled: Invoice has been cancelled |
Oops, something went wrong.