Sha256: b2dce286b8f9b32ec177c91ee4e900051c27cc2d949f8c7d182186e5d361a7fb
Contents?: true
Size: 1.33 KB
Versions: 12
Compression:
Stored size: 1.33 KB
Contents
# An ActiveRecord extension that will let you automatically truncate # certain attributes to the maximum length allowed by the DB. # # require 'truncate_to_db_limit' # class Something < ActiveRecord::Base # include TruncateToDbLimit # truncate_to_db_limit :short_attr, :short_attr2 # #... # # Truncation is done in a before_validate hook, so won't happen until # you try to save. # module TruncateToDbLimit extend ActiveSupport::Concern included do class_attribute :'_truncate_to_db_limit_attributes', instance_accessor: false before_validation :do_truncate_to_db_limit! def self.truncate_to_db_limit(*attribute_names) self._truncate_to_db_limit_attributes = attribute_names end end def do_truncate_to_db_limit! self.class._truncate_to_db_limit_attributes.each do |attribute_name| ar_attr = self.class.columns_hash[attribute_name.to_s] unless ar_attr raise ArgumentError.new("truncate_to_db_limit #{attribute_name}: No such attribute") end limit = ar_attr.limit unless limit && limit.to_i != 0 raise ArgumentError.new("truncate_to_db_limit #{attribute_name}: Limit not known") end normalized = send(attribute_name).try {|v| v.slice(0, limit)} send("#{attribute_name}=", normalized) end end end
Version data entries
12 entries across 12 versions & 1 rubygems