From b9b91231ae176d68ecd42815543f3e55a482c5a4 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 6 Dec 2018 13:23:06 +0000 Subject: [PATCH] Fix rubocop issues in mailers --- app/helpers/i18n_helper.rb | 4 +- app/mailers/producer_mailer.rb | 3 +- app/mailers/spree/order_mailer_decorator.rb | 46 ++++++++++++--------- app/mailers/subscription_mailer.rb | 3 +- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/app/helpers/i18n_helper.rb b/app/helpers/i18n_helper.rb index f22f5b0f54..215028e5aa 100644 --- a/app/helpers/i18n_helper.rb +++ b/app/helpers/i18n_helper.rb @@ -15,7 +15,9 @@ module I18nHelper end def valid_locale(object_with_locale) - if object_with_locale.present? && object_with_locale.locale.present? && available_locale?(object_with_locale.locale) + if object_with_locale.present? && + object_with_locale.locale.present? && + available_locale?(object_with_locale.locale) object_with_locale.locale else I18n.default_locale diff --git a/app/mailers/producer_mailer.rb b/app/mailers/producer_mailer.rb index a41ec0e0d1..058d5403ba 100644 --- a/app/mailers/producer_mailer.rb +++ b/app/mailers/producer_mailer.rb @@ -12,7 +12,8 @@ class ProducerMailer < Spree::BaseMailer @tax_total = tax_total_from_line_items(line_items) I18n.with_locale valid_locale(@producer.owner) do - subject = "[#{Spree::Config.site_name}] #{I18n.t('producer_mailer.order_cycle.subject', producer: producer.name)}" + order_cycle_subject = I18n.t('producer_mailer.order_cycle.subject', producer: producer.name) + subject = "[#{Spree::Config.site_name}] #{order_cycle_subject}" return unless has_orders?(order_cycle, producer) mail( diff --git a/app/mailers/spree/order_mailer_decorator.rb b/app/mailers/spree/order_mailer_decorator.rb index c4bedc932a..7d2cce4609 100644 --- a/app/mailers/spree/order_mailer_decorator.rb +++ b/app/mailers/spree/order_mailer_decorator.rb @@ -7,48 +7,54 @@ Spree::OrderMailer.class_eval do def cancel_email(order, resend = false) @order = find_order(order) I18n.with_locale valid_locale(@order.user) do - subject = (resend ? "[#{t(:resend).upcase}] " : '') - subject += "#{Spree::Config[:site_name]} #{t('order_mailer.cancel_email.subject')} ##{order.number}" - mail(to: order.email, from: from_address, subject: subject) + mail(to: order.email, + from: from_address, + subject: mail_subject(t('order_mailer.cancel_email.subject'), resend)) end end def confirm_email_for_customer(order, resend = false) find_order(order) # Finds an order instance from an id I18n.with_locale valid_locale(@order.user) do - subject = (resend ? "[#{t(:resend).upcase}] " : '') - subject += "#{Spree::Config[:site_name]} #{t('order_mailer.confirm_email.subject')} ##{@order.number}" - mail(:to => @order.email, - :from => from_address, - :subject => subject, - :reply_to => @order.distributor.contact.email) + mail(to: @order.email, + from: from_address, + subject: mail_subject(t('order_mailer.confirm_email.subject'), resend), + reply_to: @order.distributor.contact.email) end end def confirm_email_for_shop(order, resend = false) find_order(order) # Finds an order instance from an id I18n.with_locale valid_locale(@order.user) do - subject = (resend ? "[#{t(:resend).upcase}] " : '') - subject += "#{Spree::Config[:site_name]} #{t('order_mailer.confirm_email.subject')} ##{@order.number}" - mail(:to => @order.distributor.contact.email, - :from => from_address, - :subject => subject) + mail(to: @order.distributor.contact.email, + from: from_address, + subject: mail_subject(t('order_mailer.confirm_email.subject'), resend)) end end def invoice_email(order, pdf) find_order(order) # Finds an order instance from an id + attach_file("invoice-#{@order.number}.pdf", pdf) I18n.with_locale valid_locale(@order.user) do - attachments["invoice-#{@order.number}.pdf"] = pdf if pdf.present? - subject = "#{Spree::Config[:site_name]} #{t(:invoice)} ##{@order.number}" - mail(:to => @order.email, - :from => from_address, - :subject => subject, - :reply_to => @order.distributor.contact.email) + mail(to: @order.email, + from: from_address, + subject: mail_subject(t(:invoice), false), + reply_to: @order.distributor.contact.email) end end def find_order(order) @order = order.respond_to?(:id) ? order : Spree::Order.find(order) end + + private + + def mail_subject(base_subject, resend) + resend_prefix = (resend ? "[#{t(:resend).upcase}] " : '') + "#{resend_prefix}#{Spree::Config[:site_name]} #{base_subject} ##{@order.number}" + end + + def attach_file(filename, file) + attachments[filename] = file if file.present? + end end diff --git a/app/mailers/subscription_mailer.rb b/app/mailers/subscription_mailer.rb index bd2a1fcb21..0046155499 100644 --- a/app/mailers/subscription_mailer.rb +++ b/app/mailers/subscription_mailer.rb @@ -48,7 +48,8 @@ class SubscriptionMailer < Spree::BaseMailer def send_mail(order) I18n.with_locale valid_locale(order.user) do - subject = "#{Spree::Config[:site_name]} #{t('order_mailer.confirm_email.subject')} ##{order.number}" + confirm_email_subject = t('order_mailer.confirm_email.subject') + subject = "#{Spree::Config[:site_name]} #{confirm_email_subject} ##{order.number}" mail(to: order.email, from: from_address, subject: subject,