Class: CmsSnippet

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/cms_snippet.rb

Direct Known Subclasses

CmsTag::Snippet

Class Method Summary collapse

Class Method Details

.content_for(slug) ⇒ Object

– Class Methods ——————————————————–



21
22
23
# File 'app/models/cms_snippet.rb', line 21

def self.content_for(slug)
  (s = find_by_slug(slug)) ? s.content : ''
end

.initialize_or_find(cms_page, slug) ⇒ Object



25
26
27
# File 'app/models/cms_snippet.rb', line 25

def self.initialize_or_find(cms_page, slug)
  load_for_slug(cms_page.cms_site, slug) || new(:slug => slug)
end

.load_for_slug(site, slug) ⇒ Object

Non-blowing-up version of the method above



54
55
56
57
58
# File 'app/models/cms_snippet.rb', line 54

def self.load_for_slug(site, slug)
  load_for_slug!(site, slug) 
rescue ActiveRecord::RecordNotFound
  nil
end

.load_for_slug!(site, slug) ⇒ Object

Wrapper around load_from_file and find_by_slug returns layout object if loaded / found



42
43
44
45
46
47
48
49
50
51
# File 'app/models/cms_snippet.rb', line 42

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

.load_from_file(site, name) ⇒ Object

Attempting to initialize snippet object from yaml file that is found in config.seed_data_path



30
31
32
33
34
35
36
37
38
# File 'app/models/cms_snippet.rb', line 30

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)
rescue
  raise "Failed to load from #{file_path}"
end