Module: NullifyEmpty::ActiveRecordExtension

Defined in:
lib/nullify_empty/active_record_extension.rb

Instance Method Summary collapse

Instance Method Details

#nullify_empty(*attr_names) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/nullify_empty/active_record_extension.rb', line 3

def nullify_empty(*attr_names)
  return unless connection.table_exists? table_name

  (column_names & attr_names.map(&:to_s)).each do |attr_name|
    original_type_klass = attribute_types[attr_name.to_s].class

    new_type_klass =
      NULLIFY_EMPTY_TYPES.find { |_, klass| klass.superclass == original_type_klass }.first
    next unless new_type_klass

    attribute attr_name, new_type_klass
  end
rescue
  # ignore
end