Sha256: bf7f93c242995b6db84c8ba8a67202206a77c7a237df8bcb83f6712057190cfc

Contents?: true

Size: 1.28 KB

Versions: 11

Compression:

Stored size: 1.28 KB

Contents

class CmsSnippet < ActiveRecord::Base
  
  # -- Relationships --------------------------------------------------------
  belongs_to :cms_site
  
  # -- Validations ----------------------------------------------------------
  validates :cms_site_id,
    :presence   => true
  validates :label,
    :presence   => true
  validates :slug,
    :presence   => true,
    :uniqueness => { :scope => :cms_site_id },
    :format     => { :with => /^\w[a-z0-9_-]*$/i }
    
  # -- Class Methods --------------------------------------------------------
  def self.content_for(slug)
    (s = find_by_slug(slug)) ? s.content : ''
  end
  
  def self.initialize_or_find(cms_page, slug)
    if ComfortableMexicanSofa.configuration.seed_data_path
      s = CmsTag::Snippet.load_from_file(cms_page.cms_site, slug)
    end
    s || find_by_slug(slug) || new(:slug => slug)
  end
  
  # Attempting to initialize snippet object from yaml file that is found in config.seed_data_path
  def self.load_from_file(site, name)
    return nil if ComfortableMexicanSofa.config.seed_data_path.blank?
    file_path = "#{ComfortableMexicanSofa.config.seed_data_path}/#{site.hostname}/snippets/#{name}.yml"
    return nil unless File.exists?(file_path)
    attributes = YAML.load_file(file_path).symbolize_keys!
    new(attributes)
  end
  
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
comfortable_mexican_sofa-1.0.14 app/models/cms_snippet.rb
comfortable_mexican_sofa-1.0.13 app/models/cms_snippet.rb
comfortable_mexican_sofa-1.0.12 app/models/cms_snippet.rb
comfortable_mexican_sofa-1.0.11 app/models/cms_snippet.rb
comfortable_mexican_sofa-1.0.10 app/models/cms_snippet.rb
comfortable_mexican_sofa-1.0.9 app/models/cms_snippet.rb
comfortable_mexican_sofa-1.0.7 app/models/cms_snippet.rb
comfortable_mexican_sofa-1.0.6 app/models/cms_snippet.rb
comfortable_mexican_sofa-1.0.5 app/models/cms_snippet.rb
comfortable_mexican_sofa-1.0.4 app/models/cms_snippet.rb
comfortable_mexican_sofa-1.0.3 app/models/cms_snippet.rb