Sha256: 092cf1cbb52e9603613143deb20fc4a09e18211464a090fa77835911334e1ce5

Contents?: true

Size: 985 Bytes

Versions: 1

Compression:

Stored size: 985 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', '')
        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.type_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.0 lib/boolean_timestamps.rb