diff options
author | James Lopez <james@jameslopez.es> | 2016-06-30 14:17:37 +0300 |
---|---|---|
committer | James Lopez <james@jameslopez.es> | 2016-06-30 14:17:37 +0300 |
commit | 5b893d603dd68f263129523f13e8eb68b67fe790 (patch) | |
tree | f17be7ff52f89672f6895c45d0c15e687e50527a /app/validators/addressable_url_validator.rb | |
parent | 0ca275748314a27a1f36e12fe1360df11c9be25d (diff) |
few changes based on feedback
Diffstat (limited to 'app/validators/addressable_url_validator.rb')
-rw-r--r-- | app/validators/addressable_url_validator.rb | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/app/validators/addressable_url_validator.rb b/app/validators/addressable_url_validator.rb index 634a15aea01..c97acf7da95 100644 --- a/app/validators/addressable_url_validator.rb +++ b/app/validators/addressable_url_validator.rb @@ -18,6 +18,9 @@ # end # class AddressableUrlValidator < ActiveModel::EachValidator + + DEFAULT_OPTIONS = { protocols: %w(http https ssh git) } + def validate_each(record, attribute, value) unless valid_url?(value) record.errors.add(attribute, "must be a valid URL") @@ -29,15 +32,9 @@ class AddressableUrlValidator < ActiveModel::EachValidator def valid_url?(value) return false unless value - value.strip! - valid_protocol?(value) && valid_uri?(value) end - def default_options - @default_options ||= { protocols: %w(http https ssh git) } - end - def valid_uri?(value) Addressable::URI.parse(value).is_a?(Addressable::URI) rescue Addressable::URI::InvalidURIError @@ -45,7 +42,7 @@ class AddressableUrlValidator < ActiveModel::EachValidator end def valid_protocol?(value) - options = default_options.merge(self.options) - !!(value =~ /\A#{URI.regexp(options[:protocols])}\z/) + options = DEFAULT_OPTIONS.merge(self.options) + value =~ /\A#{URI.regexp(options[:protocols])}\z/ end end |