Skip to content
This repository has been archived by the owner on Mar 5, 2020. It is now read-only.

Commit

Permalink
[#48] Implemented master/slave domain support at model level
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethkalmer committed Feb 15, 2009
1 parent 576ecca commit 68ae720
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
10 changes: 9 additions & 1 deletion app/models/domain.rb
Expand Up @@ -30,6 +30,9 @@ class Domain < ActiveRecord::Base

validates_presence_of :name
validates_uniqueness_of :name
validates_inclusion_of :type, :in => %w( NATIVE MASTER SLAVE )
validates_presence_of :master, :if => Proc.new { |d| d.slave? }
validates_format_of :master, :if => Proc.new { |d| d.slave? }, :with => /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/

# Disable single table inheritence (STI)
set_inheritance_column 'not_used_here'
Expand All @@ -39,7 +42,7 @@ class Domain < ActiveRecord::Base
SOA_FIELDS = [ :primary_ns, :contact, :refresh, :retry, :expire, :minimum ]
SOA_FIELDS.each do |f|
attr_accessor f
validates_presence_of f, :on => :create
validates_presence_of f, :on => :create, :if => Proc.new { |d| !d.slave? }
end

# Serial is optional, but will be passed to the SOA too
Expand All @@ -48,6 +51,11 @@ class Domain < ActiveRecord::Base
# Helper attributes for API clients and forms (keep it RESTful)
attr_accessor :zone_template_id, :zone_template_name

# Are we a slave domain
def slave?
self.type == 'SLAVE'
end

# return the records, excluding the SOA record
def records_without_soa
records.find(:all, :include => :domain ).select { |r| !r.is_a?( SOA ) }
Expand Down
25 changes: 24 additions & 1 deletion spec/models/domain_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Domain, "when new" do
describe "New MASTER/NATIVE", Domain do
fixtures :all

before(:each) do
Expand All @@ -27,6 +27,29 @@
it "should be NATIVE by default" do
@domain.type.should eql('NATIVE')
end

it "should not require a MASTER" do
@domain.should have(:no).errors_on(:master)
end
end

describe "New SLAVE", Domain do
before(:each) do
@domain = Domain.new( :type => 'SLAVE' )
end

it "should require a master address" do
# Format and missing
@domain.should have(2).error_on(:master)
end

it "should require a valid master address" do
@domain.master = 'foo'
@domain.should have(1).error_on(:master)

@domain.master = '127.0.0.1'
@domain.should have(:no).errors_on(:master)
end
end

describe Domain, "when loaded" do
Expand Down

1 comment on commit 68ae720

@antage
Copy link

@antage antage commented on 68ae720 Feb 22, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s very useful feature. Thanks!

Please sign in to comment.