Class: Bootstrap::Component

Inherits:
Object
  • Object
show all
Includes:
BasicTags, Delegate
Defined in:
mod/bootstrap/lib/bootstrap/component.rb,
mod/bootstrap/lib/bootstrap/component/form.rb,
mod/bootstrap/lib/bootstrap/component/panel.rb,
mod/bootstrap/lib/bootstrap/component/layout.rb,
mod/bootstrap/lib/bootstrap/component/carousel.rb,
mod/bootstrap/lib/bootstrap/component/horizontal_form.rb

Direct Known Subclasses

Carousel, Form, OldComponent

Defined Under Namespace

Classes: Carousel, Form, HorizontalForm, Layout, Panel

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Delegate

#method_missing, #respond_to_missing?

Methods included from BasicTags

#html

Constructor Details

#initialize(context, *args, &block) ⇒ Component

Returns a new instance of Component.



66
67
68
69
70
71
72
73
74
75
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 66

def initialize context, *args, &block
  @context = context
  @content = ["".html_safe]
  @args = args
  @child_args = []
  @append = []
  @wrap = []
  @build_block = block
  @html = Builder::XmlMarkup.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Bootstrap::Delegate

Class Method Details

.def_div_method(name, html_class, opts = {}, &tag_block) ⇒ Object

Like def_tag_method but always generates a div tag The tag option is not available



84
85
86
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 84

def def_div_method name, html_class, opts={}, &tag_block
  def_tag_method name, html_class, opts.merge(tag: :div), &tag_block
end

.def_simple_tag_method(method_name, tag, html_class, tag_opts = {}) ⇒ Object



132
133
134
135
136
137
138
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 132

def def_simple_tag_method method_name, tag, html_class, tag_opts={}
  define_method method_name do |*args, &content_block|
    @html.tag! tag, class: html_class do
      instance_exec &content_block
    end
  end
end

.def_tag_method(method_name, html_class, tag_opts = {}, &tag_opts_block) ⇒ Object

Defines a method that generates a html tag def add_tag_method name, html_class, tag_opts={}, &tag_block @tag_method = TagMethod.new self,name, html_class, tag_opts, &tag_block define_method name do |*args, &block| @tag_method.call *args, &block process_tag tag_opts[:tag] || name do content, opts, new_child_args = standardize_args args, &tag_block add_classes opts, html_class, tag_opts.delete(:optional_classes) if (attributes = tag_opts.delete(:attributes)) opts.merge! attributes end

  content = with_child_args new_child_args do
    generate_content content,
                     tag_opts[:content_processor],
                     &block
  end

  [content, opts]
end

end end

Examples:

def_tag_method :link, "known-link", tag: :a, id: "uniq-link"
link  # => <a class="known-link" id="uniq-link"></a>

Parameters:

  • name (Symbol, String)

    the name of the method. If no :tag option in tag_opts is defined then the name is also the name of the tag that the method generates

  • html_class (String)

    a html class that is added to tag. Use nil if you don't want a html_class

  • tag_opts (Hash) (defaults to: {})

    additional argument that will be added to the tag

Options Hash (tag_opts):

  • tag (Symbol, String)

    the name of the tag



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 118

def def_tag_method method_name, html_class, tag_opts={}, &tag_opts_block
  tag = tag_opts.delete(:tag) || method_name
  return def_simple_tag_method method_name, tag, html_class, tag_opts unless block_given?

  define_method method_name do |*args, &content_block|
    content, opts, new_child_args = standardize_args args, &tag_opts_block
    add_classes opts, html_class, tag_opts.delete(:optional_classes)

    @html.tag! tag, opts do
      instance_exec &content_block
    end
  end
end

.render(format, *args, &block) ⇒ Object



78
79
80
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 78

def render format, *args, &block
  new(format, *args, &block).render
end

Instance Method Details

#append(&block) ⇒ Object



159
160
161
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 159

def append &block
  @append[-1] << block
end

#insert(&block) ⇒ Object



155
156
157
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 155

def insert &block
  instance_exec &block
end

#prepend(&block) ⇒ Object



149
150
151
152
153
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 149

def prepend &block
  tmp = @content.pop
  instance_exec &block
  @content << tmp
end

#renderObject



142
143
144
145
146
147
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 142

def render
  @rendered = begin
    render_content
    # @content[-1]
  end
end

#wrap(tag = nil, &block) ⇒ Object



163
164
165
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 163

def wrap tag=nil, &block
  @wrap[-1] << (block_given? ? block : tag)
end