From 2a84db73b5529b8b68d084d480aeb5df86b9dcd3 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 11 Jan 2016 11:06:09 -0500 Subject: [PATCH] Support IP addresses for embeddable hosts --- app/models/embeddable_host.rb | 10 +++++++++- spec/models/embeddable_host_spec.rb | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/models/embeddable_host.rb b/app/models/embeddable_host.rb index ee51105bcd4..98eb59a2f54 100644 --- a/app/models/embeddable_host.rb +++ b/app/models/embeddable_host.rb @@ -1,5 +1,5 @@ class EmbeddableHost < ActiveRecord::Base - validates_format_of :host, :with => /\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,7}(:[0-9]{1,5})?(\/.*)?\Z/i + validate :host_must_be_valid belongs_to :category before_validation do @@ -21,6 +21,14 @@ class EmbeddableHost < ActiveRecord::Base record_for_host(host).present? end + private + + def host_must_be_valid + if host !~ /\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,7}(:[0-9]{1,5})?(\/.*)?\Z/i && + host !~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/ + errors.add(:host, I18n.t('errors.messages.invalid')) + end + end end # == Schema Information diff --git a/spec/models/embeddable_host_spec.rb b/spec/models/embeddable_host_spec.rb index 2a4c306a09a..4ee44c56085 100644 --- a/spec/models/embeddable_host_spec.rb +++ b/spec/models/embeddable_host_spec.rb @@ -20,6 +20,12 @@ describe EmbeddableHost do expect(eh.host).to eq('example.com') end + it "supports ip addresses" do + eh = EmbeddableHost.new(host: '192.168.0.1') + expect(eh).to be_valid + expect(eh.host).to eq('192.168.0.1') + end + describe "allows_embeddable_host" do let!(:host) { Fabricate(:embeddable_host) }