Class: Sinicum::Content::Aggregator

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/sinicum/content/aggregator.rb

Overview

Public: Handles the content and provides access to it during a request

Class Method Summary collapse

Methods included from Logger

included, #logger

Class Method Details

.active_pageObject

Retrieves the current “active page”, comparable to the Magnolia property “actpage”.

Returns the Node with the current “active page” or the original_content if no active page has explicitly been set.



38
39
40
# File 'lib/sinicum/content/aggregator.rb', line 38

def active_page
  stack(:__cms_active_page).first || original_content
end

.cleanObject

Resets all stacks. Should always be called at the end of a request



151
152
153
154
155
156
157
158
# File 'lib/sinicum/content/aggregator.rb', line 151

def clean
  Thread.current[:__cms_original_content] = nil
  Thread.current[:__cms_active_page] = nil
  Thread.current[:__cms_stack] = nil
  Thread.current[:__content_node_stack] = nil
  Thread.current[:__content_iterator_stack] = nil
  Thread.current[:__content_iterator_stack_key] = nil
end

.content_dataObject

Retrieves the content_data object currently on top of the stack.

Returns the current Node.



60
61
62
63
64
65
66
# File 'lib/sinicum/content/aggregator.rb', line 60

def content_data
  if stack && stack.length > 0
    stack.last
  else
    original_content
  end
end

.content_nodeContent?

Retrieves the current content_node or nil if none is set

has been set

Returns:

  • (Content, nil)

    Usually a Content-like object, nil if no content_node



142
143
144
145
146
147
148
# File 'lib/sinicum/content/aggregator.rb', line 142

def content_node
  if stack(:__content_node_stack) && stack(:__content_node_stack).length > 0
    stack(:__content_node_stack).last
  else
    nil
  end
end

.node_iteratorObject



109
110
111
# File 'lib/sinicum/content/aggregator.rb', line 109

def node_iterator
  stack(:__content_iterator_stack).last
end

.node_iterator_keyObject



105
106
107
# File 'lib/sinicum/content/aggregator.rb', line 105

def node_iterator_key
  stack(:__content_iterator_stack_key).last
end

.original_contentObject

Retrieves the original_content object.

Returns a Node with the original content object.



29
30
31
# File 'lib/sinicum/content/aggregator.rb', line 29

def original_content
  Thread.current[:__cms_original_content]
end

.original_content=(node) ⇒ Object

Public: Set the “original content” of a request, i.e. the content object that represents the “base” of the request, before any other object is subsequently pushed. Usually the content object of the node corresponding to the URI called.

Usually, this method should only be called once during a request.

When this method is called, all other stacks (content_data, content_node) are being reset.

node - The Node with the original content object.

Returns nothing.



21
22
23
24
# File 'lib/sinicum/content/aggregator.rb', line 21

def original_content=(node)
  clean
  Thread.current[:__cms_original_content] = node
end

.push(node, &block) ⇒ Object

Push a new content_data object on top of the stack and pop it after returning from the block

node - The Node to push on top of the stack



72
73
74
75
76
77
78
79
# File 'lib/sinicum/content/aggregator.rb', line 72

def push(node, &block)
  stack.push(node)
  begin
    yield
  ensure
    pop
  end
end

.push_active_page(node, &block) ⇒ Object

Sets a new “active page”, makes it the new cms_content_data and executes the given block in this context. Resets all changes afterwards.

node - The node with the active active page.



46
47
48
49
50
51
52
53
54
55
# File 'lib/sinicum/content/aggregator.rb', line 46

def push_active_page(node, &block)
  stack(:__cms_active_page).push(node)
  begin
    push(node) do
      yield
    end
  ensure
    pop(:__cms_active_page)
  end
end

.push_content_node(content_node_name, &block) ⇒ Object

Convenience method for pushing the contents of a ContentNode with the name content_node_name, based on the current content_data object, on top of the stack and popping it afterwards. If no node with the name exists, an empty Hash is pushed.

Primarily intended to be used with the cms_content_node helper

current content_data object

Parameters:

  • content_node_name (String)

    the name of the content_node as it is stored in the



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/sinicum/content/aggregator.rb', line 121

def push_content_node(content_node_name, &block)
  node_data = nil
  if content_data && content_data.key?(content_node_name)
    node_data = content_data[content_node_name]
  else
    node_data = {}
  end
  stack(:__content_node_stack).push(content_node_name)
  stack.push(node_data)
  begin
    yield
  ensure
    pop
    pop(:__content_node_stack)
  end
end

.push_current_content(node, &block) ⇒ Object

Push a new Content object on top of the stack and update Magnolia’s current_content.

node - The object to push, must wrap a Magnolia Content object.



85
86
87
88
89
90
91
92
# File 'lib/sinicum/content/aggregator.rb', line 85

def push_current_content(node, &block)
  stack.push(node)
  begin
    yield
  ensure
    pop
  end
end

.push_node_iterator(node, key) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/sinicum/content/aggregator.rb', line 94

def push_node_iterator(node, key)
  stack(:__content_iterator_stack).push(node)
  stack(:__content_iterator_stack_key).push(key)
  begin
    yield
  ensure
    pop(:__content_iterator_stack)
    pop(:__content_iterator_stack_key)
  end
end