Sha256: 9e2d89ae42ad6f967af2355307ba6a5a993f031712067a87814712a9a4bc4a62
Contents?: true
Size: 1.21 KB
Versions: 2
Compression:
Stored size: 1.21 KB
Contents
module AttributeExtras module BaseExtensions # overrides the writer to truncate if that value is blank def truncate_attributes(*attributes, validator: false, writer: true) string_columns = self.columns.select { |column| column.type == :string && !column.limit.nil? }.map(&:name) if self.table_exists? and (non_attributes = attributes.map(&:to_s) - string_columns).any? raise ArgumentError, <<-MSG.squish Invalid attributes passed to truncate_attributes: #{non_attributes.join(', ')}; attributes must be string columns that have a set limit MSG end include ::AttributeExtras::TruncateAttributes truncated_attributes attributes.each do |attribute| limit = self.columns_hash[attribute.to_s].limit if validator validates attribute, length: { maximum: limit } end if writer define_method("#{attribute}=") do |value| write_attribute(attribute, ::AttributeExtras::TruncateAttributes::FUNCTION[value, limit: limit]) end end truncated_attributes << Modifier.new(attribute, limit: limit) end end end end ActiveRecord::Base.extend AttributeExtras::BaseExtensions
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
attribute_extras-0.1.1 | lib/attribute_extras/base_extensions.rb |
attribute_extras-0.1.0 | lib/attribute_extras/base_extensions.rb |