Skip to content

Instantly share code, notes, and snippets.

View chunpan's full-sized avatar

Chun Pan (Billy) chunpan

View GitHub Profile
@chunpan
chunpan / auto_truncate_string_column.rb
Last active April 21, 2021 22:17
Ruby Module to Allow Auto Truncation of String Columns using ActiveRecord Callbacks
require 'active_support/core_ext/class/attribute'
require 'active_support/concern'
# This module enables a simple declaration for the including ActiveRecord model
# class to specify one or more columns (attributes) to automatically truncate
# its length to a max value or as defined by the column width in your schema.
#
# Usage example:
#
# class MyModel << ActiveRecord::Base