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 ###
def validate_nameservers_count
unless nameservers.length.between?(1, 13)
errors.add(:nameservers, :out_of_range, {min: 1, max: 13})
sg = SettingGroup.find_by(code: SettingGroup::DOMAIN_VALIDATION_CODE)
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

View file

@ -2,4 +2,11 @@ class SettingGroup < ActiveRecord::Base
has_many :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