Permalink
Browse files

Add flash feedback to user

  • Loading branch information...
avdgaag committed Mar 24, 2015
1 parent 455fc58 commit 877e60896cec14e905c51eaddb9d06d3c93c5666
View
@@ -62,7 +62,7 @@ class Application < Lotus::Application
#
# See: http://www.rubydoc.info/gems/rack/Rack/Session/Cookie
#
# sessions :cookie, secret: ENV['WEB_SESSIONS_SECRET']
sessions :cookie, secret: ENV['WEB_SESSIONS_SECRET']
# Configure Rack middleware for this application
#
@@ -190,6 +190,7 @@ class Application < Lotus::Application
controller.prepare do
# include MyAuthentication # included in all the actions
# before :authenticate! # run an authentication before callback
expose :flash
end
# Configure the code that will yield each time Web::View is included
@@ -13,8 +13,10 @@ def initialize(comment_repository: Demo::CommentRepository)
end
def call(params)
if params.valid?
@comment_repository.create(Demo::Comment.new(params))
if params.valid? && @comment_repository.create(Demo::Comment.new(params))
flash[:notice] = 'Comment added'
else
flash[:alert] = 'Something went wrong. Please try again.'
end
redirect_to "/articles/#{params[:article_id]}"
end
@@ -6,6 +6,11 @@
</head>
<body>
<div id="page">
<% each_flash do |style, message| %>
<div class="flash flash--<%= style %>">
<%= message %>
</div>
<% end %>
<h1><a href="<%= routes.root_path %>" rel="home">Lotus example blog</a></h1>
<%= yield %>
</div>
@@ -2,6 +2,13 @@ module Web
module Views
class ApplicationLayout
include Web::Layout
def each_flash
%i(alert notice).each do |type|
message = @scope.locals[:flash][type]
yield type, message if message
end
end
end
end
end
@@ -28,5 +28,15 @@ module Web::Controllers::Comments
comment_repository.verify
end
end
it 'assigns a flash notice when comment was created' do
action.call(params)
assert_equal 'Comment added', action.send(:flash)[:notice]
end
it 'assigns a flash alert when comment was not created' do
action.call({})
assert_equal 'Something went wrong. Please try again.', action.send(:flash)[:alert]
end
end
end
@@ -25,6 +25,7 @@
click_button 'Submit Comment'
assert page.has_css?('.comments__heading', text: '1 comment'), 'there is no comments heading'
assert page.has_css?('.comment__body', text: 'Nice post!'), 'there is no comment'
assert page.has_css?('.flash--notice', text: 'Comment added'), 'there was no flash notice'
end
end
end

0 comments on commit 877e608

Please sign in to comment.