Require attribute

#660
This commit is contained in:
Artur Beljajev 2018-01-30 01:37:31 +02:00
parent 00041df311
commit 80327d3447
4 changed files with 17 additions and 2 deletions

View file

@ -5,7 +5,7 @@ class Nameserver < ActiveRecord::Base
belongs_to :domain, required: true
# rubocop: disable Metrics/LineLength
validates :hostname, format: { with: /\A(([a-zA-Z0-9]|[a-zA-ZäöüõšžÄÖÜÕŠŽ0-9][a-zA-ZäöüõšžÄÖÜÕŠŽ0-9\-]*[a-zA-ZäöüõšžÄÖÜÕŠŽ0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\z/ }
validates :hostname, presence: true, format: { with: /\A(([a-zA-Z0-9]|[a-zA-ZäöüõšžÄÖÜÕŠŽ0-9][a-zA-ZäöüõšžÄÖÜÕŠŽ0-9\-]*[a-zA-ZäöüõšžÄÖÜÕŠŽ0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\z/ }
validate :val_ipv4
validate :val_ipv6
# rubocop: enable Metrics/LineLength
@ -40,6 +40,8 @@ class Nameserver < ActiveRecord::Base
end
def check_label_length
return unless hostname
hostname_puny.split('.').each do |label|
errors.add(:hostname, :puny_to_long) if label.length > 63
end

View file

@ -0,0 +1,5 @@
class ChangeNameserversHostnameToNotNull < ActiveRecord::Migration
def change
change_column_null :nameservers, :hostname, false
end
end

View file

@ -2221,7 +2221,7 @@ ALTER SEQUENCE messages_id_seq OWNED BY messages.id;
CREATE TABLE nameservers (
id integer NOT NULL,
hostname character varying,
hostname character varying NOT NULL,
ipv4 character varying[] DEFAULT '{}'::character varying[],
created_at timestamp without time zone,
updated_at timestamp without time zone,
@ -5082,3 +5082,5 @@ INSERT INTO schema_migrations (version) VALUES ('20180129143538');
INSERT INTO schema_migrations (version) VALUES ('20180129232054');
INSERT INTO schema_migrations (version) VALUES ('20180129233223');

View file

@ -14,4 +14,10 @@ class NameserverTest < ActiveSupport::TestCase
@nameserver.validate
assert @nameserver.invalid?
end
def test_invalid_without_hostname
@nameserver.hostname = nil
@nameserver.validate
assert @nameserver.invalid?
end
end