Class: Arbre::HTML::Element

Inherits:
Object show all
Includes:
Arbre::HTML, BuilderMethods
Defined in:
lib/active_admin/arbre/element.rb

Direct Known Subclasses

Context, Tag, TextNode

Constant Summary

Constants included from Arbre::HTML

AUTO_BUILD_ELEMENTS, HTML5_ELEMENTS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BuilderMethods

#build_tag, #current_dom_context, #insert_tag, #insert_text_node_if_string, #with_current_dom_context

Methods included from Arbre::HTML

#current_dom_context, #method_missing

Constructor Details

#initialize(assigns = {}, helpers = nil) ⇒ Element

Returns a new instance of Element.



19
20
21
22
# File 'lib/active_admin/arbre/element.rb', line 19

def initialize(assigns = {}, helpers = nil)
  @_assigns, @_helpers = assigns, helpers
  @children = Collection.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Arbre::HTML

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



9
10
11
# File 'lib/active_admin/arbre/element.rb', line 9

def children
  @children
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/active_admin/arbre/element.rb', line 8

def parent
  @parent
end

Class Method Details

.builder_method(method_name) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/active_admin/arbre/element.rb', line 11

def self.builder_method(method_name)
  ::Arbre::HTML::BuilderMethods.class_eval <<-EOF, __FILE__, __LINE__
    def #{method_name}(*args, &block)
      insert_tag #{self.name}, *args, &block
    end
  EOF
end

Instance Method Details

#+(element) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/active_admin/arbre/element.rb', line 128

def +(element)
  case element
  when Element, Collection
  else
    element = TextNode.from_string(element)
  end
  Collection.new([self]) + element
end

#<<(child) ⇒ Object



69
70
71
# File 'lib/active_admin/arbre/element.rb', line 69

def <<(child)
  add_child(child)
end

#add_child(child) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_admin/arbre/element.rb', line 41

def add_child(child)
  return unless child

  if child.is_a?(Array)
    child.each{|item| add_child(item) }
    return @children
  end

  # If its not an element, wrap it in a TextNode
  unless child.is_a?(Element)
    child = TextNode.from_string(child)
  end

  if child.respond_to?(:parent)
    # Remove the child
    child.parent.remove_child(child) if child.parent
    # Set ourselves as the parent
    child.parent = self
  end

  @children << child
end

#assignsObject



24
25
26
# File 'lib/active_admin/arbre/element.rb', line 24

def assigns
  @_assigns
end

#build(*args, &block) ⇒ Object



36
37
38
39
# File 'lib/active_admin/arbre/element.rb', line 36

def build(*args, &block)
  # Render the block passing ourselves in
  insert_text_node_if_string(block.call(self)) if block
end

#contentObject



100
101
102
# File 'lib/active_admin/arbre/element.rb', line 100

def content
  children.to_html
end

#content=(contents) ⇒ Object



85
86
87
88
# File 'lib/active_admin/arbre/element.rb', line 85

def content=(contents)
  clear_children!
  add_child(contents)
end

#documentObject



81
82
83
# File 'lib/active_admin/arbre/element.rb', line 81

def document
  parent.document if parent?
end

#each(&block) ⇒ Object



112
113
114
# File 'lib/active_admin/arbre/element.rb', line 112

def each(&block)
  [to_html].each(&block)
end

#get_elements_by_tag_name(tag_name) ⇒ Object Also known as: find_by_tag



90
91
92
93
94
95
96
97
# File 'lib/active_admin/arbre/element.rb', line 90

def get_elements_by_tag_name(tag_name)
  elements = Collection.new
  children.each do |child|
    elements << child if child.tag_name == tag_name
    elements.concat(child.get_elements_by_tag_name(tag_name))
  end
  elements
end

#helpersObject



28
29
30
# File 'lib/active_admin/arbre/element.rb', line 28

def helpers
  @_helpers
end

#html_safeObject



104
105
106
# File 'lib/active_admin/arbre/element.rb', line 104

def html_safe
  to_html
end

#indent_levelObject



108
109
110
# File 'lib/active_admin/arbre/element.rb', line 108

def indent_level
  parent? ? parent.indent_level + 1 : 0
end

#parent?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/active_admin/arbre/element.rb', line 77

def parent?
  !@parent.nil?
end

#remove_child(child) ⇒ Object



64
65
66
67
# File 'lib/active_admin/arbre/element.rb', line 64

def remove_child(child)
  child.parent = nil if child.respond_to?(:parent=)
  @children.delete(child)
end

#tag_nameObject



32
33
34
# File 'lib/active_admin/arbre/element.rb', line 32

def tag_name
  @tag_name ||= self.class.name.demodulize.downcase
end

#to_aryObject Also known as: to_a



137
138
139
# File 'lib/active_admin/arbre/element.rb', line 137

def to_ary
  Collection.new [self]
end

#to_htmlObject



124
125
126
# File 'lib/active_admin/arbre/element.rb', line 124

def to_html
  content
end

#to_sObject



116
117
118
# File 'lib/active_admin/arbre/element.rb', line 116

def to_s
  to_html
end

#to_strObject



120
121
122
# File 'lib/active_admin/arbre/element.rb', line 120

def to_str
  to_s
end