about summary refs log tree commit diff
path: root/app/lib/url_validator.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-01-15 14:01:33 +0100
committerEugen Rochko <eugen@zeonfederated.com>2017-01-15 14:01:33 +0100
commite9737c2235ec56502e650bd1adad3f32bf85f0ef (patch)
treeececd729f513fd14998f2be30169973956ef6f59 /app/lib/url_validator.rb
parentab165547fdf556b10b80898c030d54e20bff50af (diff)
Fix tests, add applications to eager loading/cache for statuses, fix
application website validation, don't link to app website if website isn't set,
also comment out animated boost icon from #464 until it's consistent with non-animated version
Diffstat (limited to 'app/lib/url_validator.rb')
-rw-r--r--app/lib/url_validator.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/app/lib/url_validator.rb b/app/lib/url_validator.rb
new file mode 100644
index 000000000..4a5c4ef3f
--- /dev/null
+++ b/app/lib/url_validator.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+class UrlValidator < ActiveModel::EachValidator
+  def validate_each(record, attribute, value)
+    record.errors.add(attribute, I18n.t('applications.invalid_url')) unless compliant?(value)
+  end
+
+  private
+
+  def compliant?(url)
+    parsed_url = Addressable::URI.parse(url)
+    !parsed_url.nil? && %w(http https).include?(parsed_url.scheme) && parsed_url.host
+  end
+end