Skip to content

Instantly share code, notes, and snippets.

@marcioj
Created October 14, 2014 21:52
Show Gist options
  • Save marcioj/bfb50b0b60b8e4ede962 to your computer and use it in GitHub Desktop.
Save marcioj/bfb50b0b60b8e4ede962 to your computer and use it in GitHub Desktop.
Supress devise flash messages
require 'rails_helper'
RSpec.describe Devise::SessionsController, :type => :controller do
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
end
def action
post :create
end
context "action filtered" do
controller do
self.supress_devise_flash = :create
def create
set_flash_message(:notice, "message")
render text: "hello world"
end
end
before { action }
it { should_not set_the_flash }
end
context "action not filtered" do
controller do
self.supress_devise_flash = []
def create
set_flash_message(:notice, "message")
render text: "hello world"
end
end
before { action }
it { should set_the_flash }
end
context "filtered non existent action" do
controller do
self.supress_devise_flash = [:update]
def create
set_flash_message(:notice, "message")
render text: "hello world"
end
end
before { action }
it { should set_the_flash }
end
end
Devise::SessionsController.supress_devise_flash = :create, :destroy
module SupressDeviseFlashMessage
extend ActiveSupport::Concern
included do
class_attribute :supress_devise_flash, instance_accessor: false
self.supress_devise_flash = []
def set_flash_message_with_supress(*args)
set_flash_message_without_supress(*args) unless Array.wrap(self.class.supress_devise_flash).include?(params[:action].to_sym)
end
alias_method_chain :set_flash_message, :supress
end
end
DeviseController.send(:include, SupressDeviseFlashMessage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment