Class: Arbre::HTML::Element

Inherits:
Object
  • Object
show all
Includes:
Builder, Builder::BuilderMethods
Defined in:
lib/active_admin/arbre/html/element.rb

Direct Known Subclasses

Context, Tag, TextNode

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Builder::BuilderMethods

#append_return_block, #build_tag, #current_dom_context, #insert_tag, #with_current_dom_context

Methods included from Builder

#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/html/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::Builder

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/active_admin/arbre/html/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/html/element.rb', line 11

def self.builder_method(method_name)
  ::Arbre::Builder::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



134
135
136
137
138
139
140
141
# File 'lib/active_admin/arbre/html/element.rb', line 134

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

#<<(child) ⇒ Object



69
70
71
# File 'lib/active_admin/arbre/html/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/html/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 = Arbre::HTML::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/html/element.rb', line 24

def assigns
  @_assigns
end

#build(*args, &block) ⇒ Object



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

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

#contentObject



110
111
112
# File 'lib/active_admin/arbre/html/element.rb', line 110

def content
  children.to_s
end

#content=(contents) ⇒ Object



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

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

#documentObject



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

def document
  parent.document if parent?
end

#each(&block) ⇒ Object



122
123
124
# File 'lib/active_admin/arbre/html/element.rb', line 122

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

#get_elements_by_class_name(class_name) ⇒ Object Also known as: find_by_class



100
101
102
103
104
105
106
107
# File 'lib/active_admin/arbre/html/element.rb', line 100

def get_elements_by_class_name(class_name)
  elements = Collection.new
  children.each do |child|
    elements << child if child.class_list =~ /#{class_name}/
    elements.concat(child.get_elements_by_tag_name(tag_name))
  end
  elements
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/html/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/html/element.rb', line 28

def helpers
  @_helpers
end

#html_safeObject



114
115
116
# File 'lib/active_admin/arbre/html/element.rb', line 114

def html_safe
  to_s
end

#indent_levelObject



118
119
120
# File 'lib/active_admin/arbre/html/element.rb', line 118

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

#parent?Boolean

Returns:

  • (Boolean)


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

def parent?
  !@parent.nil?
end

#remove_child(child) ⇒ Object



64
65
66
67
# File 'lib/active_admin/arbre/html/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/html/element.rb', line 32

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

#to_aryObject Also known as: to_a



143
144
145
# File 'lib/active_admin/arbre/html/element.rb', line 143

def to_ary
  Collection.new [self]
end

#to_sObject



130
131
132
# File 'lib/active_admin/arbre/html/element.rb', line 130

def to_s
  content
end

#to_strObject



126
127
128
# File 'lib/active_admin/arbre/html/element.rb', line 126

def to_str
  to_s
end