From b4ef61a9f31ac8bb911c094bc2fb00a9fa2102a6 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Mon, 5 Nov 2018 18:29:17 +0000 Subject: [PATCH] Merge leading and trailing whitespace validators --- app/models/user.rb | 3 +- .../leading_whitespace_validator.rb | 5 -- .../trailing_whitespace_validator.rb | 5 -- app/validators/whitespace_validator.rb | 11 ++++ .../leading_whitespace_validator_test.rb | 33 ---------- .../trailing_whitespace_validator_test.rb | 33 ---------- test/validators/whitespace_validator_test.rb | 61 +++++++++++++++++++ 7 files changed, 73 insertions(+), 78 deletions(-) delete mode 100644 app/validators/leading_whitespace_validator.rb delete mode 100644 app/validators/trailing_whitespace_validator.rb create mode 100644 app/validators/whitespace_validator.rb delete mode 100644 test/validators/leading_whitespace_validator_test.rb delete mode 100644 test/validators/trailing_whitespace_validator_test.rb create mode 100644 test/validators/whitespace_validator_test.rb diff --git a/app/models/user.rb b/app/models/user.rb index 17985e05f..a57c88379 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 index 90e7d9c95..000000000 --- a/app/validators/leading_whitespace_validator.rb +++ /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 index aacc6dd04..000000000 --- a/app/validators/trailing_whitespace_validator.rb +++ /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 index 000000000..d177df92a --- /dev/null +++ b/app/validators/whitespace_validator.rb @@ -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 index e5c8374df..000000000 --- a/test/validators/leading_whitespace_validator_test.rb +++ /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 index 5aac3bef2..000000000 --- a/test/validators/trailing_whitespace_validator_test.rb +++ /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 index 000000000..c908538d1 --- /dev/null +++ b/test/validators/whitespace_validator_test.rb @@ -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 -- 2.39.5