# Offense count: 26
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
- Max: 313
+ Max: 314
# Offense count: 59
# Configuration parameters: AllowedMethods, AllowedPatterns.
# Used to resize user images
gem "image_processing"
+# Used to validate widths
+gem "unicode-display_width"
+
# Gems useful for development
group :development do
gem "better_errors"
sprockets-exporters_pack
strong_migrations
terser
+ unicode-display_width
validates_email_format_of (>= 1.5.1)
vendorer
webmock
:normalized_uniqueness => { :case_sensitive => false }
validates :display_name, :if => proc { |u| u.display_name_changed? },
:characters => { :url_safe => true },
- :whitespace => { :leading => false, :trailing => false }
+ :whitespace => { :leading => false, :trailing => false },
+ :width => { :minimum => 3 }
validate :display_name_cannot_be_user_id_with_other_id, :if => proc { |u| u.display_name_changed? }
validates :email, :presence => true, :confirmation => true, :characters => true
validates :email, :if => proc { |u| u.email_changed? },
--- /dev/null
+class WidthValidator < ActiveModel::Validations::LengthValidator
+ module WidthAsLength
+ def length
+ Unicode::DisplayWidth.of(to_s)
+ end
+ end
+
+ def validate_each(record, attribute, value)
+ super(record, attribute, value.extend(WidthAsLength))
+ end
+end
assert_not_predicate user, :valid?, "should not allow nil value"
end
+ def test_display_name_width
+ user = build(:user)
+ user.display_name = "123"
+ assert_predicate user, :valid?, "should allow 3 column name name"
+ user.display_name = "12"
+ assert_not_predicate user, :valid?, "should not allow 2 column name"
+ user.display_name = "1\u{200B}2"
+ assert_not_predicate user, :valid?, "should not allow 2 column name"
+ user.display_name = "\u{200B}\u{200B}\u{200B}"
+ assert_not_predicate user, :valid?, "should not allow 0 column name"
+ end
+
def test_display_name_valid
# Due to sanitisation in the view some of these that you might not
# expect are allowed