about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-01-15 14:04:56 +0100
committerEugen Rochko <eugen@zeonfederated.com>2017-01-15 14:04:56 +0100
commitbfec9aaee077c6dd54081a89f697271d7a5c0a6a (patch)
treeececd729f513fd14998f2be30169973956ef6f59 /app/lib
parent1f7c0ad8d3d336b036d16272716e41812f65f5d9 (diff)
parente9737c2235ec56502e650bd1adad3f32bf85f0ef (diff)
Merge branch 'ineffyble-feature/toot-app-source'
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/application_extension.rb9
-rw-r--r--app/lib/url_validator.rb14
2 files changed, 23 insertions, 0 deletions
diff --git a/app/lib/application_extension.rb b/app/lib/application_extension.rb
new file mode 100644
index 000000000..93c0f42f0
--- /dev/null
+++ b/app/lib/application_extension.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module ApplicationExtension
+  extend ActiveSupport::Concern
+
+  included do
+    validates :website, url: true, unless: 'website.blank?'
+  end
+end
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