Module: Arbre::Builder::BuilderMethods

Included in:
HTML::Element
Defined in:
lib/active_admin/arbre/builder.rb

Instance Method Summary collapse

Instance Method Details

#append_return_block(tag) ⇒ Object

Appends the value to the current DOM element if there are no existing DOM Children and it responds to #to_s



103
104
105
106
107
108
109
# File 'lib/active_admin/arbre/builder.rb', line 103

def append_return_block(tag)
  return nil if current_dom_context.children.any?

  if !tag.is_a?(Arbre::HTML::Element) && tag.respond_to?(:to_s)
    current_dom_context << Arbre::HTML::TextNode.from_string(tag.to_s)
  end
end

#build_tag(klass, *args, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/active_admin/arbre/builder.rb', line 52

def build_tag(klass, *args, &block)
  tag = klass.new(assigns, helpers)

  # If you passed in a block and want the object
  if block_given? && block.arity > 0
    # Set out context to the tag, and pass responsibility to the tag
    with_current_dom_context tag do
      tag.build(*args, &block)
    end
  else
    # Build the tag
    tag.build(*args)

    # Render the blocks contents
    if block_given?
      with_current_dom_context tag do
        append_return_block(yield)
      end
    end
  end

  tag
end

#current_dom_contextObject



82
83
84
85
86
87
88
89
90
# File 'lib/active_admin/arbre/builder.rb', line 82

def current_dom_context
  @__current_dom_element_buffer__ ||= [self]
  current_element = @__current_dom_element_buffer__.last
  if current_element == self
    self
  else
    current_element.current_dom_context
  end
end

#insert_tag(klass, *args, &block) ⇒ Object



76
77
78
79
80
# File 'lib/active_admin/arbre/builder.rb', line 76

def insert_tag(klass, *args, &block)
  tag = build_tag(klass, *args, &block)
  current_dom_context.add_child(tag)
  tag
end

#with_current_dom_context(tag) ⇒ Object Also known as: within

Raises:

  • (ArgumentError)


92
93
94
95
96
97
98
# File 'lib/active_admin/arbre/builder.rb', line 92

def with_current_dom_context(tag)
  raise ArgumentError, "Can't be in the context of nil. #{@__current_dom_element_buffer__.inspect}" unless tag
  current_dom_context # Ensure a context is setup
  @__current_dom_element_buffer__.push tag
  yield
  @__current_dom_element_buffer__.pop
end