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.3.0'
Instance Method Summary collapse
Instance Method Details
#time_for_a_boolean(attribute, field = :"#{attribute}_at", scopes: false) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/time_for_a_boolean.rb', line 9 def time_for_a_boolean(attribute, field=:"#{attribute}_at", scopes: false) 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 ActiveModel::Type::Boolean::FALSE_VALUES.include?(value) send(setter_attribute, nil) else send(setter_attribute, -> { Time.current }.()) end end define_method(:"#{attribute}!") do send(:"#{attribute}=", true) end if scopes && respond_to?(:where) singleton_class.instance_eval do define_method(:"#{attribute}") do where(field => ..Time.current) end define_method(:"not_#{attribute}") do where(field => [nil, Time.current...]) end end end end |