]> git.openstreetmap.org Git - rails.git/commitdiff
Merge leading and trailing whitespace validators
authorTom Hughes <tom@compton.nu>
Mon, 5 Nov 2018 18:29:17 +0000 (18:29 +0000)
committerTom Hughes <tom@compton.nu>
Mon, 5 Nov 2018 18:29:17 +0000 (18:29 +0000)
app/models/user.rb
app/validators/leading_whitespace_validator.rb [deleted file]
app/validators/trailing_whitespace_validator.rb [deleted file]
app/validators/whitespace_validator.rb [new file with mode: 0644]
test/validators/leading_whitespace_validator_test.rb [deleted file]
test/validators/trailing_whitespace_validator_test.rb [deleted file]
test/validators/whitespace_validator_test.rb [new file with mode: 0644]

index 17985e05f45d1ad56d126b2886d5b20b81f5db48..a57c8837948a5f6f86a1c8a27972978a0d90ec9b 100644 (file)
@@ -95,8 +95,7 @@ class User < ActiveRecord::Base
   validates :display_name, :if => proc { |u| u.display_name_changed? },
                            :invalid_chars => true,
                            :invalid_url_chars => true,
-                           :leading_whitespace => true,
-                           :trailing_whitespace => true
+                           :whitespace => { :leading => false, :trailing => false }
   validates :email, :presence => true, :confirmation => true, :invalid_chars => true
   validates :email, :if => proc { |u| u.email_changed? },
                     :uniqueness => { :case_sensitive => false }
diff --git a/app/validators/leading_whitespace_validator.rb b/app/validators/leading_whitespace_validator.rb
deleted file mode 100644 (file)
index 90e7d9c..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-class LeadingWhitespaceValidator < ActiveModel::EachValidator
-  def validate_each(record, attribute, value)
-    record.errors[attribute] << (options[:message] || I18n.t("validations.leading_whitespace")) if value =~ /\A\s/
-  end
-end
diff --git a/app/validators/trailing_whitespace_validator.rb b/app/validators/trailing_whitespace_validator.rb
deleted file mode 100644 (file)
index aacc6dd..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-class TrailingWhitespaceValidator < ActiveModel::EachValidator
-  def validate_each(record, attribute, value)
-    record.errors[attribute] << (options[:message] || I18n.t("validations.trailing_whitespace")) if value =~ /\s\z/
-  end
-end
diff --git a/app/validators/whitespace_validator.rb b/app/validators/whitespace_validator.rb
new file mode 100644 (file)
index 0000000..d177df9
--- /dev/null
@@ -0,0 +1,11 @@
+class WhitespaceValidator < ActiveModel::EachValidator
+  def validate_each(record, attribute, value)
+    unless options.fetch(:leading, true)
+      record.errors[attribute] << (options[:message] || I18n.t("validations.leading_whitespace")) if value =~ /\A\s/
+    end
+
+    unless options.fetch(:trailing, true)
+      record.errors[attribute] << (options[:message] || I18n.t("validations.trailing_whitespace")) if value =~ /\s\z/
+    end
+  end
+end
diff --git a/test/validators/leading_whitespace_validator_test.rb b/test/validators/leading_whitespace_validator_test.rb
deleted file mode 100644 (file)
index e5c8374..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-require "test_helper"
-
-class LeadingWhitespaceValidatable
-  include ActiveModel::Validations
-  validates :string, :leading_whitespace => true
-  attr_accessor :string
-end
-
-class LeadingWhitespaceValidatorTest < ActiveSupport::TestCase
-  include Rails::Dom::Testing::Assertions::SelectorAssertions
-
-  def test_with_leading_whitespace
-    validator = LeadingWhitespaceValidatable.new
-
-    strings = [" ", " test", "  ", "\ttest"]
-
-    strings.each do |v|
-      validator.string = v
-      assert_not validator.valid?, "'#{v}' should not be valid"
-    end
-  end
-
-  def test_without_leading_whitespace
-    validator = LeadingWhitespaceValidatable.new
-
-    strings = ["test", "test ", "t est", "test\t", ".test", "_test"]
-
-    strings.each do |v|
-      validator.string = v
-      assert validator.valid?, "'#{v}' should be valid"
-    end
-  end
-end
diff --git a/test/validators/trailing_whitespace_validator_test.rb b/test/validators/trailing_whitespace_validator_test.rb
deleted file mode 100644 (file)
index 5aac3be..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-require "test_helper"
-
-class TrailingWhitespaceValidatable
-  include ActiveModel::Validations
-  validates :string, :trailing_whitespace => true
-  attr_accessor :string
-end
-
-class TrailingWhitespaceValidatorTest < ActiveSupport::TestCase
-  include Rails::Dom::Testing::Assertions::SelectorAssertions
-
-  def test_with_trailing_whitespace
-    validator = TrailingWhitespaceValidatable.new
-
-    strings = [" ", "test ", "  ", "test\t", "_test_ "]
-
-    strings.each do |v|
-      validator.string = v
-      assert_not validator.valid?, "'#{v}' should not be valid"
-    end
-  end
-
-  def test_without_trailing_whitespace
-    validator = TrailingWhitespaceValidatable.new
-
-    strings = ["test", " test", "tes t", "\ttest", "test.", "test_"]
-
-    strings.each do |v|
-      validator.string = v
-      assert validator.valid?, "'#{v}' should be valid"
-    end
-  end
-end
diff --git a/test/validators/whitespace_validator_test.rb b/test/validators/whitespace_validator_test.rb
new file mode 100644 (file)
index 0000000..c908538
--- /dev/null
@@ -0,0 +1,61 @@
+require "test_helper"
+
+class LeadingWhitespaceValidatable
+  include ActiveModel::Validations
+  validates :string, :whitespace => { :leading => false }
+  attr_accessor :string
+end
+
+class TrailingWhitespaceValidatable
+  include ActiveModel::Validations
+  validates :string, :whitespace => { :trailing => false }
+  attr_accessor :string
+end
+
+class WhitespaceValidatorTest < ActiveSupport::TestCase
+  include Rails::Dom::Testing::Assertions::SelectorAssertions
+
+  def test_with_leading_whitespace
+    validator = LeadingWhitespaceValidatable.new
+
+    strings = [" ", " test", "  ", "\ttest"]
+
+    strings.each do |v|
+      validator.string = v
+      assert_not validator.valid?, "'#{v}' should not be valid"
+    end
+  end
+
+  def test_without_leading_whitespace
+    validator = LeadingWhitespaceValidatable.new
+
+    strings = ["test", "test ", "t est", "test\t", ".test", "_test"]
+
+    strings.each do |v|
+      validator.string = v
+      assert validator.valid?, "'#{v}' should be valid"
+    end
+  end
+
+  def test_with_trailing_whitespace
+    validator = TrailingWhitespaceValidatable.new
+
+    strings = [" ", "test ", "  ", "test\t", "_test_ "]
+
+    strings.each do |v|
+      validator.string = v
+      assert_not validator.valid?, "'#{v}' should not be valid"
+    end
+  end
+
+  def test_without_trailing_whitespace
+    validator = TrailingWhitespaceValidatable.new
+
+    strings = ["test", " test", "tes t", "\ttest", "test.", "test_"]
+
+    strings.each do |v|
+      validator.string = v
+      assert validator.valid?, "'#{v}' should be valid"
+    end
+  end
+end