Sha256: 78816fc70cc51335575ea5d08fc0a69e81d327c402a86d75f1dc8c91716f7ca0

Contents?: true

Size: 815 Bytes

Versions: 6

Compression:

Stored size: 815 Bytes

Contents

module Cms
  class Site < ActiveRecord::Base
    validates_uniqueness_of :domain

    before_validation :remove_www

    before_save :unset_default
    after_save :set_default

    scope :default, :conditions => {:the_default => true}

    def self.find_by_domain(domain)
      d = domain.clone
      strip_www!(d)
      if site = first(:conditions => {:domain => d})
        site
      else
        default.first
      end
    end

    def self.strip_www!(d)
      return unless d
      d.sub!(/\Awww./, '')
    end

    def remove_www
      self.class.strip_www!(domain)
    end

    def unset_default
      self.class.update_all(["the_default = ?", false]) if the_default
    end

    def set_default
      if self.class.default.count < 1
        update_attribute(:the_default, true)
      end
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
browsercms-3.4.2 app/models/cms/site.rb
browsercms-3.4.2.rc1 app/models/cms/site.rb
browsercms-3.4.1 app/models/cms/site.rb
browsercms-3.4.0 app/models/cms/site.rb
browsercms-3.4.0.rc2 app/models/cms/site.rb
browsercms-3.4.0.rc1 app/models/cms/site.rb