Class: Comfy::Cms::Layout

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/comfy/cms/layout.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.app_layouts_for_select(view_paths) ⇒ Object

List of available application layouts



45
46
47
48
49
50
51
52
# File 'app/models/comfy/cms/layout.rb', line 45

def self.app_layouts_for_select(view_paths)
  view_paths.map(&:to_s).select { |path| path.start_with?(Rails.root.to_s) }.flat_map do |full_path|
    Dir.glob("#{full_path}/layouts/**/*.html.*").collect do |filename|
      filename.gsub!("#{full_path}/layouts/", "")
      filename.split("/").last[0...1] == "_" ? nil : filename.split(".").first
    end.compact.sort
  end.compact.uniq.sort
end

.options_for_select(site, layout = nil, current_layout = nil, depth = 0, spacer = ". . ") ⇒ Object

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



32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/comfy/cms/layout.rb', line 32

def self.options_for_select(site, layout = nil, current_layout = nil, depth = 0, spacer = ". . ")
  out = []
  [current_layout || site.layouts.roots.order(:position)].flatten.each do |l|
    next if layout == l
    out << ["#{spacer * depth}#{l.label}", l.id]
    l.children.order(:position).each do |child|
      out += options_for_select(site, layout, child, depth + 1, spacer)
    end
  end
  out.compact
end

Instance Method Details

#cache_busterObject



83
84
85
# File 'app/models/comfy/cms/layout.rb', line 83

def cache_buster
  updated_at.to_i
end

#clear_page_content_cacheObject

Forcing page content reload



88
89
90
91
# File 'app/models/comfy/cms/layout.rb', line 88

def clear_page_content_cache
  Comfy::Cms::Page.where(id: pages.pluck(:id)).update_all(content_cache: nil)
  children.each(&:clear_page_content_cache)
end

#content_tokensObject

– Instance Methods ——————————————————– Tokenized layout content that also pulls in parent layout (if there’s one) and merges on the content} tag (if parent layout has that). Returns a list of tokens that can be fed into the renderer.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/comfy/cms/layout.rb', line 58

def content_tokens
  renderer  = ComfortableMexicanSofa::Content::Renderer.new(nil)
  tokens    = renderer.tokenize(content)

  if parent
    fragment_tags = ComfortableMexicanSofa::Content::Tag::Fragment.subclasses.map do |c|
      ComfortableMexicanSofa::Content::Renderer.tags.key(c)
    end

    parent_tokens = parent.content_tokens
    replacement_position = parent_tokens.index do |n|
      n.is_a?(Hash) &&
      fragment_tags.member?(n[:tag_class]) &&
      n[:tag_params].split(%r{\s}).first == "content"
    end

    if replacement_position
      parent_tokens[replacement_position] = tokens
      tokens = parent_tokens.flatten
    end
  end

  tokens
end