diff --git a/app/models/domain.rb b/app/models/domain.rb new file mode 100644 index 0000000..ca828fb --- /dev/null +++ b/app/models/domain.rb @@ -0,0 +1,28 @@ +class Domain < ActiveRecord::Base + self.inheritance_column = :nx + + def self.domain_types + [ + 'NATIVE', + 'MASTER', + 'SLAVE', + ] + end + + has_many :records + has_one :soa, class_name: SOA + + validates :name, uniqueness: true, presence: true + validates :type, presence: true, inclusion: { in: domain_types } + + after_create :generate_soa + + private + + # Hooks + def generate_soa + soa_record = SOA.new(domain: self) + soa_record.save + end + +end diff --git a/app/models/record.rb b/app/models/record.rb new file mode 100644 index 0000000..faf9fdd --- /dev/null +++ b/app/models/record.rb @@ -0,0 +1,54 @@ +class Record < ActiveRecord::Base + belongs_to :domain + + def self.record_types + [ + 'SOA', 'NS', 'CNAME', + 'A', 'AAAA', + 'MX', + 'TXT', 'SPF', 'SRV', 'SSHFP', + 'PTR', + ] + end + + validates :name, presence: true + validates :type, inclusion: { in: record_types } + + before_validation :set_name + after_save :update_zone_serial + after_destroy :update_zone_serial + + def short + return '' if name == domain.name + return '' if name.blank? + + File.basename(name, ".#{domain.name}") + end + + # Create record specific urls for all record types + # + # Overrides default rails STI + def self.model_name + return super if self == Record + + Record.model_name + end + + private + + # Hooks + + # Powerdns expects full domain names + def set_name + self.name = domain.name if name.blank? + self.name = "#{name}.#{domain.name}" if not name.end_with?(domain.name) + end + + def update_zone_serial + # SOA records handle serial themselves + return true if type == 'SOA' + + domain.soa.bump_serial! + end + +end