mdeering / attribute_normalizer

Adds the ability to normalize attributes cleanly with code blocks and predefined normalizers
MIT License
475 stars 53 forks source link

boolean normalizer and active record #44

Closed mauriciopasquier closed 10 years ago

mauriciopasquier commented 11 years ago

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 :)

mdeering commented 11 years ago

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.

mdeering commented 10 years ago

45 pull request sent.