Skip to content

Commit

Permalink
Merge pull request #5576 from heartcombo/ca-multiple-orms
Browse files Browse the repository at this point in the history
Improve support for Devise in apps with multiple ORMs loaded
  • Loading branch information
carlosantoniodasilva committed Mar 30, 2023
2 parents 8dbe5b2 + 207ddc5 commit 506eaf4
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
* Failure app will respond with configured `redirect_status` instead of `error_status` if the recall app returns a redirect status (300..399) [#5573](https://github.com/heartcombo/devise/pull/5573)
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 506eaf4

Please sign in to comment.