Class: CmsLayout

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.app_layouts_for_selectObject

List of available application layouts



42
43
44
45
46
47
48
# File 'app/models/cms_layout.rb', line 42

def self.app_layouts_for_select
  Dir.glob(File.expand_path('app/views/layouts/*.html.*', Rails.root)).collect do |filename|
    match = filename.match(/\w*.html.\w*$/)
    app_layout = match && match[0]
    app_layout.to_s[0...1] == '_' ? nil : app_layout
  end.compact
end

.load_for_slug(site, slug) ⇒ Object

Non-blowing-up version of the method above



74
75
76
77
78
# File 'app/models/cms_layout.rb', line 74

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



65
66
67
68
69
70
71
# File 'app/models/cms_layout.rb', line 65

def self.load_for_slug!(site, slug)
  if LucyCms.configuration.seed_data_path
    load_from_file(site, slug)
  else
    site.cms_layouts.find_by_slug(slug)
  end || raise(ActiveRecord::RecordNotFound, "CmsLayout with slug: #{slug} cannot be found")
end

.load_from_file(site, slug) ⇒ Object

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



51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/cms_layout.rb', line 51

def self.load_from_file(site, slug)
  return nil if LucyCms.config.seed_data_path.blank?
  file_path = "#{LucyCms.config.seed_data_path}/#{site.hostname}/layouts/#{slug}.yml"
  return nil unless File.exists?(file_path)
  attributes            = YAML.load_file(file_path).symbolize_keys!
  attributes[:parent]   = CmsLayout.load_from_file(site, attributes[:parent])
  attributes[:cms_site] = site
  new(attributes)
rescue
  raise "Failed to load from #{file_path}"  
end

.options_for_select(cms_site, cms_layout = nil, current_layout = nil, depth = 0, spacer = '. . ') ⇒ Object

– Class Methods ——————————————————– Tree-like structure for layouts



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

def self.options_for_select(cms_site, cms_layout = nil, current_layout = nil, depth = 0, spacer = '. . ')
  out = []
  [current_layout || cms_site.cms_layouts.roots].flatten.each do |layout|
    next if cms_layout == layout
    out << [ "#{spacer*depth}#{layout.label}", layout.id ]
    layout.children.each do |child|
      out += options_for_select(cms_site, cms_layout, child, depth + 1, spacer)
    end
  end
  return out.compact
end

Instance Method Details

#merged_contentObject

– Instance Methods —————————————————– magical merging tag is cms:page:content If parent layout has this tag defined its content will be merged. If no such tag found, parent content is ignored.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/cms_layout.rb', line 84

def merged_content
  if parent
    regex = /\{\{\s*cms:page:content:?(?:(?::text)|(?::rich_text))?\s*\}\}/
    if parent.merged_content.match(regex)
      parent.merged_content.gsub(regex, content)
    else
      content
    end
  else
    content
  end
end