From bd4990dd21f011c78ec9ef04102b078c41e720be Mon Sep 17 00:00:00 2001 From: Ryan Sonnek Date: Tue, 15 Jun 2010 12:49:39 -0500 Subject: [PATCH] fix exception when field is nil --- lib/acts_as_stripped.rb | 2 +- test/acts_as_stripped_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/acts_as_stripped.rb b/lib/acts_as_stripped.rb index b10fd67..3d6244c 100644 --- a/lib/acts_as_stripped.rb +++ b/lib/acts_as_stripped.rb @@ -22,7 +22,7 @@ module InstanceMethods private def strip_fields strippable_attributes.each do |attr| - self[attr.to_s].strip! + self[attr.to_s].strip! unless self[attr.to_s].nil? end end def strippable_attributes diff --git a/test/acts_as_stripped_test.rb b/test/acts_as_stripped_test.rb index a60d346..b32999a 100644 --- a/test/acts_as_stripped_test.rb +++ b/test/acts_as_stripped_test.rb @@ -53,6 +53,14 @@ class ActsAsStrippedTest < Test::Unit::TestCase setup do @post = Post.new end + should 'not fail if title is nil' do + @post.title = nil + assert_nothing_raised do + @post.save! + end + assert_nil @post.title + end + should 'strip whitespace only from title' do @post.title = ' hello world ' @post.body = ' awesome '