【发布时间】:2015-02-06 18:19:36
【问题描述】:
我有一个邮件程序,其方法如下所示:
def review_comment_notification comment_id, locale = I18n.locale
comment = Spree::Comment.find(comment_id)
assign(:review, review_data(comment.commentable))
assign(:user, user_data(comment.commentable.user))
assign(:commenter, user_data(comment.user))
assign(:unsubscribe, unsubscribe_data(self.action))
assign(:comment, comment_data(comment))
mail(
email_id: REVIEW_COMMENT_NOTIFICATION_TEMPLATE,
recipient_address: comment.commentable.user.email,
version_name: localized_version(locale)
)
end
def store_credit_receipt(user_id, store_credit_id, locale = I18n.locale)
store_credit = Spree::StoreCredit.find(store_credit_id)
user = Spree::User.find(user_id)
assign(:user, user_data(user))
assign(:store_credit, store_credit_data(store_credit))
assign(:unsubscribe, unsubscribe_data(self.action))
mail(
email_id: STORE_CREDIT_RECEIPT_TEMPLATE,
recipient_address: user.email,
version_name: localized_version(locale)
)
end
def reset_password_instructions user, store_id, locale = I18n.locale
# Dual handling here kept due to external libraries.
user = user.respond_to?(:id) ? user : Spree::User.find(user)
set_store(Spree::Store.find(store_id)) if store_id
password_reset_url = spree.edit_password_url(
reset_password_token: user.reset_password_token
)
assign(:password_reset_url, password_reset_url)
mail(
email_id: RESET_PASSWORD_INSTRUCTIONS_TEMPLATE,
recipient_address: user.email,
version_name: localized_version(locale)
)
end
def welcome user_id, store_id, locale = I18n.locale
user = Spree::User.find(user_id)
set_store(Spree::Store.find(store_id)) if store_id
assign(:user, user_data(user))
mail(
email_id: WELCOME_TEMPLATE,
recipient_address: user.email,
version_name: localized_version(locale)
)
end
现在我们需要在运行时使用当前语言环境来决定要发送的电子邮件版本。另请注意,这不是标准的 ActionMailer 邮件程序。
问题是,要实现这一点,我必须将 locale = I18n.locale 添加到我们所有邮件程序的每个方法中。
这对我来说是一种主要气味。但是因为我在调用方法时需要语言环境,所以我不能将其设为默认类(除非我遗漏了什么)
有没有办法重构这个添加的逻辑?
【问题讨论】:
标签: ruby-on-rails ruby refactoring dry