Skip to content

Commit 761c7ff

Browse files
committed
move alert_class to ApplicationHelper (+ minor refactor); use valid css class in show_error_messages! helper
1 parent 898d6fe commit 761c7ff

File tree

5 files changed

+25
-20
lines changed

5 files changed

+25
-20
lines changed

app/helpers/application_helper.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def show_error_messages!(resource)
4141
messages = resource.errors.
4242
full_messages.map { |msg| content_tag(:li, msg) }.join
4343
html = <<-HTML
44-
<div class="alert alert-error alert-block">
44+
<div class="alert alert-danger">
4545
<button type="button" class="close" data-dismiss="alert">x</button>
4646
<ul>
4747
#{messages}
@@ -83,4 +83,15 @@ def get_body_css_class(controller)
8383

8484
"#{classes[controller]}"
8585
end
86+
87+
def alert_class(alert)
88+
case alert
89+
when 'error', 'alert'
90+
'alert-danger'
91+
when 'notice'
92+
'alert-success'
93+
else
94+
'alert-info'
95+
end
96+
end
8697
end

app/helpers/messages_helper.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

app/views/layouts/_messages.html.erb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
<div class="alert <%= alert_class(key) %>">
55
<button type="button" class="close" data-dismiss="alert">x</button>
6-
<ul>
7-
<li>
8-
<%= value %>
9-
</li>
10-
</ul>
6+
<ul>
7+
<li>
8+
<%= value %>
9+
</li>
10+
</ul>
1111
</div>
1212
<% end %>

config/initializers/simple_form.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
config.error_notification_tag = :div
6767

6868
# CSS class to add for error notification helper.
69-
config.error_notification_class = 'alert alert-error'
69+
config.error_notification_class = 'alert alert-danger'
7070

7171
# ID to add for error notification helper.
7272
# config.error_notification_id = nil

spec/helpers/application_helper_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@
1919
expect(helper.seconds_to_hm(3600)).to eq("1:00")
2020
end
2121
end
22+
23+
it 'alert_class returns specific error classes' do
24+
expect(helper.alert_class('error')).to eq('alert-danger')
25+
expect(helper.alert_class('alert')).to eq('alert-danger')
26+
expect(helper.alert_class('notice')).to eq('alert-success')
27+
expect(helper.alert_class('foo')).to eq('alert-info')
28+
end
2229
end

0 commit comments

Comments
 (0)