Sha256: b02ef9b00a63643b71cfe6d27c3cd41acf77abfacd799506a5819f85a9f7c025

Contents?: true

Size: 1.6 KB

Versions: 27

Compression:

Stored size: 1.6 KB

Contents

class Cms::Site < ActiveRecord::Base
  
  ComfortableMexicanSofa.establish_connection(self)
  
  set_table_name :cms_sites
  
  # -- Relationships --------------------------------------------------------
  has_many :layouts,  :dependent => :destroy
  has_many :pages,    :dependent => :destroy
  has_many :snippets, :dependent => :destroy
  has_many :files,    :dependent => :destroy
  
  # -- Callbacks ------------------------------------------------------------
  before_validation :assign_label
  before_save :clean_path
  
  # -- Validations ----------------------------------------------------------
  validates :label,
    :presence   => true
  validates :hostname,
    :presence   => true,
    :uniqueness => { :scope => :path },
    :format     => { :with => /^[\w\.\-]+$/ }
    
  # -- Scopes ---------------------------------------------------------------
  scope :mirrored, where(:is_mirrored => true)
  
  # -- Class Methods --------------------------------------------------------
  # returning the Cms::Site instance based on host and path
  def self.find_site(host, path = nil)
    return Cms::Site.first if Cms::Site.count == 1
    cms_site = nil
    Cms::Site.find_all_by_hostname(host).each do |site|
      if site.path.blank?
        cms_site = site
      elsif "#{path}/".match /^\/#{Regexp.escape(site.path.to_s)}\//
        cms_site = site
        break
      end
    end
    return cms_site
  end
  
protected
  
  def assign_label
    self.label = self.label.blank?? self.hostname : self.label
  end
  
  def clean_path
    self.path ||= ''
    self.path.squeeze!('/')
    self.path.gsub!(/\/$/, '')
  end
  
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
comfortable_mexican_sofa-1.5.11 app/models/cms/site.rb
comfortable_mexican_sofa-1.5.10 app/models/cms/site.rb
comfortable_mexican_sofa-1.5.9 app/models/cms/site.rb
comfortable_mexican_sofa-1.5.8 app/models/cms/site.rb
comfortable_mexican_sofa-1.5.7 app/models/cms/site.rb
comfortable_mexican_sofa-1.5.6 app/models/cms/site.rb
comfortable_mexican_sofa-1.5.5 app/models/cms/site.rb
comfortable_mexican_sofa-1.5.4 app/models/cms/site.rb
comfortable_mexican_sofa-1.5.3 app/models/cms/site.rb
comfortable_mexican_sofa-1.5.2 app/models/cms/site.rb
comfortable_mexican_sofa-1.5.1 app/models/cms/site.rb
comfortable_mexican_sofa-1.5.0 app/models/cms/site.rb
comfortable_mexican_sofa-1.4.22 app/models/cms/site.rb
comfortable_mexican_sofa-1.4.21 app/models/cms/site.rb
comfortable_mexican_sofa-1.4.20 app/models/cms/site.rb
comfortable_mexican_sofa-1.4.19 app/models/cms/site.rb
comfortable_mexican_sofa-1.4.18 app/models/cms/site.rb
comfortable_mexican_sofa-1.4.17 app/models/cms/site.rb
comfortable_mexican_sofa-1.4.16 app/models/cms/site.rb
comfortable_mexican_sofa-1.4.15 app/models/cms/site.rb