minor refactoring of flash messages

This commit is contained in:
Hauke Klement
2015-03-23 16:42:57 +01:00
parent 2e596110fd
commit 4eef3d70d5
12 changed files with 46 additions and 49 deletions

View File

@ -17,8 +17,7 @@ class ApplicationController < ActionController::Base
end end
def render_not_authorized def render_not_authorized
flash[:danger] = t('application.not_authorized') redirect_to(:root, alert: t('application.not_authorized'))
redirect_to(:root)
end end
private :render_not_authorized private :render_not_authorized

View File

@ -39,8 +39,7 @@ class InternalUsersController < ApplicationController
def deliver_reset_password_instructions def deliver_reset_password_instructions
if params[:email].present? if params[:email].present?
InternalUser.find_by(email: params[:email]).try(:deliver_reset_password_instructions!) InternalUser.find_by(email: params[:email]).try(:deliver_reset_password_instructions!)
flash[:notice] = t('.success') redirect_to(:root, notice: t('.success'))
redirect_to(:root)
end end
end end
private :deliver_reset_password_instructions private :deliver_reset_password_instructions
@ -77,10 +76,7 @@ class InternalUsersController < ApplicationController
end end
def render_forgot_password_form def render_forgot_password_form
if current_user redirect_to(:root, alert: t('shared.already_signed_in')) if current_user
flash[:warning] = t('shared.already_signed_in')
redirect_to(:root)
end
end end
private :render_forgot_password_form private :render_forgot_password_form

View File

@ -21,8 +21,7 @@ class SessionsController < ApplicationController
set_current_user set_current_user
store_lti_session_data(consumer: @consumer, parameters: params) store_lti_session_data(consumer: @consumer, parameters: params)
store_nonce(params[:oauth_nonce]) store_nonce(params[:oauth_nonce])
flash[:notice] = I18n.t("sessions.create_through_lti.session_#{lti_outcome_service? ? 'with' : 'without'}_outcome", consumer: @consumer) redirect_to(implement_exercise_path(@exercise), notice: t("sessions.create_through_lti.session_#{lti_outcome_service? ? 'with' : 'without'}_outcome", consumer: @consumer))
redirect_to(implement_exercise_path(@exercise))
end end
def destroy def destroy
@ -41,9 +40,6 @@ class SessionsController < ApplicationController
end end
def new def new
if current_user redirect_to(:root, alert: t('shared.already_signed_in')) if current_user
flash[:warning] = t('shared.already_signed_in')
redirect_to(:root)
end
end end
end end

View File

@ -22,7 +22,7 @@ module ApplicationHelper
def label_column(label) def label_column(label)
content_tag(:div, class: 'col-sm-3') do content_tag(:div, class: 'col-sm-3') do
content_tag(:strong) do content_tag(:strong) do
translation_present?("activerecord.attributes.#{label}") ? t("activerecord.attributes.#{label}") : t(label) I18n.translation_present?("activerecord.attributes.#{label}") ? t("activerecord.attributes.#{label}") : t(label)
end end
end end
end end
@ -60,11 +60,6 @@ module ApplicationHelper
end end
end end
def translation_present?(key)
I18n.t(key, default: '').present?
end
private :translation_present?
def value_column(value) def value_column(value)
content_tag(:div, class: 'col-sm-9') do content_tag(:div, class: 'col-sm-9') do
block_given? ? yield : symbol_for(value) block_given? ? yield : symbol_for(value)

View File

@ -11,7 +11,7 @@
- if object - if object
li = link_to(object, send(:"#{model.model_name.singular}_path", object)) li = link_to(object, send(:"#{model.model_name.singular}_path", object))
li.active li.active
- if translation_present?("shared.#{params[:action]}") - if I18n.translation_present?("shared.#{params[:action]}")
= t("shared.#{params[:action]}") = t("shared.#{params[:action]}")
- else - else
= t("#{controller_name}.index.#{params[:action]}") = t("#{controller_name}.index.#{params[:action]}")

View File

@ -13,3 +13,9 @@ unless Array.respond_to?(:to_h)
end end
end end
end end
module I18n
def self.translation_present?(key)
t(key, default: '').present?
end
end

View File

@ -22,18 +22,13 @@ describe ApplicationController do
end end
describe '#render_not_authorized' do describe '#render_not_authorized' do
let(:render_not_authorized) { controller.send(:render_not_authorized) } before(:each) do
expect(controller).to receive(:welcome) { controller.send(:render_not_authorized) }
it 'displays a flash message' do get :welcome
expect(controller).to receive(:redirect_to)
render_not_authorized
expect(flash[:danger]).to eq(I18n.t('application.not_authorized'))
end end
it 'redirects to the root URL' do expect_flash_message(:alert, I18n.t('application.not_authorized'))
expect(controller).to receive(:redirect_to).with(:root) expect_redirect(:root)
render_not_authorized
end
end end
describe '#set_locale' do describe '#set_locale' do

View File

@ -118,10 +118,7 @@ describe ExecutionEnvironmentsController do
end end
expect_assigns(docker_images: Array) expect_assigns(docker_images: Array)
expect_flash_message(:warning, :error_message)
it 'displays a flash message' do
expect(flash[:warning]).to eq(error_message)
end
end end
end end

View File

@ -46,8 +46,7 @@ describe ExercisesController do
end end
expect_assigns(exercise: Exercise) expect_assigns(exercise: Exercise)
expect_flash_message(:danger, :'shared.message_failure')
expect_flash_message(:danger)
expect_redirect(:exercise) expect_redirect(:exercise)
end end
end end

View File

@ -180,6 +180,7 @@ describe InternalUsersController do
get :forgot_password get :forgot_password
end end
expect_flash_message(:alert, :'shared.already_signed_in')
expect_redirect(:root) expect_redirect(:root)
end end
end end

View File

@ -103,25 +103,25 @@ describe SessionsController do
end end
context 'when LTI outcomes are supported' do context 'when LTI outcomes are supported' do
let(:message) { I18n.t('sessions.create_through_lti.session_with_outcome', consumer: consumer) }
before(:each) do before(:each) do
expect(controller).to receive(:lti_outcome_service?).and_return(true) expect(controller).to receive(:lti_outcome_service?).and_return(true)
request request
end end
it 'displays a flash message' do expect_flash_message(:notice, :message)
expect(flash[:notice]).to eq(I18n.t('sessions.create_through_lti.session_with_outcome', consumer: consumer))
end
end end
context 'when LTI outcomes are not supported' do context 'when LTI outcomes are not supported' do
let(:message) { I18n.t('sessions.create_through_lti.session_without_outcome', consumer: consumer) }
before(:each) do before(:each) do
expect(controller).to receive(:lti_outcome_service?).and_return(false) expect(controller).to receive(:lti_outcome_service?).and_return(false)
request request
end end
it 'displays a flash message' do expect_flash_message(:notice, :message)
expect(flash[:notice]).to eq(I18n.t('sessions.create_through_lti.session_without_outcome', consumer: consumer))
end
end end
it 'redirects to the requested exercise' do it 'redirects to the requested exercise' do
@ -206,6 +206,7 @@ describe SessionsController do
get :new get :new
end end
expect_flash_message(:alert, :'shared.already_signed_in')
expect_redirect(:root) expect_redirect(:root)
end end
end end

View File

@ -24,7 +24,7 @@ end
def expect_flash_message(type, message = nil) def expect_flash_message(type, message = nil)
it 'displays a flash message' do it 'displays a flash message' do
if message if message
expect(flash[type]).to eq(message.is_a?(String) ? message : I18n.t(message)) expect(flash[type]).to eq(obtain_message(message))
else else
expect(flash[type]).to be_present expect(flash[type]).to be_present
end end
@ -59,13 +59,25 @@ def expect_template(template)
end end
end end
def obtain_object(value) def obtain_object(object)
case value case object
when Proc when Proc
value.call object.call
when Symbol when Symbol
send(value) send(object)
else else
value object
end end
end end
private :obtain_object
def obtain_message(object)
if object.is_a?(String)
object
elsif I18n.translation_present?(object)
I18n.t(object)
else
send(object)
end
end
private :obtain_message