Module: Arbre::HTML::BuilderMethods

Included in:
Element
Defined in:
lib/active_admin/arbre/html.rb

Instance Method Summary collapse

Instance Method Details

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_admin/arbre/html.rb', line 25

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
        insert_text_node_if_string(yield)
      end
    end
  end

  tag
end

#current_dom_contextObject



55
56
57
58
59
60
61
62
63
# File 'lib/active_admin/arbre/html.rb', line 55

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



49
50
51
52
53
# File 'lib/active_admin/arbre/html.rb', line 49

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

#insert_text_node_if_string(tag) ⇒ Object

Inserts a text node if the tag is a string



75
76
77
78
79
# File 'lib/active_admin/arbre/html.rb', line 75

def insert_text_node_if_string(tag)
  if tag.is_a?(String)
    current_dom_context << TextNode.from_string(tag)
  end
end

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

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
# File 'lib/active_admin/arbre/html.rb', line 65

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