Skip to content

Commit

Permalink
Improve support for Devise in apps with multiple ORMs loaded
Browse files Browse the repository at this point in the history
Devise is able to work with a specific ORM, either Active Record or
Mongoid, but nothing stops apps from using multiple ORMs within the same
application -- they just need to pick one to use with Devise. That's
generally determined by the require that is added to the Devise
initializer, that will load up either ORM's extensions so you can call
things like `devise` on your model to set it up.

However, some conditional logic in Devise, more specifically around
dirty tracking, was only considering having Active Record loaded up
after a certain version, to determine which methods to call in parts of
the implementation. In a previous change we refactored all that dirty
tracking code into this `OrmDirtyTracking` module to make it easier to
view all the methods that were being conditionally called, and now we're
repurposing this into a more generic `Orm` module (that's nodoc'ed by
default) so that upon including it, we can conditionally include the
proper dirty tracking extensions but also check whether the including
model is really Active Record or not, so we can trigger the correct
dirty tracking behavior for Mongoid as well if both are loaded on the
same app, whereas previously the Mongoid behavior would always use the
new Active Record behavior, but support may differ.

While we are also working to ensure the latest versions of Mongoid are
fully running with Devise, this should improve the situation by giving
apps with multiple ORMs loaded a chance to rely on some of these Devise
bits of functionality better now that weren't working properly before
without some monkey-patching on their end.

Closes #5539
Closes #4542
  • Loading branch information
carlosantoniodasilva committed Mar 23, 2023
1 parent 367ea42 commit 207ddc5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion lib/devise/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
2 changes: 1 addition & 1 deletion lib/devise/models/confirmable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 19 additions & 5 deletions lib/devise/orm_dirty_tracking.rb → lib/devise/orm.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down

0 comments on commit 207ddc5

Please sign in to comment.