Support IP addresses for embeddable hosts

This commit is contained in:
Robin Ward 2016-01-11 11:06:09 -05:00
parent 38c63c2185
commit 2a84db73b5
2 changed files with 15 additions and 1 deletions

View File

@ -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

View File

@ -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) }