Module: NdrUi::Bootstrap::ErrorAndWarningAlertBoxes

Included in:
NdrUi::BootstrapBuilder
Defined in:
app/builders/ndr_ui/bootstrap/error_and_warning_alert_boxes.rb

Overview

This mixin provides standard error and warning boxes. A translated heading is needed for base and other error and warning messages.

The overall error alertbox heading uses:

en:

errors:
  alertbox:
    heading: "%{reason} prevented this record from being saved."

Where reason is the number of errors e.g. “1 error” or “3 errors”

For :base errors use the following heading translations:

en:

errors:
  alertbox:
    base:
      readonly: "Overall errors:"
      editing: "Overall errors:"

If there is no difference in the readonly/editing messages then you can use

en:

errors:
  alertbox:
    base: "Overall errors:"

For all other non-base errors use the following heading translations:

en:

errors:
  alertbox:
    other:
      readonly: "Specific errors:"
      editing: "Specific errors (click on labels!):"

The pattern is exactly the same for warnings:

The overall warning alertbox heading uses:

en:

warnings:
  alertbox:
    heading: "%{reason} prevented this record from being saved."

Where reason is the number of warnings e.g. “1 warning” or “3 warnings”

For :base warnings use the following heading translations:

en:

warnings:
  alertbox:
    base:
      readonly: "Overall warnings:"
      editing: "Overall warnings:"

If there is no difference in the readonly/editing messages then you can use

en:

warnings:
  alertbox:
    base: "Overall errors:"

For all other non-base errors use the following heading translations:

en:

warnings:
  alertbox:
    other:
      readonly: "Specific warnings:"
      editing: "Specific warnings (click on labels!):"

Constant Summary collapse

SAFE_BLANK_STRING =
ERB::Util.html_escape('').freeze
ALERT_BOX_TYPE =

Map issue types to Boostrap alert styles

{ errors: :danger, warnings: :warning }.freeze

Instance Method Summary collapse

Instance Method Details

#base_issues(type, messages) ⇒ Object



106
107
108
109
110
# File 'app/builders/ndr_ui/bootstrap/error_and_warning_alert_boxes.rb', line 106

def base_issues(type, messages)
  issue_wrapper(type, :base, messages) do |message|
    message
  end
end

#error_alert_boxObject



86
87
88
# File 'app/builders/ndr_ui/bootstrap/error_and_warning_alert_boxes.rb', line 86

def error_alert_box
  issue_alert_box(:errors)
end

#error_and_warning_alert_boxesObject



82
83
84
# File 'app/builders/ndr_ui/bootstrap/error_and_warning_alert_boxes.rb', line 82

def error_and_warning_alert_boxes
  error_alert_box + warning_alert_box
end

#issue_alert_box(type) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
101
102
103
104
# File 'app/builders/ndr_ui/bootstrap/error_and_warning_alert_boxes.rb', line 94

def issue_alert_box(type)
  raise ArgumentError unless [:errors, :warnings].include?(type)
  return SAFE_BLANK_STRING unless object && object.respond_to?(type) && object.send(type).any?

  issues = object.send(type).to_hash
  @template.bootstrap_alert_tag(ALERT_BOX_TYPE[type]) do
    alertbox_heading(type, issues) +
      base_issues(type, issues[:base]) +
      non_base_issues(type, issues.except(:base))
  end
end

#issue_wrapper(type, name, messages, &block) ⇒ Object

This wraps the issues in a paragraph and puts them into an unordered list



119
120
121
122
123
124
125
126
127
128
129
# File 'app/builders/ndr_ui/bootstrap/error_and_warning_alert_boxes.rb', line 119

def issue_wrapper(type, name, messages, &block)
  return SAFE_BLANK_STRING if messages.blank?

  @template.(:p) do
    wrapper_heading(type, name) +
      @template.(:ul) do
        list_items = messages.map(&block).map { |item| @template.(:li, item) }
        @template.safe_join(list_items)
      end
  end
end

#non_base_issues(type, messages) ⇒ Object



112
113
114
115
116
# File 'app/builders/ndr_ui/bootstrap/error_and_warning_alert_boxes.rb', line 112

def non_base_issues(type, messages)
  issue_wrapper(type, :other, messages) do |attribute, errors|
    readonly_friendly_label(attribute) + ' ' + errors.to_sentence
  end
end

#warning_alert_boxObject



90
91
92
# File 'app/builders/ndr_ui/bootstrap/error_and_warning_alert_boxes.rb', line 90

def warning_alert_box
  issue_alert_box(:warnings)
end