Class: ActionView::Helpers::TagHelper::TagBuilder

Inherits:
Object
  • Object
show all
Defined in:
actionview/lib/action_view/helpers/tag_helper.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context) ⇒ TagBuilder

Returns a new instance of TagBuilder.



213
214
215
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 213

def initialize(view_context)
  @view_context = view_context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(called, *args, escape: true, **options, &block) ⇒ Object (private)



321
322
323
324
325
326
327
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 321

def method_missing(called, *args, escape: true, **options, &block)
  name = called.name.dasherize

  TagHelper.ensure_valid_html5_tag_name(name)

  tag_string(name, *args, options, escape: escape, &block)
end

Class Method Details

.define_element(name, code_generator:, method_name: name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 47

def self.define_element(name, code_generator:, method_name: name)
  return if method_defined?(name)

  code_generator.class_eval do |batch|
    batch << "\n" <<
      "def #{method_name}(content = nil, escape: true, **options, &block)" <<
      "  tag_string(#{name.inspect}, content, options, escape: escape, &block)" <<
      "end"
  end
end

.define_self_closing_element(name, code_generator:, method_name: name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 67

def self.define_self_closing_element(name, code_generator:, method_name: name)
  code_generator.class_eval do |batch|
    batch << "\n" <<
      "def #{method_name}(content = nil, escape: true, **options, &block)" <<
      "  if content || block" <<
      "    tag_string(#{name.inspect}, content, options, escape: escape, &block)" <<
      "  else" <<
      "   self_closing_tag_string(#{name.inspect}, options, escape)" <<
      "  end" <<
      "end"
  end
end

.define_void_element(name, code_generator:, method_name: name) ⇒ Object



58
59
60
61
62
63
64
65
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 58

def self.define_void_element(name, code_generator:, method_name: name)
  code_generator.class_eval do |batch|
    batch << "\n" <<
      "def #{method_name}(escape: true, **options, &block)" <<
      "  self_closing_tag_string(#{name.inspect}, options, escape, '>')" <<
      "end"
  end
end

Instance Method Details

#attributes(attributes) ⇒ Object

Transforms a Hash into HTML Attributes, ready to be interpolated into ERB.

<input <%= tag.attributes(type: :text, aria: { label: "Search" }) %> >
# => <input type="text" aria-label="Search">


222
223
224
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 222

def attributes(attributes)
  tag_options(attributes.to_h).to_s.strip.html_safe
end

#content_tag_string(name, content, options, escape = true) ⇒ Object

:nodoc:



226
227
228
229
230
231
232
233
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 226

def (name, content, options, escape = true) # :nodoc:
  tag_options = tag_options(options, escape) if options

  if escape && content.present?
    content = ERB::Util.unwrapped_html_escape(content)
  end
  "<#{name}#{tag_options}>#{PRE_CONTENT_STRINGS[name]}#{content}</#{name}>".html_safe
end

#tag_options(options, escape = true) ⇒ Object

:nodoc:



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 235

def tag_options(options, escape = true) # :nodoc:
  return if options.blank?
  output = +""
  sep    = " "
  options.each_pair do |key, value|
    type = TAG_TYPES[key]
    if type == :data && value.is_a?(Hash)
      value.each_pair do |k, v|
        next if v.nil?
        output << sep
        output << prefix_tag_option(key, k, v, escape)
      end
    elsif type == :aria && value.is_a?(Hash)
      value.each_pair do |k, v|
        next if v.nil?

        case v
        when Array, Hash
          tokens = TagHelper.build_tag_values(v)
          next if tokens.none?

          v = @view_context.safe_join(tokens, " ")
        else
          v = v.to_s
        end

        output << sep
        output << prefix_tag_option(key, k, v, escape)
      end
    elsif type == :boolean
      if value
        output << sep
        output << boolean_tag_option(key)
      end
    elsif !value.nil?
      output << sep
      output << tag_option(key, value, escape)
    end
  end
  output unless output.empty?
end