I'm using this custom normalizer with virtual boolean attributes
# Changes true, 'true', 1 and '1' into true
config.normalizers[:boolean] = lambda do |value, options|
ActiveRecord::ConnectionAdapters::Column.value_to_boolean value
end
I was about to send a pull request but I'm not sure if it's ok to add this ActiveRecord dependency. If it's not, I could use the code directly. It's something like this:
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].to_set
def value_to_boolean(value)
if value.is_a?(String) && value.blank?
nil
else
TRUE_VALUES.include?(value)
end
end
Disregard all of this if you don't want to add a normalizer useful only for virtual boolean attributes :)
I have used the raw code like in your second code snippet several times in the past. Yes, I would take in the pull request for the new boolean normalizer done without the Active Record dependencies.
I'm using this custom normalizer with virtual boolean attributes
I was about to send a pull request but I'm not sure if it's ok to add this ActiveRecord dependency. If it's not, I could use the code directly. It's something like this:
Disregard all of this if you don't want to add a normalizer useful only for virtual boolean attributes :)