]> git.openstreetmap.org Git - rails.git/blobdiff - app/validators/width_validator.rb
Require display names to have a minimum unicode width of 3 columns
[rails.git] / app / validators / width_validator.rb
diff --git a/app/validators/width_validator.rb b/app/validators/width_validator.rb
new file mode 100644 (file)
index 0000000..a3a4d41
--- /dev/null
@@ -0,0 +1,11 @@
+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