Module: ActiveRecord::DatabaseValidations::StringTruncator::ClassMethods

Defined in:
lib/active_record/validations/string_truncator.rb

Instance Method Summary collapse

Instance Method Details

#truncate_string(field) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/active_record/validations/string_truncator.rb', line 30

def truncate_string(field)
  method_name = :"truncate_#{field}_at_database_limit"
  define_method(method_name) do
    return unless self.changes.key?(field.to_s)
    self[field] = truncate_value_to_field_limit(field, self[field])
    return # to make sure the callback chain doesn't halt
  end
  return method_name
end

#truncate_to_field_limit(field) ⇒ Object



24
25
26
27
28
# File 'lib/active_record/validations/string_truncator.rb', line 24

def truncate_to_field_limit(field)
  define_method(:"#{field}=") do |value|
    super(truncate_value_to_field_limit(field, value))
  end
end