小编Sak*_*ain的帖子

添加各种模型的关注范围

在 apps/models/concerns/deactivable.rb 中

module Deactivatable
  extend ActiveSupport::Concern

  included do
    scope :alive, -> { where(:deactivated_at => nil) }
  end

  def deactivate(t = Time.current)
    update_attribute(:deactivated_at,t)
  end

  def activate
    update_attribute(:deactivated_at,nil)
  end

  def deactivated?
    deactivated_at.present?
  end

end
Run Code Online (Sandbox Code Playgroud)

这包含在 2 个模型中:app/models/activity_rules/activity_detection_rule.rb 和 app/models/concerns/generic_campaign.rb。

还有 2 个模型包含相同的方法,但属性名称不同。

在可赎回.rb中,

class Redeemable < ActiveRecord::Base
  scope :alive, -> { where("(deactivation_date is null) and (expiry_date is null or expiry_date >= ?)",Date.today) }

  def deactivate(t = Time.current)
    update_attribute(:deactivation_date,t)
  end

  def reactivate
    update_attribute(:deactivation_date,nil)
  end

  def deactivated?
    deactivation_date.present?
  end
end
Run Code Online (Sandbox Code Playgroud)

并在surprise_set.rb中

scope :alive, …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails ruby-on-rails-4

3
推荐指数
1
解决办法
3406
查看次数

救援中的DRY代码=> e在Rails中的各种模型文件中

在sidekiq_batch.rb中,

def sidekiq_status
  begin
    something    
  rescue => e
    Rails.logger.error("\nRESCUENIL in sidekiq_status #{e.class} #{e.message} in #{e.backtrace}")
    # FIXME RESCUENIL
    nil
  end
end
Run Code Online (Sandbox Code Playgroud)

在checkin.rb中,

def attached_receipt_image
  begin
    something else
  rescue => e
    Rails.logger.error("\nRESCUENIL in attached_receipt_image #{e.class} #{e.message} in #{e.backtrace}")
    # FIXME RESCUENIL
    nil
  end
end
Run Code Online (Sandbox Code Playgroud)

在barcode.rb中,

def receipt_check?
  begin
    some code      
  rescue => e
    Rails.logger.error("\nRESCUENIL in receipt_check #{e.class} #{e.message} in #{e.backtrace}")
    # FIXME RESCUENIL
    nil
  end
end
Run Code Online (Sandbox Code Playgroud)

需要干掉代码.如何在模型中为所有这些方法编写常见的错误记录例程?

ruby error-handling model ruby-on-rails dry

2
推荐指数
1
解决办法
290
查看次数