mirror of
https://github.com/internetee/registry.git
synced 2025-08-01 23:42:04 +02:00
Handle children errors
This commit is contained in:
parent
3dca771bcb
commit
9ce6efc4db
8 changed files with 86 additions and 9 deletions
|
@ -32,12 +32,15 @@ module Epp::Common
|
|||
@current_epp_user ||= EppUser.find(epp_session[:epp_user_id]) if epp_session[:epp_user_id]
|
||||
end
|
||||
|
||||
def handle_errors(error_code_map, obj)
|
||||
def handle_epp_errors(error_code_map, obj)
|
||||
obj.errors.each do |key, err|
|
||||
error_code_map.each do |code, values|
|
||||
|
||||
has_error = Proc.new do |x|
|
||||
x.is_a?(Array) ? obj.errors.added?(key, x[0], x[1]) : obj.errors.added?(key, x)
|
||||
if x.is_a?(Array)
|
||||
obj.errors.generate_message(key, x[0], x[1]) == err
|
||||
else
|
||||
obj.errors.generate_message(key, x) == err
|
||||
end
|
||||
end
|
||||
|
||||
if err.is_a?(Hash)
|
||||
|
|
|
@ -57,10 +57,11 @@ module Epp::DomainsHelper
|
|||
end
|
||||
|
||||
def handle_errors
|
||||
super({
|
||||
handle_epp_errors({
|
||||
'2302' => [:epp_domain_taken, :epp_domain_reserved],
|
||||
'2306' => [:blank, [:out_of_range, {min: 1, max: 13}]],
|
||||
'2303' => [:not_found]
|
||||
'2303' => [:not_found],
|
||||
'2005' => [:hostname_invalid]
|
||||
}, @domain
|
||||
)
|
||||
end
|
||||
|
|
|
@ -20,6 +20,7 @@ class Domain < ActiveRecord::Base
|
|||
validates :name_dirty, domain_name: true, uniqueness: { message: :epp_domain_taken }
|
||||
validates :period, numericality: { only_integer: true, greater_than: 0, less_than: 100 }
|
||||
validates :name, :owner_contact, presence: true
|
||||
validates_associated :nameservers
|
||||
|
||||
def name=(value)
|
||||
value.strip!
|
||||
|
@ -61,17 +62,38 @@ class Domain < ActiveRecord::Base
|
|||
)
|
||||
end
|
||||
|
||||
def attach_nameservers(nameservers)
|
||||
nameservers.each do |x|
|
||||
self.nameservers << Nameserver.find_or_create_by(hostname: x)
|
||||
def attach_nameservers(ns_list)
|
||||
ns_list.each do |x|
|
||||
self.nameservers.build(hostname: x)
|
||||
end
|
||||
save!
|
||||
|
||||
save
|
||||
|
||||
add_child_collection_errors(:nameservers, :ns)
|
||||
|
||||
validate_nameservers_count
|
||||
|
||||
errors.empty?
|
||||
end
|
||||
|
||||
def add_child_collection_errors(attr_key, epp_obj_name)
|
||||
send(attr_key).each do |obj|
|
||||
obj.errors.keys.each do |key|
|
||||
add_errors_with_value(attr_key, epp_obj_name, obj, key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def add_errors_with_value(attr_key, epp_obj_name, obj, key)
|
||||
obj.errors[key].each do |x|
|
||||
errors.add(attr_key, {
|
||||
obj: epp_obj_name,
|
||||
val: obj.send(key),
|
||||
msg: x
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
def validate_nameservers_count
|
||||
errors.add(:nameservers, :blank) if nameservers.empty?
|
||||
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
class Nameserver < ActiveRecord::Base
|
||||
belongs_to :registrar
|
||||
has_and_belongs_to_many :domains
|
||||
|
||||
validates :hostname, hostname: true
|
||||
end
|
||||
|
|
13
app/validators/hostname_validator.rb
Normal file
13
app/validators/hostname_validator.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
class HostnameValidator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, value)
|
||||
if !self.class.validate_format(value)
|
||||
record.errors.add(attribute, (options[:message] || :invalid))
|
||||
end
|
||||
end
|
||||
|
||||
class << self
|
||||
def validate_format(value)
|
||||
!!(value =~ /\A(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\z/)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue