From f9e4ea6e1dd6c10ff941f93d2f011a5656bc8f1e Mon Sep 17 00:00:00 2001 From: David Kus Date: Fri, 1 May 2015 15:05:38 -0400 Subject: [PATCH] Defining the logging_in callback when the controller loads (along with the helper_methods) This will ensure that the callback gets defined. Before, define_callbacks was being run before define_concern_callbacks, and so the callback was never defined. --- lib/devise-guests/controllers/helpers.rb | 14 +------------ .../application_controller_spec.rb | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/devise-guests/controllers/helpers.rb b/lib/devise-guests/controllers/helpers.rb index fb0b982..32de694 100644 --- a/lib/devise-guests/controllers/helpers.rb +++ b/lib/devise-guests/controllers/helpers.rb @@ -4,31 +4,18 @@ module Helpers included do include ActiveSupport::Callbacks - (DeviseGuests::Controllers::Helpers.callbacks || []).each do |c| - define_callbacks *c - end - end - mattr_reader :callbacks - module ClassMethods end - def self.define_concern_callbacks *args - @@callbacks ||= [] - @@callbacks << args - end - def self.define_helpers(mapping) #:nodoc: class_name = mapping.class_name mapping = mapping.name class_eval <<-METHODS, __FILE__, __LINE__ + 1 - define_concern_callbacks :logging_in_#{mapping} - def guest_#{mapping} return @guest_#{mapping} if @guest_#{mapping} @@ -89,6 +76,7 @@ def unique_#{mapping}_counter ActiveSupport.on_load(:action_controller) do helper_method "guest_#{mapping}", "current_or_guest_#{mapping}" + define_callbacks "logging_in_#{mapping}" end end end diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index f3d756b..2a3b776 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -24,4 +24,25 @@ expect(@controller.current_or_guest_user).to eq(m) end + it "should run the 'logging_in' callbacks" do + + # A user is logged in + current_user = double() + allow(@controller).to receive(:current_user) { current_user } + + # There is a guest user instance + guest_user = double() + allow(guest_user).to receive(:destroy) + allow(@controller).to receive(:guest_user) { guest_user } + allow(@controller).to receive(:session) { { guest_user_id: 123 } } + + expect(@controller).to receive(:run_callbacks).with(:logging_in_user).and_call_original + + @controller.current_or_guest_user + end + + it "should define a 'logging_in' callback" do + expect(@controller.respond_to? :_logging_in_user_callbacks).to be true + end + end