mirror of
https://github.com/internetee/registry.git
synced 2025-07-23 03:06:14 +02:00
Refactor epp errors to concern
This commit is contained in:
parent
b944516f17
commit
6b86af3048
4 changed files with 15 additions and 65 deletions
|
@ -1,7 +1,6 @@
|
|||
module EppErrors
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
|
||||
EPP_CODE_MAP = {
|
||||
'2302' => ['Domain name already exists', 'Domain name is reserved or restricted'],
|
||||
'2306' => ['Registrant is missing', 'Nameservers count must be between 1-13', 'Admin contact is missing'],
|
||||
|
@ -18,10 +17,10 @@ module EppErrors
|
|||
epp_errors = []
|
||||
errors.messages.each do |key, values|
|
||||
if self.class.reflect_on_association(key)
|
||||
epp_errors = collect_child_errors(key, values)
|
||||
else
|
||||
epp_errors = collect_parent_errors(key, values)
|
||||
epp_errors << collect_child_errors(key)
|
||||
end
|
||||
|
||||
epp_errors << collect_parent_errors(key, values)
|
||||
end
|
||||
|
||||
errors[:epp_errors] = epp_errors
|
||||
|
@ -34,15 +33,19 @@ module EppErrors
|
|||
|
||||
values.each do |err|
|
||||
if err.is_a?(Hash)
|
||||
epp_errors << {code: find_epp_code(err[:msg]), msg: err[:msg], value: {val: err[:val], obj: err[:obj]}}
|
||||
next unless code = find_epp_code(err[:msg])
|
||||
epp_errors << {code: code, msg: err[:msg], value: {val: err[:val], obj: err[:obj]}}
|
||||
else
|
||||
epp_errors << {code: find_epp_code(err), msg: err, value: {val: send(key), obj: EPP_OBJ_MAP[key]}}
|
||||
next unless code = find_epp_code(err)
|
||||
err = {code: code, msg: err}
|
||||
err[:value] = {val: send(key), obj: EPP_OBJ_MAP[key]} unless self.class.reflect_on_association(key)
|
||||
epp_errors << err
|
||||
end
|
||||
end
|
||||
epp_errors
|
||||
end
|
||||
|
||||
def collect_child_errors(key, values)
|
||||
def collect_child_errors(key)
|
||||
macro = self.class.reflect_on_association(key).macro
|
||||
multi = [:has_and_belongs_to_many, :has_many]
|
||||
single = [:belongs_to, :has_one]
|
||||
|
|
|
@ -2,8 +2,6 @@ class Domain < ActiveRecord::Base
|
|||
#TODO whois requests ip whitelist for full info for own domains and partial info for other domains
|
||||
#TODO most inputs should be trimmed before validatation, probably some global logic?
|
||||
|
||||
|
||||
|
||||
include EppErrors
|
||||
|
||||
belongs_to :registrar
|
||||
|
@ -77,8 +75,6 @@ class Domain < ActiveRecord::Base
|
|||
|
||||
save
|
||||
|
||||
# add_child_collection_errors(:nameservers, :ns)
|
||||
|
||||
validate_nameservers_count
|
||||
|
||||
errors.empty?
|
||||
|
@ -102,24 +98,6 @@ class Domain < ActiveRecord::Base
|
|||
self.nameservers.build(attrs)
|
||||
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
|
||||
unless nameservers.count.between?(1, 13)
|
||||
errors.add(:nameservers, :out_of_range, {min: 1, max: 13})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue