Dynamic validations example

This commit is contained in:
Martin Lensment 2014-08-18 14:54:46 +03:00
parent 0c5b7c4cb8
commit 415f109d8f
7 changed files with 75 additions and 48 deletions

View file

@ -127,8 +127,10 @@ class Domain < ActiveRecord::Base
### VALIDATIONS ### ### VALIDATIONS ###
def validate_nameservers_count def validate_nameservers_count
unless nameservers.length.between?(1, 13) sg = SettingGroup.find_by(code: SettingGroup::DOMAIN_VALIDATION_CODE)
errors.add(:nameservers, :out_of_range, {min: 1, max: 13}) min, max = sg.get(:ns_min_count).to_i, sg.get(:ns_max_count).to_i
unless nameservers.length.between?(min, max)
errors.add(:nameservers, :out_of_range, {min: min, max: max})
end end
end end

View file

@ -2,4 +2,11 @@ class SettingGroup < ActiveRecord::Base
has_many :settings has_many :settings
accepts_nested_attributes_for :settings accepts_nested_attributes_for :settings
DOMAIN_VALIDATION_CODE = 'domain_validation'
def get(key)
s = settings.find_by(code: key.to_s)
s.try(:value)
end
end end

View file

@ -4,7 +4,10 @@ describe 'EPP Domain', epp: true do
let(:server) { server = Epp::Server.new({server: 'localhost', tag: 'gitlab', password: 'ghyt9e4fu', port: 701}) } let(:server) { server = Epp::Server.new({server: 'localhost', tag: 'gitlab', password: 'ghyt9e4fu', port: 701}) }
context 'with valid user' do context 'with valid user' do
before(:each) { Fabricate(:epp_user) } before(:each) do
Fabricate(:epp_user)
Fabricate(:domain_validation_setting_group)
end
it 'returns error if contact does not exists' do it 'returns error if contact does not exists' do
Fabricate(:contact, code: 'jd1234') Fabricate(:contact, code: 'jd1234')

View file

@ -5,3 +5,11 @@ Fabricator(:setting_group) do
Fabricate(:setting, code: 'ns_max_count', value: 13) Fabricate(:setting, code: 'ns_max_count', value: 13)
]} ]}
end end
Fabricator(:domain_validation_setting_group, from: :setting_group) do
code 'domain_validation'
settings { [
Fabricate(:setting, code: 'ns_min_count', value: 1),
Fabricate(:setting, code: 'ns_max_count', value: 13)
]}
end

View file

@ -1,7 +1,7 @@
require 'rails_helper' require 'rails_helper'
feature 'Setting management' do feature 'Setting management' do
background { Fabricate(:setting_group) } background { Fabricate(:domain_validation_setting_group) }
scenario 'User changes a setting', js: true do scenario 'User changes a setting', js: true do
visit root_path visit root_path

View file

@ -51,7 +51,11 @@ describe Contact, '#get_relation' do
end end
describe Contact, '#has_relation' do describe Contact, '#has_relation' do
before(:each) { Fabricate(:domain) } before(:each) do
Fabricate(:domain_validation_setting_group)
Fabricate(:domain)
end
it 'should return false if no relation' do it 'should return false if no relation' do
expect(Contact.last.has_relation(:chewbacca)).to eq false expect(Contact.last.has_relation(:chewbacca)).to eq false
end end

View file

@ -7,6 +7,9 @@ describe Domain do
it { should have_many(:tech_contacts) } it { should have_many(:tech_contacts) }
it { should have_many(:admin_contacts) } it { should have_many(:admin_contacts) }
context 'with sufficient settings' do
before(:each) { Fabricate(:domain_validation_setting_group) }
it 'validates domain name' do it 'validates domain name' do
d = Fabricate(:domain) d = Fabricate(:domain)
expect(d.name).to_not be_nil expect(d.name).to_not be_nil
@ -57,5 +60,5 @@ describe Domain do
expect(Fabricate.build(:domain, period: 4).valid?).to be false expect(Fabricate.build(:domain, period: 4).valid?).to be false
expect(Fabricate.build(:domain, period: 3).valid?).to be true expect(Fabricate.build(:domain, period: 3).valid?).to be true
end end
end
end end