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
This commit is contained in:
Artur Beljajev 2019-04-07 19:10:11 +03:00
parent 7e0fd30125
commit 27ea790b28
30 changed files with 288 additions and 138 deletions

View file

@ -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

View file

@ -2,8 +2,6 @@ module Admin
class InvoicesController < BaseController
load_and_authorize_resource
before_action :set_invoice, only: [:forward, :download_pdf]
def new
@deposit = Deposit.new
end
@ -28,33 +26,16 @@ module Admin
@invoices = @q.result.page(params[:page])
end
def show
@invoice = Invoice.find(params[:id])
end
def show; end
def cancel
@invoice.cancel
redirect_to [:admin, @invoice], notice: t('.cancelled')
end
def forward
@invoice.billing_email = @invoice.buyer.billing_email
return unless request.post?
@invoice.billing_email = params[:invoice][:billing_email]
if @invoice.forward(render_to_string('registrar/invoices/pdf', layout: false))
flash[:notice] = t(:invoice_forwared)
redirect_to([:admin, @invoice])
else
flash.now[:alert] = t(:failed_to_forward_invoice)
end
end
def download_pdf
pdf = @invoice.pdf(render_to_string('registrar/invoices/pdf', layout: false))
send_data pdf, filename: @invoice.pdf_name
def download
filename = "invoice-#{@invoice.number}.pdf"
send_data @invoice.as_pdf, filename: filename
end
private
@ -62,9 +43,5 @@ module Admin
def deposit_params
params.require(:deposit).permit(:amount, :description, :registrar_id)
end
def set_invoice
@invoice = Invoice.find(params[:invoice_id])
end
end
end

View file

@ -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

View file

@ -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

View file

@ -2,8 +2,6 @@ class Registrar
class InvoicesController < BaseController
load_and_authorize_resource
before_action :set_invoice, only: [:show, :forward, :download_pdf]
def index
params[:q] ||= {}
invoices = current_registrar_user.registrar.invoices.includes(:items, :account_activity)
@ -15,40 +13,20 @@ class Registrar
end
end
def show;
end
def forward
@invoice.billing_email = @invoice.buyer.billing_email
return unless request.post?
@invoice.billing_email = params[:invoice][:billing_email]
if @invoice.forward(render_to_string('pdf', layout: false))
flash[:notice] = t(:invoice_forwared)
redirect_to([:registrar, @invoice])
else
flash.now[:alert] = t(:failed_to_forward_invoice)
end
end
def show; end
def cancel
@invoice.cancel
redirect_to [:registrar, @invoice], notice: t('.cancelled')
end
def download_pdf
pdf = @invoice.pdf(render_to_string('pdf', layout: false))
send_data pdf, filename: @invoice.pdf_name
def download
filename = "invoice-#{@invoice.number}.pdf"
send_data @invoice.as_pdf, filename: filename
end
private
def set_invoice
@invoice = Invoice.find(params[:id])
end
def normalize_search_parameters
params[:q][:total_gteq].gsub!(',', '.') if params[:q][:total_gteq]
params[:q][:total_lteq].gsub!(',', '.') if params[:q][:total_lteq]