Skip to content

Commit 30f7c7a

Browse files
committed
Support flash[:alert] = <String>
Passing strings as messages to `flash[]` is more common than using other primitives, and users deciding to set flash[:alert] might not be aware of the Hash structure that is required to correctly construct and display a UI alert from flash[:alert]. So instead of letting it fail because of the type mismatch (when calling #slice on a string instead of a hash), we can add a fallback option to treat given string as a body of the alert message and construct a `danger` type alert with default title.
1 parent 4329459 commit 30f7c7a

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

admin/app/components/solidus_admin/layout/flashes/alerts/component.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@
33
class SolidusAdmin::Layout::Flashes::Alerts::Component < SolidusAdmin::BaseComponent
44
attr_reader :alerts
55

6+
# Construct alert flashes like:
7+
# flash[:alert] = { <alert_type>: { title: "", description: "" } }
8+
# See +SolidusAdmin::UI::Alert::Component::SCHEMES+ for available alert types.
9+
#
10+
# If a string is passed to flash[:alert], we treat it is a body of the alert message and fall back to +danger+ type
11+
# and default title (see +SolidusAdmin::UI::Alert::Component+).
612
def initialize(alerts:)
7-
@alerts = alerts
13+
if alerts.is_a?(String)
14+
alerts = { danger: { description: alerts } }
15+
end
16+
17+
@alerts = alerts.slice(*SolidusAdmin::UI::Alert::Component::SCHEMES.keys)
818
end
919
end

admin/app/helpers/solidus_admin/flash_helper.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ def toasts
77
flash.to_hash.with_indifferent_access.except(:alert)
88
end
99

10-
# Construct alert flashes like +flash[:alert] = { <alert_type>: { title: "", description: "" } }+.
11-
# See +SolidusAdmin::UI::Alert::Component::SCHEMES+ for available alert types.
1210
def alerts
13-
flash.to_hash.with_indifferent_access.fetch(:alert, {}).slice(*SolidusAdmin::UI::Alert::Component::SCHEMES.keys)
11+
flash.to_hash.with_indifferent_access.fetch(:alert, {})
1412
end
1513
end
1614
end

0 commit comments

Comments
 (0)