From 7f26c8c769ced6dcd23af73ce3f6fc459ebe27ff Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Tue, 9 Sep 2014 17:26:08 +0300 Subject: [PATCH] Let domain to to draft status --- app/helpers/epp/domains_helper.rb | 18 +++++++++++++++--- app/models/domain.rb | 14 +++++++++----- app/validators/domain_name_validator.rb | 4 ++-- app/views/admin/domains/new.haml | 5 +++-- config/locales/en.yml | 4 +++- spec/epp/domain_spec.rb | 1 - 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/app/helpers/epp/domains_helper.rb b/app/helpers/epp/domains_helper.rb index e5b89c5b6..2ebf4ac12 100644 --- a/app/helpers/epp/domains_helper.rb +++ b/app/helpers/epp/domains_helper.rb @@ -3,8 +3,20 @@ module Epp::DomainsHelper Domain.transaction do @domain = Domain.new(domain_create_params) - handle_errors(@domain) and return unless @domain.parse_and_attach_domain_dependencies(@ph, parsed_frame) - handle_errors(@domain) and return unless @domain.save + @domain.attach_owner_contact(@ph[:registrant]) if @ph[:registrant] + @domain.save + @domain.parse_and_attach_domain_dependencies(parsed_frame) + @domain.all_dependencies_valid? + + if @domain.errors.any? + handle_errors(@domain) + raise ActiveRecord::Rollback and return + end + + unless @domain.save + handle_errors(@domain) + raise ActiveRecord::Rollback and return + end render '/epp/domains/create' end @@ -40,7 +52,7 @@ module Epp::DomainsHelper handle_errors(@domain) and return unless @domain - @domain.parse_and_attach_domain_dependencies(@ph, parsed_frame.css('add')) + @domain.parse_and_attach_domain_dependencies(parsed_frame.css('add')) @domain.parse_and_detach_domain_dependencies(parsed_frame.css('rem')) @domain.parse_and_update_domain_dependencies(parsed_frame.css('chg')) diff --git a/app/models/domain.rb b/app/models/domain.rb index 1a23dbee2..847da4ebc 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -39,11 +39,9 @@ class Domain < ActiveRecord::Base validates :name_dirty, domain_name: true, uniqueness: true validates :period, numericality: { only_integer: true } - validates :name, :owner_contact, presence: true + validates :owner_contact, presence: true validate :validate_period - validate :validate_nameservers_count - validate :validate_admin_contacts_count def name=(value) value.strip! @@ -54,8 +52,7 @@ class Domain < ActiveRecord::Base ### CREATE & UPDATE ### - def parse_and_attach_domain_dependencies(ph, parsed_frame) - attach_owner_contact(ph[:registrant]) if ph[:registrant] + def parse_and_attach_domain_dependencies(parsed_frame) attach_contacts(self.class.parse_contacts_from_frame(parsed_frame)) attach_nameservers(self.class.parse_nameservers_from_frame(parsed_frame)) attach_statuses(self.class.parse_statuses_from_frame(parsed_frame)) @@ -300,6 +297,13 @@ class Domain < ActiveRecord::Base add_epp_error('2306', 'curExpDate', cur_exp_date, I18n.t('errors.messages.epp_exp_dates_do_not_match')) end + def all_dependencies_valid? + validate_nameservers_count + validate_admin_contacts_count + + errors.empty? + end + def epp_code_map # rubocop:disable Metrics/MethodLength domain_validation_sg = SettingGroup.domain_validation diff --git a/app/validators/domain_name_validator.rb b/app/validators/domain_name_validator.rb index 167c46d82..78a1a1099 100644 --- a/app/validators/domain_name_validator.rb +++ b/app/validators/domain_name_validator.rb @@ -8,9 +8,9 @@ class DomainNameValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) if !self.class.validate_format(value) - record.errors[attribute] << (options[:message] || 'invalid format') + record.errors[attribute] << (options[:message] || record.errors.generate_message(attribute, :invalid)) elsif !self.class.validate_reservation(value) - record.errors.add(attribute, (options[:message] || :reserved)) + record.errors.add(attribute, (options[:message] || record.errors.generate_message(attribute, :reserved))) end end diff --git a/app/views/admin/domains/new.haml b/app/views/admin/domains/new.haml index 87e95cab1..3221a18dc 100644 --- a/app/views/admin/domains/new.haml +++ b/app/views/admin/domains/new.haml @@ -1,9 +1,10 @@ %h2= t('shared.new_domain') %hr = form_for([:admin, @domain]) do |f| + = @domain.errors.inspect - if @domain.errors.any? - - @domain.errors.full_messages.each do |x| - = x + - @domain.errors.each do |attr, err| + = err %br diff --git a/config/locales/en.yml b/config/locales/en.yml index e9bd76836..d1700266e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -70,6 +70,7 @@ en: domain: attributes: name_dirty: + invalid: 'Domain name is invalid' reserved: 'Domain name is reserved or restricted' taken: 'Domain name already exists' owner_contact: @@ -80,11 +81,12 @@ en: admin_contacts: out_of_range: 'Admin contacts count must be between 1 - infinity' nameservers: - out_of_range: 'count must be between %{min}-%{max}' + out_of_range: 'Nameservers count must be between %{min}-%{max}' not_found: 'Nameserver was not found' taken: 'Nameserver already exists on this domain' period: out_of_range: 'Period must add up to 1, 2 or 3 years' + not_a_number: 'Period is not a number' auth_info: wrong_pw: 'Authentication error' domain_statuses: diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index 2210dbdb6..073e7f3d2 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -172,7 +172,6 @@ describe 'EPP Domain', epp: true do it 'creates a domain' do response = epp_request(domain_create_xml, :xml) - d = Domain.first expect(response[:result_code]).to eq('1000')