Rails 2.3 应用程序中代码的重构

发布于 2024-10-19 07:27:18 字数 2379 浏览 2 评论 0原文

我有一个相当简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄暮涼年 2024-10-26 07:27:18

不确定这是否是您想要的,但我会制作一种方法来发送电子邮件和创建自由代理,然后让它由delayed_job处理。

的东西可能会更简单/更干净。

class Kase

  def send_emails(current_user, send_to_highrise, notify_team, send_to_freeagent)
    UserMailer.deliver_makeakase("[email protected]", "Highrise", self) if send_to_highrise
    UserMailer.deliver_makeakaseteam("[email protected]", "Highrise", self) if notify_team
    self.create_freeagent_project(current_user) if send_to_freeagent
  end
end

然后,在您的控制器中编写

if @kase.save
  @kase.delay.send_emails(current_user, params[:sendtohighrise], 
                         params[:notify_team_of_creation], params[:send_to_freeagent])
  redirect_to @kase
  flash[:notice] = fading_flash_message("Case was successfully created.", 5)
else
  ...

或传递 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

class Kase

  def send_emails(current_user, send_to_highrise, notify_team, send_to_freeagent)
    UserMailer.deliver_makeakase("[email protected]", "Highrise", self) if send_to_highrise
    UserMailer.deliver_makeakaseteam("[email protected]", "Highrise", self) if notify_team
    self.create_freeagent_project(current_user) if send_to_freeagent
  end
end

and in your controller you would then write

if @kase.save
  @kase.delay.send_emails(current_user, params[:sendtohighrise], 
                         params[:notify_team_of_creation], params[:send_to_freeagent])
  redirect_to @kase
  flash[:notice] = fading_flash_message("Case was successfully created.", 5)
else
  ...

or just handing down the params is maybe simpler/cleaner.

森末i 2024-10-26 07:27:18

我已成功使用 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.

咆哮 2024-10-26 07:27:18

安装delayed_job后,对于旧版本,您应该能够简单地执行以下操作。

UserMailer.send_later(:deliver_makeakase, "[email protected]", "Highrise", @kase) if params[:sendtohighrise]
UserMailer.send_later(:deliver_makeakaseteam, "[email protected]", "Highrise", @kase) if params[:notify_team_of_creation]

对于从 2.0.4 开始的版本,语法有点不同。

UserMailer.delay.deliver_makeakase("[email protected]", "Highrise", @kase) if params[:sendtohighrise]
UserMailer.delay.deliver_makeakaseteam("[email protected]", "Highrise", @kase) if params[:notify_team_of_creation]

With delayed_job installed, for older versions you should be able to simply do the following.

UserMailer.send_later(:deliver_makeakase, "[email protected]", "Highrise", @kase) if params[:sendtohighrise]
UserMailer.send_later(:deliver_makeakaseteam, "[email protected]", "Highrise", @kase) if params[:notify_team_of_creation]

For version starting at 2.0.4 the syntax is a bit different.

UserMailer.delay.deliver_makeakase("[email protected]", "Highrise", @kase) if params[:sendtohighrise]
UserMailer.delay.deliver_makeakaseteam("[email protected]", "Highrise", @kase) if params[:notify_team_of_creation]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文