module TrackingColumn::ClassMethods

Public Instance Methods

tracking_column(target, r_column = nil, allow_nil: false) click to toggle source
# File lib/tracking_column.rb, line 8
def tracking_column(target, r_column = nil, allow_nil: false)
  define_method :"set_#{target}_updated_at" do
    recorder = r_column || :"#{target}_updated_at"

    new_record = send(:"new_record?")

    return if new_record && allow_nil
    return if !new_record && !send(:"#{target}_changed?")

    send(:"#{recorder}=", Time.current)
  end

  private :"set_#{target}_updated_at"

  before_save :"set_#{target}_updated_at"
end