From 20a2aa9611fc22bd78771692eaff687fe480e9ff Mon Sep 17 00:00:00 2001 From: thomas morgan Date: Sat, 15 Feb 2020 20:20:55 -0700 Subject: [PATCH] avoid setting non-Strings to nil --- lib/strip_attributes.rb | 2 ++ test/strip_attributes_test.rb | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/strip_attributes.rb b/lib/strip_attributes.rb index 2f85a5c..2a7a83d 100644 --- a/lib/strip_attributes.rb +++ b/lib/strip_attributes.rb @@ -58,6 +58,8 @@ def self.strip_record(record, options = {}) end def self.strip_string(value, options = {}) + return value unless value.is_a?(String) + allow_empty = options[:allow_empty] collapse_spaces = options[:collapse_spaces] replace_newlines = options[:replace_newlines] diff --git a/test/strip_attributes_test.rb b/test/strip_attributes_test.rb index 9efe6bb..317870e 100644 --- a/test/strip_attributes_test.rb +++ b/test/strip_attributes_test.rb @@ -61,6 +61,17 @@ class ReplaceNewLinesAndDuplicateSpaces < Tableless strip_attributes replace_newlines: true, collapse_spaces: true end +class CoexistWithOtherObjects < Tableless + attr_accessor :arr, :hsh, :str + strip_attributes + def initialize + @arr, @hsh, @str = [], {}, "foo " + end + def attributes + {arr: arr, hsh: hsh, str: str} + end +end + class CoexistWithOtherValidations < Tableless attribute :number, type: Integer @@ -242,6 +253,14 @@ def test_should_strip_and_allow_empty_always assert_equal "", record.bang end + def test_should_allow_other_empty_objects + record = CoexistWithOtherObjects.new + record.valid? + assert_equal [], record.arr + assert_equal({}, record.hsh) + assert_equal "foo", record.str + end + def test_should_coexist_with_other_validations record = CoexistWithOtherValidations.new record.number = 1000.1