Sha256: 00e2c021bfbd7514697816c573c23c51be43e99bad1ca68e8005bd090edcfab1

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

module SiteFramework
  # This model represent a **Domain**. Each domain
  # belongs to a [Site] model and may or may not
  # belongs to another  **Domain**
  class Domain < (defined?(ActiveRecord) ? ActiveRecord::Base : Object)

    PATTERN = /\A[a-z0-9]*(\.?[a-z0-9]+)\.[a-z]{2,5}(:[0-9]{1,5})?(\/.)?$/ix

    if defined? Mongoid
      include Mongoid::Document
      include Mongoid::Timestamps

      field :name,   type: String
      field :parent, type: String,  default: nil
      field :alias,  type: Boolean, default: false

      embedded_in :site

      index({ name: 1 }, { unique: true, background: true })

      def parent
        # TODO: return a domain with alias == false
      end
    end

    if defined? ActiveRecord
      belongs_to :site

      # Self relation
      belongs_to :parent, class_name: self.to_s
      validates_associated :site
    end

    validates :name, presence: true, if: :valid_domain_name?


    validates_uniqueness_of :name

    before_save :normalize_name

    def normalize_name
      self.name = name.downcase
    end

    def valid_domain_name?
      if read_attribute :alias
        false unless name =~ PATTERN
        # TODO: Check the owner of domain
      else
        true
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
site_framework-4.3.3 app/models/site_framework/domain.rb
site_framework-4.3.2 app/models/site_framework/domain.rb
site_framework-4.3.1 app/models/site_framework/domain.rb
site_framework-4.3.0 app/models/site_framework/domain.rb