Sha256: b18ea9ddbc4198d9f39629a8de5679b57908d2d6b7f9b7f7eb3401fdb44fac07

Contents?: true

Size: 982 Bytes

Versions: 1

Compression:

Stored size: 982 Bytes

Contents

require 'boolean_timestamps/version'
require 'active_support/concern'
require 'active_support/lazy_load_hooks'

module BooleanTimestamps
  extend ActiveSupport::Concern

  class_methods do
    def boolean_timestamps(*attributes)
      attributes.each do |timestamp_attribute|
        boolean_attribute = timestamp_attribute.to_s.gsub(/_at\z/, '')
        define_method boolean_attribute do
          send("#{timestamp_attribute}?")
        end
        define_method "#{boolean_attribute}?" do
          send("#{timestamp_attribute}?")
        end
        define_method "#{boolean_attribute}=" do |value|
          boolean_value = ActiveRecord::Type::Boolean.new.cast(value)
          unless boolean_value && send("#{timestamp_attribute}?")
            timestamp = boolean_value ? Time.zone.now : nil
            send("#{timestamp_attribute}=", timestamp)
          end
        end
      end
    end
  end
end
ActiveSupport.on_load(:active_record) do
  include BooleanTimestamps
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
boolean_timestamps-1.0.2 lib/boolean_timestamps.rb