Module: BooleanValue

Defined in:
lib/bit_settings/boolean_value.rb

Constant Summary collapse

FALSE_VALUES =
[false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF']

Class Method Summary collapse

Class Method Details

.cast_to_boolean_funcObject



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

def self.cast_to_boolean_func

  major, minor, _ = ActiveModel.version.to_s.split('.').map(&:to_i)
  case major
  # Rails 5
  when 5
    return ->(value) { ActiveModel::Type::Boolean.new.cast(value) }
  # Rails 4.2
  when 4
    if minor >= 2
      return ->(value) { ActiveRecord::Type::Boolean.new.type_cast_from_user(value) }
    end
  end
  return ->(value) { value == '' ? nil : !FALSE_VALUES.include?(value) }
end