From d1948b79d3e933253baa753bd033c92171c0a7d0 Mon Sep 17 00:00:00 2001 From: Leonardo Tegon Date: Tue, 16 Jan 2018 10:25:20 -0200 Subject: [PATCH] Fix `ActiveRecord` check on `Confirmable` (#4752) * Fix `ActiveRecord` check on `Confirmable` As pointed out by @dark-panda in #4302, the condition for an `ActiveRecord` model is wrong inside the `Confirmable` initialization block. https://github.com/plataformatec/devise/pull/4302#issuecomment-355103489 * Add specs --- lib/devise/models/confirmable.rb | 2 +- test/models/confirmable_test.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/devise/models/confirmable.rb b/lib/devise/models/confirmable.rb index 0bd9d409..ca1acf92 100644 --- a/lib/devise/models/confirmable.rb +++ b/lib/devise/models/confirmable.rb @@ -48,7 +48,7 @@ module Devise included do before_create :generate_confirmation_token, if: :confirmation_required? after_create :skip_reconfirmation_in_callback!, if: :send_confirmation_notification? - if defined?(ActiveRecord) && self.is_a?(ActiveRecord::Base) # ActiveRecord + if defined?(ActiveRecord) && self < ActiveRecord::Base # ActiveRecord after_commit :send_on_create_confirmation_instructions, on: :create, if: :send_confirmation_notification? after_commit :send_reconfirmation_instructions, on: :update, if: :reconfirmation_required? else # Mongoid diff --git a/test/models/confirmable_test.rb b/test/models/confirmable_test.rb index d0446642..739dc779 100644 --- a/test/models/confirmable_test.rb +++ b/test/models/confirmable_test.rb @@ -8,6 +8,17 @@ class ConfirmableTest < ActiveSupport::TestCase setup_mailer end + test 'should set callbacks to send the mail' do + if DEVISE_ORM == :active_record + defined_callbacks = User._commit_callbacks.map(&:filter) + assert_includes defined_callbacks, :send_on_create_confirmation_instructions + assert_includes defined_callbacks, :send_reconfirmation_instructions + elsif DEVISE_ORM == :mongoid + assert_includes User._create_callbacks.map(&:filter), :send_on_create_confirmation_instructions + assert_includes User._update_callbacks.map(&:filter), :send_reconfirmation_instructions + end + end + test 'should generate confirmation token after creating a record' do assert_nil new_user.confirmation_token assert_not_nil create_user.confirmation_token