module TimestampAsBoolean::ClassMethods

Constants

POSSIBLE_TRUTHY_VALUES

Public Instance Methods

timestamp_as_boolean(method, attrx = nil) click to toggle source
# File lib/timestamp_as_boolean.rb, line 13
def timestamp_as_boolean(method, attrx = nil)
  attrx ||= "#{method}_at"

  @@timestamped_to_bool_list ||= []
  @@timestamped_to_bool_list << method.to_sym

  define_method method do
    eval("#{attrx}.present?")
  end

  define_method "#{method}=" do |val|
    if val.present? && POSSIBLE_TRUTHY_VALUES.include?(val)
      send("#{attrx}=", Time.current) if send(attrx).nil? # Use the setter to preserve AR dirty tracking.
    else
      send("#{attrx}=", nil)
    end
  end
end
timestamped_to_bool_list() click to toggle source
# File lib/timestamp_as_boolean.rb, line 32
def timestamped_to_bool_list
  @@timestamped_to_bool_list
end