Class: Pulse::Forms::ErrorExplanation

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/pulse/forms/error_explanation.rb

Overview

Render error message box for an ActiveModel resource

Instance Method Summary collapse

Constructor Details

#initialize(resource:, **options) ⇒ ErrorExplanation

Returns a new instance of ErrorExplanation.



7
8
9
10
11
12
13
14
15
# File 'app/components/pulse/forms/error_explanation.rb', line 7

def initialize(resource:, **options)
  @resource = resource
  @options = options
  @options[:classes] = class_names(
    'pulse-bg-error-50 pulse-text-error pulse-px-3 pulse-py-2',
    'pulse-font-medium pulse-rounded-lg pulse-my-3',
    options[:classes]
  )
end

Instance Method Details

#errorsObject



17
18
19
20
21
22
23
24
25
# File 'app/components/pulse/forms/error_explanation.rb', line 17

def errors
  @errors = ActiveModel::Errors.new(resource)
  # Only display the last of each attribute error, to avoid needless
  # duplication.
  resource.errors.group_by(&:attribute).values.map(&:last).each do |error|
    @errors.add(error.attribute, error.message)
  end
  @errors
end

#render?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'app/components/pulse/forms/error_explanation.rb', line 27

def render?
  resource.errors.any?
end