diff --git a/CHANGELOG.md b/CHANGELOG.md index 73b834bb65..e47658cec5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * enhancements * Allow resource class scopes to override the global configuration for `sign_in_after_reset_password` behaviour. [#5429](https://github.com/heartcombo/devise/pull/5429) [@mattr](https://github.com/mattr) * Refactor conditional dirty tracking logic to a centralized module to simplify usage throughout the codebase. [#5575](https://github.com/heartcombo/devise/pull/5575) + * Improve support for Devise in apps with Active Record and Mongoid ORMs loaded, so it does not incorrectly uses new Active Record dirty tracking APIs with a Mongoid Devise model. [#5576](https://github.com/heartcombo/devise/pull/5576) * bug fixes * Fix frozen string exception in validatable. [#5563](https://github.com/heartcombo/devise/pull/5563) [#5465](https://github.com/heartcombo/devise/pull/5465) [@mameier](https://github.com/mameier) diff --git a/lib/devise.rb b/lib/devise.rb index e0749eb824..ca1130d9e9 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -13,7 +13,7 @@ module Devise autoload :Encryptor, 'devise/encryptor' autoload :FailureApp, 'devise/failure_app' autoload :OmniAuth, 'devise/omniauth' - autoload :OrmDirtyTracking, 'devise/orm_dirty_tracking' + autoload :Orm, 'devise/orm' autoload :ParameterFilter, 'devise/parameter_filter' autoload :ParameterSanitizer, 'devise/parameter_sanitizer' autoload :TestHelpers, 'devise/test_helpers' diff --git a/lib/devise/models.rb b/lib/devise/models.rb index 1dc5753b04..fb7dd89b06 100644 --- a/lib/devise/models.rb +++ b/lib/devise/models.rb @@ -84,7 +84,7 @@ def devise(*modules) end devise_modules_hook! do - include Devise::OrmDirtyTracking + include Devise::Orm include Devise::Models::Authenticatable selected_modules.each do |m| diff --git a/lib/devise/models/confirmable.rb b/lib/devise/models/confirmable.rb index 0f74d07578..6ce22c30f0 100644 --- a/lib/devise/models/confirmable.rb +++ b/lib/devise/models/confirmable.rb @@ -48,7 +48,7 @@ module Confirmable included do before_create :generate_confirmation_token, if: :confirmation_required? after_create :skip_reconfirmation_in_callback!, if: :send_confirmation_notification? - if defined?(ActiveRecord) && self < ActiveRecord::Base # ActiveRecord + if Devise::Orm.active_record?(self) # 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/lib/devise/orm_dirty_tracking.rb b/lib/devise/orm.rb similarity index 69% rename from lib/devise/orm_dirty_tracking.rb rename to lib/devise/orm.rb index 07391108fb..75baf2be50 100644 --- a/lib/devise/orm_dirty_tracking.rb +++ b/lib/devise/orm.rb @@ -1,10 +1,22 @@ module Devise - module OrmDirtyTracking # :nodoc: - def self.activerecord51? - defined?(ActiveRecord) && ActiveRecord.gem_version >= Gem::Version.new("5.1.x") + module Orm # :nodoc: + def self.active_record?(model) + defined?(ActiveRecord) && model < ActiveRecord::Base end - if activerecord51? + def self.active_record_51?(model) + active_record?(model) && ActiveRecord.gem_version >= Gem::Version.new("5.1.x") + end + + def self.included(model) + if Devise::Orm.active_record_51?(model) + model.include DirtyTrackingNewMethods + else + model.include DirtyTrackingOldMethods + end + end + + module DirtyTrackingNewMethods def devise_email_before_last_save email_before_last_save end @@ -28,7 +40,9 @@ def devise_will_save_change_to_email? def devise_respond_to_and_will_save_change_to_attribute?(attribute) respond_to?("will_save_change_to_#{attribute}?") && send("will_save_change_to_#{attribute}?") end - else + end + + module DirtyTrackingOldMethods def devise_email_before_last_save email_was end