Sha256: 9f4119980a8895376f77300a9f30ec903a232621c5a26e7d61451e28c100f116
Contents?: true
Size: 1.17 KB
Versions: 1
Compression:
Stored size: 1.17 KB
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 scope boolean_attribute, lambda { |value = true| if value where.not(timestamp_attribute => nil) else where(timestamp_attribute => nil) 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.1.0 | lib/boolean_timestamps.rb |