Module: Arbre::Rails::Layouts::ContextMethods

Defined in:
lib/arbre/rails/layouts.rb

Overview

ContextMethods

Instance Method Summary collapse

Instance Method Details

#documentObject #document(klass, *build_arguments, &content_block) ⇒ Object

Obtains and/or sets the current content document.

Overloads:

  • #documentObject

    Gets an object representing the content document to create. This is mostly for internal use.

  • #document(klass, *build_arguments, &content_block) ⇒ Object

    Specifies a document class and arguments to use. You may optionally customize it using a block.

    Parameters:

    • klass (Class)

      The document class to use.

    • build_arguments (Array)

      Any arguments to pass to the document’s build method.

    • content_block (Proc)

      A block to be executed as the content block. See above for more info.



85
86
87
# File 'lib/arbre/rails/layouts.rb', line 85

def document(klass = nil, *build_arguments, &content_block)
  controller.send :arbre_document, klass, *build_arguments, &content_block
end

#layout(&layout_block) ⇒ Arbre::Html::Document

Provides a layout block to the current view and appends the current document to the current arbre element.

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/arbre/rails/layouts.rb', line 93

def layout(&layout_block)
  doc = if document && document.content_block
    # Use the specified document with the specified content block.

    # Detect whether the block is called.
    block_called = false
    content_block = document.content_block
    doc = append(document.klass, *document.build_arguments) do |*args|
      block_called = true
      instance_exec *args, &content_block
    end

    # Call the block anyway if it hasn't been called yet.
    doc.instance_exec &document.content_block unless block_called
    doc

  elsif document
    # Use the specified document.
    append document.klass, *document.build_arguments
  else
    # Append an empty document.
    append Arbre::Rails.legacy_document
  end

  # Run the layout block unless this is an AJAX request.
  doc.instance_exec &layout_block if layout_block && !request.xhr?
  doc
end