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


53
54
55
56
57
58
59
60
# File 'app/models/comfy/cms/layout.rb', line 53

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, current_layout = nil) ⇒ Object

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


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/comfy/cms/layout.rb', line 32

def self.options_for_select(site, current_layout = nil)
  options = []

  options_for_layout = ->(layout, depth = 0) do
    return if layout == current_layout

    options << ["#{'. . ' * depth}#{layout.label}", layout.id]

    layout.children.order(:position).each do |child_layout|
      options_for_layout.call(child_layout, depth + 1)
    end
  end

  site.layouts.roots.order(:position).each do |layout|
    options_for_layout.call(layout)
  end

  options
end

Instance Method Details

#cache_busterObject


91
92
93
# File 'app/models/comfy/cms/layout.rb', line 91

def cache_buster
  updated_at.to_i
end

#clear_page_content_cacheObject

Forcing page content reload


96
97
98
99
# File 'app/models/comfy/cms/layout.rb', line 96

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.


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/comfy/cms/layout.rb', line 66

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