Module: Comfy::Cms::WithFragments

Extended by:
ActiveSupport::Concern
Included in:
Page, Translation
Defined in:
app/models/concerns/comfy/cms/with_fragments.rb

Instance Method Summary collapse

Instance Method Details

#clear_content_cacheObject

Blanking cache on page saves so it can be regenerated on access



108
109
110
# File 'app/models/concerns/comfy/cms/with_fragments.rb', line 108

def clear_content_cache
  write_attribute(:content_cache, nil)
end

#clear_content_cache!Object

Nuking content cache so it can be regenerated.



103
104
105
# File 'app/models/concerns/comfy/cms/with_fragments.rb', line 103

def clear_content_cache!
  update_column(:content_cache, nil)
end

#content_cacheObject

If content_cache column is populated we don’t need to call render for this page.



94
95
96
97
98
99
100
# File 'app/models/concerns/comfy/cms/with_fragments.rb', line 94

def content_cache
  if (cache = read_attribute(:content_cache)).nil?
    cache = render
    update_column(:content_cache, cache) unless new_record?
  end
  cache
end

#fragment_nodesObject

Grabbing nodes that we need to render form elements in the admin area Rejecting duplicates as we’d need to render only one form field. Don’t declare duplicate tags on the layout. That’s wierd (but still works).



79
80
81
82
83
# File 'app/models/concerns/comfy/cms/with_fragments.rb', line 79

def fragment_nodes
  nodes
    .select { |n| n.is_a?(ComfortableMexicanSofa::Content::Tag::Fragment) }
    .uniq(&:identifier)
end

#fragments_attributes(was = false) ⇒ Object

Snapshop of page fragments data used primarily for saving revisions



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/concerns/comfy/cms/with_fragments.rb', line 57

def fragments_attributes(was = false)
  fragments.collect do |frag|
    attrs = {}
    %i[identifier tag content datetime boolean].each do |column|
      attrs[column] = frag.send(was ? "#{column}_was" : column)
    end
    # TODO: save files against revision (not on db though)
    # attrs[:files] = frag.attachments.collect do |a|
    #   {io: a.download, filename: a.filename.to_s, content_type: a.content_type}
    # end
    attrs
  end
end

#fragments_attributes=(frag_hashes = []) ⇒ Object

Array of fragment hashes in the following format:

[
  {identifier: "frag_a", format: "text", content: "fragment a content"},
  {identifier: "frag_b", format: "file", files: [{file_a}, {file_b}]}
]

It also handles when frag hashes come in as a hash:

{
  "0" => {identifer: "foo", content: "bar"},
  "1" => {identifier: "bar", content: "foo"}
}


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/concerns/comfy/cms/with_fragments.rb', line 35

def fragments_attributes=(frag_hashes = [])
  frag_hashes = frag_hashes.values if frag_hashes.is_a?(Hash)

  frag_hashes.each do |frag_attrs|
    unless frag_attrs.is_a?(HashWithIndifferentAccess)
      frag_attrs.symbolize_keys!
    end

    identifier = frag_attrs.delete(:identifier)

    fragment =
      fragments.detect { |f| f.identifier == identifier } ||
      fragments.build(identifier: identifier)

    fragment.attributes = frag_attrs

    # tracking dirty
    self.fragments_attributes_changed ||= fragment.changed?
  end
end

#fragments_attributes_wasObject

Method to collect prevous state of blocks for revisions



72
73
74
# File 'app/models/concerns/comfy/cms/with_fragments.rb', line 72

def fragments_attributes_was
  fragments_attributes(:previous_values)
end

#render(n = nodes) ⇒ Object

Rendered content of the page. We grab whatever layout is associated with the page and feed its content tokens to the renderer while passing this page as context.



88
89
90
# File 'app/models/concerns/comfy/cms/with_fragments.rb', line 88

def render(n = nodes)
  renderer.render(n)
end