Browse Source

refactor domain parent ownership validation

pull/1/head
Nicolae Claudius 13 years ago
parent
commit
783d8b4233
  1. 20
      app/models/domain.rb
  2. 3
      spec/models/domain_spec.rb

20
app/models/domain.rb

@ -50,12 +50,20 @@ class Domain < ActiveRecord::Base
errors[:name] = "cannot be a TLD or a reserved domain" if Tld.include?(name) errors[:name] = "cannot be a TLD or a reserved domain" if Tld.include?(name)
# if parent domain is on our system, the user must own it # if parent domain is on our system, the user must own it
segments = name.split('.') if parent_domain.present? && parent_domain.user_id != user_id
if segments.size >= 2 errors[:name] = "issue, the parent domain `#{parent_domain.name}` is registered to another user"
parent = segments[1..-1].join('.') end
parent_domain = Domain.find_by_name(parent) end
if parent_domain.present? && parent_domain.user_id != user_id
errors[:name] = "issue, the parent domain `#{parent}` is registered to another user" def parent_domain
@parent_domain ||= {}
@parent_domain[name] ||= begin
segments = name.split('.')
if segments.size >= 2
domain_name = segments[1..-1].join('.')
Domain.find_by_name(domain_name)
else
nil
end end
end end
end end

3
spec/models/domain_spec.rb

@ -54,7 +54,8 @@ describe Domain do
domain.should be_valid domain.should be_valid
# stub a parent domain on another user account # stub a parent domain on another user account
Domain.stub_chain('find_by_name.user_id').and_return(domain.user_id + 1) mock_domain = mock(:user_id => domain.user_id + 1, :name => 'x')
Domain.stub_chain('find_by_name').and_return(mock_domain)
domain.should have(1).errors_on(:name) domain.should have(1).errors_on(:name)
end end

Loading…
Cancel
Save