Skip to content

Commit

Permalink
avoid setting non-Strings to nil
Browse files Browse the repository at this point in the history
  • Loading branch information
zarqman authored and rmm5t committed Feb 16, 2020
1 parent ce6dc05 commit 20a2aa9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/strip_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
19 changes: 19 additions & 0 deletions test/strip_attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 20a2aa9

Please sign in to comment.