Rails 2.3 应用程序中代码的重构
我有一个相当简单的 Rails 应用程序,允许用户管理他们的客户端并为每个客户端创建一个案例。创建案例后,详细信息将发送到两个电子邮件地址(每封电子邮件中的内容不同),并使用 gem 将信息发送到 FreeagentCentral
。
我已经实现了 delayed_job
,它对于 Freeagent API
调用运行良好,但我认为可能有更好的,较低的开销,发送电子邮件的方法。似乎需要相当一段时间。
目前,我的 Rails 2.3 应用程序中有以下代码。
kases_controller.rb
# POST /kases
# POST /kases.xml
def create
@company = Company.find(params[:kase][:company_id])
@kase = @company.kases.new(params[:kase])
if @kase.save
UserMailer.deliver_makeakase("[email protected]", "Highrise", @kase) if params[:sendtohighrise]
UserMailer.deliver_makeakaseteam("[email protected]", "Highrise", @kase) if params[:notify_team_of_creation]
@kase.delay.create_freeagent_project(current_user) if params[:send_to_freeagent]
redirect_to(@kase)
#flash[:notice] = 'Case was successfully created.'
flash[:notice] = fading_flash_message("Case was successfully created.", 5)
else
render :new
end
end
user_mailer.rb
def makeakase(email, name, kase, bccemails = [])
recipients email
from "[email protected]"
subject "FW: Case creation from Survey Manager"
bcc bccemails
sent_on Time.now
body :name => name, :kase => kase
end
def makeakaseteam(email, name, kase = [])
recipients email
from "[email protected]"
subject "A new case has been created."
sent_on Time.now
body :name => name, :kase => kase
content_type "text/html"
end
我正在寻找有关减少发送电子邮件数量的建议,或者将电子邮件添加到延迟作业的方法。
我使用 PostmarkApp 来发送电子邮件,这是另一个宝石。详细信息可以在这里找到:postmark-gem
I have a fairly simple Rails application that allows users to manage their clients and create a case for each client. When a case is created the details are sent to two email addresses (different content in each email) and using a gem, sends the information to FreeagentCentral
.
I have implemented delayed_job
which is working well for the Freeagent API
call, but I think there is probably a better, lower overhead, method of sending the emails. It seems to take quite a while.
I currently have the following code in my Rails 2.3 application.
kases_controller.rb
# POST /kases
# POST /kases.xml
def create
@company = Company.find(params[:kase][:company_id])
@kase = @company.kases.new(params[:kase])
if @kase.save
UserMailer.deliver_makeakase("[email protected]", "Highrise", @kase) if params[:sendtohighrise]
UserMailer.deliver_makeakaseteam("[email protected]", "Highrise", @kase) if params[:notify_team_of_creation]
@kase.delay.create_freeagent_project(current_user) if params[:send_to_freeagent]
redirect_to(@kase)
#flash[:notice] = 'Case was successfully created.'
flash[:notice] = fading_flash_message("Case was successfully created.", 5)
else
render :new
end
end
user_mailer.rb
def makeakase(email, name, kase, bccemails = [])
recipients email
from "[email protected]"
subject "FW: Case creation from Survey Manager"
bcc bccemails
sent_on Time.now
body :name => name, :kase => kase
end
def makeakaseteam(email, name, kase = [])
recipients email
from "[email protected]"
subject "A new case has been created."
sent_on Time.now
body :name => name, :kase => kase
content_type "text/html"
end
I am looking for any advice on slimming down the number of emails sent, or a way of adding the emails to the delayed jobs.
I use PostmarkApp for sending the emails, which is another gem. Details of which can be found here: postmark-gem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定这是否是您想要的,但我会制作一种方法来发送电子邮件和创建自由代理,然后让它由delayed_job处理。
的东西可能会更简单/更干净。
然后,在您的控制器中编写
或传递 params 之类
Not sure if this is what you want, but i would make a method that would do the sending of the emails and the creation of the freeagent, and then let it be handled by delayed_job.
Something like
and in your controller you would then write
or just handing down the
params
is maybe simpler/cleaner.我已成功使用 Starling 和 Workling 解决类似问题。我不确定这是否是一个较低的开销,因为我个人从未使用过
delayed_job
。您可能想探索一下。I have successfully used Starling and Workling for a similar problem. I'm not sure if this is a lower overhead as I've personally never used
delayed_job
yet. You may want to explore it.安装delayed_job后,对于旧版本,您应该能够简单地执行以下操作。
对于从 2.0.4 开始的版本,语法有点不同。
With delayed_job installed, for older versions you should be able to simply do the following.
For version starting at 2.0.4 the syntax is a bit different.