Sha256: 9b7b70fc5279b8199604a3fdf09a79d7b65a97e224fa334e2081ccec8a59bf9a
Contents?: true
Size: 1.94 KB
Versions: 16
Compression:
Stored size: 1.94 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) load_for_slug(cms_page.cms_site, 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 # Wrapper around load_from_file and find_by_slug # returns layout object if loaded / found def self.load_for_slug!(site, slug) if ComfortableMexicanSofa.configuration.seed_data_path load_from_file(site, slug) else # FIX: This a bit odd... Snippet is used as a tag, so sometimes there's no site scope # being passed. So we're enforcing this only if it's found. Need to review. conditions = site ? {:conditions => {:cms_site_id => site.id}} : {} find_by_slug(slug, conditions) end || raise(ActiveRecord::RecordNotFound, "CmsSnippet with slug: #{slug} cannot be found") end # Non-blowing-up version of the method above def self.load_for_slug(site, slug) load_for_slug!(site, slug) rescue ActiveRecord::RecordNotFound nil end end
Version data entries
16 entries across 16 versions & 1 rubygems