You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.6 KiB

13 years ago
class Domain < ActiveRecord::Base
13 years ago
set_inheritance_column "sti_disabled"
nilify_blanks
# optional IP for create form, results in a type A record
attr_accessor :ip
13 years ago
belongs_to :user
13 years ago
has_many :records, :dependent => :destroy, :inverse_of => :domain
13 years ago
cattr_reader :types
@@types = ['NATIVE', 'MASTER', 'SLAVE', 'SUPERSLAVE']
13 years ago
has_one :soa_record,
:class_name => 'SOA',
:conditions => {:type => 'SOA'},
:inverse_of => :domain
13 years ago
for type in Record.types
13 years ago
has_many :"#{type.downcase}_records",
:class_name => type,
:conditions => {:type => type},
:inverse_of => :domain
13 years ago
validates_associated :"#{type.downcase}_records"
end
validates :name, :presence => true, :uniqueness => true, :domainname => {:require_valid_tld => false}
validates :master, :presence => true, :if => :slave?
13 years ago
validates :master, :ip => true, :allow_nil => true, :if => :slave?
13 years ago
validates :type, :inclusion => { :in => @@types, :message => "Unknown domain type" }
validates :soa_record, :presence => {:unless => :slave?}
validates_associated :soa_record, :allow_nil => true
13 years ago
validates :ns_records, :presence => true, :length => {
:minimum => 2, :maximum => 10,
:message => "must have be at least 2, at most 10"}
13 years ago
validates_associated :records
13 years ago
def slave?; self.type == 'SLAVE' end
13 years ago
before_create do
a_records.build(:content => ip) if ip.present?
end
13 years ago
def setup(email)
13 years ago
build_soa_record
13 years ago
soa = soa_record
soa.contact ||= email
13 years ago
ns_records.build
13 years ago
ns_records.build
13 years ago
ns1, ns2 = ns_records
13 years ago
ns1.content = Settings.ns.first
ns2.content = (Settings.ns - [ns1.content]).sample
13 years ago
end
13 years ago
end