Commit 700f6c4

mo khan <mo@mokhan.ca>
2014-11-16 00:14:08
display flash messages and add first functional test.
1 parent c6b4562
Changed files (5)
app/helpers/application_helper.rb
@@ -1,2 +1,5 @@
 module ApplicationHelper
+  def alert_class_for(flash_type)
+    "alert-#{{ success: 'success', error: 'danger', alert: 'warning', notice: 'info' }.fetch(flash_type.to_sym, flash_type)}"
+  end
 end
app/views/layouts/public.html.erb
@@ -14,6 +14,7 @@
   </head>
   <body>
     <div class="container">
+      <%= render partial: 'shared/flash' %>
       <%= yield %>
     </div> <!-- /container -->
   </body>
app/views/shared/_flash.html.erb
@@ -0,0 +1,6 @@
+<% flash.each do |type, message| %>
+  <div class="alert <%= alert_class_for(type) %> alert-dismissible fade in">
+    <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
+    <%= message %>
+  </div>
+<% end if flash.any? %>
spec/features/login_spec.rb
@@ -12,4 +12,16 @@ describe "the signin process", type: :feature do
     click_button "Sign in"
     expect(page).to have_content("Dashboard")
   end
+
+  context "when the password is incorrect" do
+    it 'displays an error' do
+      visit root_path
+      within ".form-signin" do
+        fill_in 'email', with: user.email
+        fill_in 'password', with: 'wrong'
+      end
+      click_button "Sign in"
+      expect(page).to have_content(I18n.translate(:invalid_credentials))
+    end
+  end
 end
spec/helpers/application_helper_spec.rb
@@ -0,0 +1,15 @@
+require 'rails_helper'
+
+describe ApplicationHelper do
+  describe "#alert_class_for" do
+    it { expect(alert_class_for(:success)).to eql('alert-success') }
+
+    it { expect(alert_class_for(:error)).to eql('alert-danger') }
+
+    it { expect(alert_class_for(:alert)).to eql('alert-warning') }
+
+    it { expect(alert_class_for(:notice)).to eql('alert-info') }
+
+    it { expect(alert_class_for(:unknown)).to eql('alert-unknown') }
+  end
+end