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

VERSION =
'0.1.0'

Instance Method Summary collapse

Instance Method Details

#time_for_a_boolean(attribute, field = "#{attribute}_at") ⇒ Object



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

def time_for_a_boolean(attribute, field="#{attribute}_at")
  define_method(attribute) do
    !send(field).nil? && send(field) <= -> { Time.current }.()
  end

  alias_method "#{attribute}?", attribute

  setter_attribute = "#{field}="
  define_method("#{attribute}=") do |value|
    if ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(value)
      send(setter_attribute, -> { Time.current }.())
    else
      send(setter_attribute, nil)
    end
  end
end