Class: CharacterEncodingValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/character_encoding_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/character_encoding_validator.rb', line 2

def validate(record)
  record.attributes.each do |attribute, value|
    next unless value.is_a?(String)
    begin
      value.encode(options[:encoding])
    rescue Encoding::UndefinedConversionError => e
      record.errors.add(attribute, :undefined_conversion)
    rescue Encoding::InvalidByteSequenceError => e
      record.errors.add(attribute, :invalid_byte_sequence)
    end
  end
end