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

Inherits:
Object
  • Object
show all
Defined in:
lib/ipaddr-ext/actionview/tag_helper.rb

Instance Method Summary collapse

Instance Method Details

#tag_option(key, value, escape) ⇒ Object

Overwrite to fix FormHelper#text_field build value attribute of IPAddr without prefix github.com/rails/rails/pull/52365



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ipaddr-ext/actionview/tag_helper.rb', line 15

def tag_option(key, value, escape)
  key = ERB::Util.xml_name_escape(key) if escape

  case value
  when Array, Hash
    value = TagHelper.build_tag_values(value) if key.to_s == "class"
    value = escape ? safe_join(value, " ") : value.join(" ")
  when Regexp
    value = escape ? ERB::Util.unwrapped_html_escape(value.source) : value.source
  when IPAddr
    addr = value.to_s
    unless (value.ipv4? && value.prefix == 32) || (value.ipv6? && value.prefix == 128)
      addr = format("%s/%s", value.to_s, value.prefix)
    end

    value = escape ? ERB::Util.unwrapped_html_escape(addr) : addr
  else
    value = escape ? ERB::Util.unwrapped_html_escape(value) : value.to_s
  end
  value = value.gsub('"', """) if value.include?('"')

  %(#{key}="#{value}")
end