Module: TimeForABoolean

Defined in:
lib/time_for_a_boolean.rb,
lib/time_for_a_boolean/railtie.rb,
lib/time_for_a_boolean/version.rb

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =
'0.0.5'

Instance Method Summary collapse

Instance Method Details

#time_for_a_boolean(attribute) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/time_for_a_boolean.rb', line 6

def time_for_a_boolean(attribute)
  define_method(attribute) do
    !send("#{attribute}_at").nil? &&
      send("#{attribute}_at") <= -> { Time.current }.()
  end
  alias_method "#{attribute}?", attribute
  define_method("#{attribute}=") do |value|
    if ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(value)
      send("#{attribute}_at=", -> { Time.current }.())
    else
      send("#{attribute}_at=", nil)
    end
  end
end