Module: Occams::Cms::WithFragments

Extended by:
ActiveSupport::Concern
Included in:
Page, Translation
Defined in:
app/models/concerns/occams/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



107
108
109
# File 'app/models/concerns/occams/cms/with_fragments.rb', line 107

def clear_content_cache
  write_attribute(:content_cache, nil)
end

#clear_content_cache!Object

Nuking content cache so it can be regenerated.



102
103
104
# File 'app/models/concerns/occams/cms/with_fragments.rb', line 102

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.



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

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).



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

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

#fragments_attributes(was = false) ⇒ Object

Snapshop of page fragments data used primarily for saving revisions



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

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"}
}


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

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



71
72
73
# File 'app/models/concerns/occams/cms/with_fragments.rb', line 71

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.



87
88
89
# File 'app/models/concerns/occams/cms/with_fragments.rb', line 87

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