<commit_before># Attributes:
# - id: String, the domain ID
# - zone: String, the zone name (ex. example.org). Must be unique
# - ttl: Integer, the default TTL for the zone (optional)
# Relations:
# - belongs_to User
# - has_many Record

class Domain
  include Mongoid::Document
  include Mongoid::Timestamps
  require "bunny"

  field :zone, type: String

  belongs_to :user
  has_many :records,     :dependent => :destroy

  attr_accessor :ttl, :st

  validates :zone,  :uniqueness => true

  before_validation :downcase_zone
  after_create :create_default_records
  before_destroy :delete_zone



  def downcase_zone
    self.zone.downcase unless self.zone.nil?
  end

  # Create default SOA and NS records
  def create_default_records
    soa_record = self.records.create(name: '', type: 'SOA')
    if self.ttl.nil? or self.ttl.blank?
      soa_record.answers.build(:mname => Settings.ns01, :rname => Settings.email, :at => 60)
    else
      soa_record.answers.build(:mname => Settings.ns01, :rname => Settings.email, :at => self.ttl).save
    end
    soa_record.save
    soa_record.answers.first.update_serial<commit_msg>Aggiunto controllo sintattico sul nome zona e sulla proprietà<commit_after># Attributes:
# - id: String, the domain ID
# - zone: String, the zone name (ex. example.org). Must be unique
# - ttl: Integer, the default TTL for the zone (optional)
# Relations:
# - belongs_to User
# - has_many Record

class Domain
  include Mongoid::Document
  include Mongoid::Timestamps
  require "bunny"

  field :zone, type: String

  belongs_to :user
  has_many :records,     :dependent => :destroy

  attr_accessor :ttl, :st

  validates :zone,  :uniqueness => true,
                    :length => { maximum: 63 },
                    format: { :with => /\A([\-a-zA-Z0-9]+\.[a-z]{1,3})\z/ }

  before_validation :downcase_zone
  after_create :create_default_records
  before_destroy :delete_zone



  def downcase_zone
    self.zone.downcase unless self.zone.nil?
  end

  def can_i_create_this_zone?
    validates_property(self.zone).nil? || validates_property(self.zone)
  end

  def validates_property(zone)
    puts "Recived this zone: #{zone}"

    zone_splitted = zone.split('.')
    unless zone_splitted.empty?
      zone_splitted.shift
      new_zone = zone_splitted.join('.')
      puts "Now validating: #{new_zone}"
      if Domain.where(zone: new_zone).count == 1
        puts 'this zone exists'
        puts "the propietary is #{Domain.where(zone: new_zone).first.user}"
        puts "I'm #{self.user}"
        return Domain.where(zone: new_zone).first.user == self.user
      else
        puts 'Domain not found: trying another round'
        validates_property(new_zone)
      end
    end
  end

  # Create default SOA and NS records
  def create_default_records
    soa_record = self.records.create(name: '', type: 'SOA')
    if self.ttl.nil? or self.ttl.blank?
      soa_record.answers.build(:mname => Settings.ns01, :rname => Settings.email, :at => 60)
    else
      soa_record.answers.build(:mname => Settings.ns01, :rname => Settings.email, :at => self.ttl).save
    end
    soa_record.save
    soa_record.answers.first.update_serial
{'fraction_non_alphanumeric': 0.1140960809102402, 'fraction_numerical': 0.0060050568900126425, 'mean_word_length': 3.254032258064516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '877327', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1051, 'n_words': 269}