Module: RailsBootstrapErrorRenderer

Defined in:
lib/rails_bootstrap_error_renderer.rb,
lib/rails_bootstrap_error_renderer/version.rb

Constant Summary collapse

IGNORED_TYPES =
%w[checkbox hidden]
VERSION =
"1.2.0"

Class Method Summary collapse

Class Method Details

.applicable_input?(element) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rails_bootstrap_error_renderer.rb', line 37

def self.applicable_input?(element)
  %w[input select textarea].include?(element.name) && !IGNORED_TYPES.member?(element.attributes["type"]&.to_s)
end

.render(html, instance) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/rails_bootstrap_error_renderer.rb', line 10

def self.render(html, instance)
  fragment = Nokogiri::HTML::DocumentFragment.parse(html)
  element = fragment.children[0]
  if should_process_element?(element)
    render_for_element(element, instance)
  else
    html
  end
end

.render_for_element(element, instance) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails_bootstrap_error_renderer.rb', line 25

def self.render_for_element(element, instance)
  element.add_class("is-invalid")
  if applicable_input? element
    msgs = Array.wrap(instance.error_message).map { |m| %(<div class="invalid-feedback">#{m}</div>) }.join("\n")
    instance.raw(
      %(#{element.to_html}\n#{msgs})
    )
  else
    instance.raw element.to_html
  end
end

.should_process_element?(element) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/rails_bootstrap_error_renderer.rb', line 20

def self.should_process_element?(element)
  return unless element
  element.attributes["data-render-errors"].to_s != "false"
end