diff options
Diffstat (limited to 'vendor/plugins/strip_attributes')
-rw-r--r-- | vendor/plugins/strip_attributes/lib/strip_attributes.rb | 16 | ||||
-rw-r--r-- | vendor/plugins/strip_attributes/test/strip_attributes_test.rb | 6 |
2 files changed, 12 insertions, 10 deletions
diff --git a/vendor/plugins/strip_attributes/lib/strip_attributes.rb b/vendor/plugins/strip_attributes/lib/strip_attributes.rb index 70f414654..bb93aa9a7 100644 --- a/vendor/plugins/strip_attributes/lib/strip_attributes.rb +++ b/vendor/plugins/strip_attributes/lib/strip_attributes.rb @@ -3,10 +3,12 @@ module StripAttributes # XXX this differs from official StripAttributes, as it doesn't make blank cells null. def strip_attributes!(options = nil) before_validation do |record| - attributes = StripAttributes.narrow(record.attributes, options) - attributes.each do |attr, value| + attribute_names = StripAttributes.narrow(record.attribute_names, options) + + attribute_names.each do |attribute_name| + value = record[attribute_name] if value.respond_to?(:strip) - record[attr] = (value.nil?) ? nil : value.strip + record[attribute_name] = (value.nil?) ? nil : value.strip end end end @@ -14,16 +16,16 @@ module StripAttributes # Necessary because Rails has removed the narrowing of attributes using :only # and :except on Base#attributes - def self.narrow(attributes, options) + def self.narrow(attribute_names, options) if options.nil? - attributes + attribute_names else if except = options[:except] except = Array(except).collect { |attribute| attribute.to_s } - attributes.except(*except) + attribute_names - except elsif only = options[:only] only = Array(only).collect { |attribute| attribute.to_s } - attributes.slice(*only) + attribute_names & only else raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})" end diff --git a/vendor/plugins/strip_attributes/test/strip_attributes_test.rb b/vendor/plugins/strip_attributes/test/strip_attributes_test.rb index 95754fca7..8158dc664 100644 --- a/vendor/plugins/strip_attributes/test/strip_attributes_test.rb +++ b/vendor/plugins/strip_attributes/test/strip_attributes_test.rb @@ -49,7 +49,7 @@ class StripAttributesTest < Test::Unit::TestCase assert_equal "foo", record.foo assert_equal "bar", record.bar assert_equal "biz", record.biz - assert_nil record.baz + assert_equal "", record.baz end def test_should_strip_only_one_field @@ -76,7 +76,7 @@ class StripAttributesTest < Test::Unit::TestCase assert_equal "\tfoo", record.foo assert_equal "bar", record.bar assert_equal "biz", record.biz - assert_nil record.baz + assert_equal "", record.baz end def test_should_strip_all_except_three_fields @@ -85,6 +85,6 @@ class StripAttributesTest < Test::Unit::TestCase assert_equal "\tfoo", record.foo assert_equal "bar \t ", record.bar assert_equal "\tbiz ", record.biz - assert_nil record.baz + assert_equal "", record.baz end end |