Skip to content

Commit

Permalink
New zone creation only loads valid zone templates [#24 state:resolved]
Browse files Browse the repository at this point in the history
Added new finder extensions to ZoneTemplate ([#24])
  • Loading branch information
kennethkalmer committed Jul 6, 2008
1 parent 5e8d910 commit 87ceaa9
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/controllers/zones_controller.rb
Expand Up @@ -17,7 +17,7 @@ def show

def new
@zone = Zone.new
@zone_templates = ZoneTemplate.find( :all )
@zone_templates = ZoneTemplate.find( :all, :require_soa => true )
end

def create
Expand Down
29 changes: 28 additions & 1 deletion app/models/zone_template.rb
Expand Up @@ -6,7 +6,28 @@ class ZoneTemplate < ActiveRecord::Base
validates_uniqueness_of :name
validates_presence_of :ttl

# Build a new zone using +self+ as a template. +zone+ should be valid zone
class << self

# Custom find that takes one additional parameter, :require_soa (bool), for
# restricting the returned resultset to only instances that #has_soa?
def find_with_validations( *args )
options = args.extract_options!
valid_soa = options.delete( :require_soa ) || false

# find as per usual
records = find_without_validations( *args << options )

if valid_soa
records.delete_if { |z| !z.has_soa? }
end

records # give back
end
alias_method_chain :find, :validations

end

# Build a new zone using +self+ as a template. +zone+ should be valid zone
# name. This method will throw exceptions as it encounters errors, and will
# use a transaction to complete the operation
def build( zone_name )
Expand Down Expand Up @@ -39,4 +60,10 @@ def build( zone_name )

zone
end

# If the template has an SOA record, it can be used for building zones
def has_soa?
record_templates.count( :conditions => "record_type LIKE 'SOA'" ) == 1
end

end
1 change: 1 addition & 0 deletions spec/controllers/zones_controller_spec.rb
Expand Up @@ -37,6 +37,7 @@
response.should render_template('zones/new')
assigns[:zone].should be_a_kind_of( Zone )
assigns[:zone_templates].should_not be_empty
assigns[:zone_templates].size.should be(2)
end

it "should not save a partial form" do
Expand Down
25 changes: 19 additions & 6 deletions spec/fixtures/record_templates.yml
Expand Up @@ -17,40 +17,53 @@ east_coast_ns_ns1:
record_type: NS
host: @
data: ns1.%ZONE%.
example_com_ns_ns2:
east_coast_ns_ns2:
zone_template: east_coast_dc
ttl: 86400
record_type: NS
host: @
data: ns2.%ZONE%.
example_com_a_ns1:
east_coast_a_ns1:
zone_template: east_coast_dc
ttl: 86400
record_type: A
host: ns1
data: 10.0.0.1
example_com_a_ns2:
east_coast_a_ns2:
zone_template: east_coast_dc
ttl: 86400
record_type: A
host: ns2
data: 10.0.0.2
example_com_a:
east_coast_a:
zone_template: east_coast_dc
ttl: 86400
record_type: A
host: @
data: 10.0.0.3
example_com_mx:
east_coast_mx:
zone_template: east_coast_dc
ttl: 86400
record_type: MX
host: @
priority: 10
data: mail
example_com_a_mail:
east_coast_a_mail:
zone_template: east_coast_dc
ttl: 86400
record_type: A
host: mail
data: 10.0.0.4

# This template only has an SOA record
west_coast_soa:
zone_template: west_coast_dc
ttl: 86400
record_type: SOA
host: @
primary_ns: ns1.%ZONE%.
contact: ewest-coast.example.com
refresh: 10800
retry: 7200
expire: 604800
minimum: 10800
5 changes: 5 additions & 0 deletions spec/fixtures/zone_templates.yml
Expand Up @@ -7,3 +7,8 @@ east_coast_dc:
west_coast_dc:
name: West Coast Data Center
ttl: 86400

# Has no SOA records, so should never be used in zone creation
partially_complete:
name: Partially Complete
ttl: 43200
18 changes: 18 additions & 0 deletions spec/models/zone_template_spec.rb
Expand Up @@ -42,6 +42,12 @@
zone.should be_a_kind_of( Zone )
zone.should be_valid
end

it "should have a sense of validity" do
@zone_template.has_soa?.should be_true

zone_templates( :partially_complete ).has_soa?.should_not be_true
end
end

describe ZoneTemplate, "when used to build a zone" do
Expand Down Expand Up @@ -75,4 +81,16 @@
ns.each { |r| r.should be_a_kind_of( NS ) }
end

end

describe ZoneTemplate, "and finders" do
fixtures :all

it "should be able to return all templates" do
ZoneTemplate.find(:all).size.should be( ZoneTemplate.count )
end

it "should respect required validations" do
ZoneTemplate.find(:all, :require_soa => true).size.should be( 2 )
end
end

0 comments on commit 87ceaa9

Please sign in to comment.