From ac2fb2330a564ba52761ed2b06036c61fc9f07d6 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Wed, 12 Dec 2012 10:34:09 +0000 Subject: Added field truncation + warning logs. --- lib/mauve/alert.rb | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/mauve/alert.rb b/lib/mauve/alert.rb index 3307b59..9d04250 100644 --- a/lib/mauve/alert.rb +++ b/lib/mauve/alert.rb @@ -118,7 +118,8 @@ module Mauve has 1, :alert_earliest_date before :valid?, :do_set_timestamps - before :save, :do_sanitize_html + before :valid?, :do_truncate_fields + before :save, :do_sanitize_html after :destroy, :destroy_associations @@ -264,6 +265,25 @@ module Mauve end end + # + # This truncates strings to their maximum allowed length. + # + def do_truncate_fields + attributes.each do |key, val| + prop = self.class.properties[key] + next unless prop.is_a?(DataMapper::Property::String) + + # + # Truncate + # + max_length = prop.length + if val.length > max_length + logger.warn "#{self.to_s} truncating #{key} to #{max_length} chars" + attribute_set(key, val[0...max_length]) + end + end + end + def do_set_timestamps(context = :default) self.updated_at = Time.now if self.dirty? end -- cgit v1.2.1