]> git.openstreetmap.org Git - rails.git/commitdiff
Strip leading/trailing whitespace from comma-separated trace tags
authorAnton Khorev <tony29@yandex.ru>
Tue, 19 Jul 2022 10:47:28 +0000 (13:47 +0300)
committerAnton Khorev <tony29@yandex.ru>
Tue, 19 Jul 2022 10:47:28 +0000 (13:47 +0300)
app/models/trace.rb
test/models/trace_test.rb

index 957bea8bc043429b5df7c72e3b0c97c55a86d87e..0b9877225b889c11d942aa1eb6f9b13feae8caec 100644 (file)
@@ -57,7 +57,7 @@ class Trace < ApplicationRecord
 
   def tagstring=(s)
     self.tags = if s.include? ","
 
   def tagstring=(s)
     self.tags = if s.include? ","
-                  s.split(/\s*,\s*/).grep_v(/^\s*$/).collect do |tag|
+                  s.split(",").map(&:strip).reject(&:empty?).collect do |tag|
                     tt = Tracetag.new
                     tt.tag = tag
                     tt
                     tt = Tracetag.new
                     tt.tag = tag
                     tt
index 2dcae58d0dc372f91ae3c10758809ec147d5d50a..8fa09f4d7233c77858b1bf3a0c90a9ae88e24651 100644 (file)
@@ -73,7 +73,7 @@ class TraceTest < ActiveSupport::TestCase
     trace_valid({ :visibility => "foo" }, :valid => false)
   end
 
     trace_valid({ :visibility => "foo" }, :valid => false)
   end
 
-  def test_tagstring
+  def test_tagstring_handles_space_separated_tags
     trace = build(:trace)
     trace.tagstring = "foo bar baz"
     assert_predicate trace, :valid?
     trace = build(:trace)
     trace.tagstring = "foo bar baz"
     assert_predicate trace, :valid?
@@ -82,6 +82,10 @@ class TraceTest < ActiveSupport::TestCase
     assert_equal "bar", trace.tags[1].tag
     assert_equal "baz", trace.tags[2].tag
     assert_equal "foo, bar, baz", trace.tagstring
     assert_equal "bar", trace.tags[1].tag
     assert_equal "baz", trace.tags[2].tag
     assert_equal "foo, bar, baz", trace.tagstring
+  end
+
+  def test_tagstring_handles_comma_separated_tags
+    trace = build(:trace)
     trace.tagstring = "foo, bar baz ,qux"
     assert_predicate trace, :valid?
     assert_equal 3, trace.tags.length
     trace.tagstring = "foo, bar baz ,qux"
     assert_predicate trace, :valid?
     assert_equal 3, trace.tags.length
@@ -91,6 +95,17 @@ class TraceTest < ActiveSupport::TestCase
     assert_equal "foo, bar baz, qux", trace.tagstring
   end
 
     assert_equal "foo, bar baz, qux", trace.tagstring
   end
 
+  def test_tagstring_strips_whitespace
+    trace = build(:trace)
+    trace.tagstring = "   zero  ,  one , two  "
+    assert_predicate trace, :valid?
+    assert_equal 3, trace.tags.length
+    assert_equal "zero", trace.tags[0].tag
+    assert_equal "one", trace.tags[1].tag
+    assert_equal "two", trace.tags[2].tag
+    assert_equal "zero, one, two", trace.tagstring
+  end
+
   def test_public?
     assert_predicate build(:trace, :visibility => "public"), :public?
     assert_not build(:trace, :visibility => "private").public?
   def test_public?
     assert_predicate build(:trace, :visibility => "public"), :public?
     assert_not build(:trace, :visibility => "private").public?