寻找标志日期时间字段的宝石

时间:2012-08-15 13:50:03

标签: ruby-on-rails ruby ruby-on-rails-3 datetime gem

我获得了datetime filds order_confirmed_at completion_confirmed_at

class CoolModel < ActiveRecord::Base
  attr_accessible :order_confirmed, completion_confirmed

  def order_confirmed
    order_confirmed_at.present?
  end

  def order_confirmed=(state)
    if state and order_confirmed_at.blank?
      self.order_confirmed_at = Time.now
    end
    order_confirmed_at.present?
  end

  def completion_confirmed
    completion_confirmed_at.present?
  end

  def completion_confirmed=(state)
    if state and completion_confirmed_at.blank?
      self.completion_confirmed_at = Time.now
    end
    completion_confirmed_at.present?
  end
end

...所以在我看来,我只需勾选确认并完成订单的复选框

事情是:不仅这是重复,但这些东西显然看起来非常标准。所以在节省我的时间里写下gem:有轨道宝石/引擎这样做(或者可能是我不知道的Rails的一部分)??

class CoolModel < ActiveRecord::Base
  #something like this 
  acts_even_coller_on :order_confirmed_at, :completion_confirmed_at
end

1 个答案:

答案 0 :(得分:0)

你可以覆盖setter方法。

def confirmed_at=(value)
  super value == "true" ? confirmed_at || Time.now : nil
end