Add `Devise.activerecord51?`

Active Record changed it's public API, so we should check against its
version instead of Rails as it is possible to use Rails 5.1 with Mongoid,
which still has the older Dirty API.

However, this patch does not fixes a scenario where an app has both
Active Record and Mongoid loaded. It should be fixed by either normalizing
the Mongoid/ActiveRecord API or replacing the conditional method
definitions with a shim layer that abstracts this away.
This commit is contained in:
Lucas Mazza 2017-05-10 17:09:38 -03:00
parent 399774af69
commit 3e1c9e343c
No known key found for this signature in database
GPG Key ID: C009F9A6BE4A44CB
5 changed files with 11 additions and 7 deletions

View File

@ -294,6 +294,10 @@ module Devise
Rails.gem_version >= Gem::Version.new("5.1.x") Rails.gem_version >= Gem::Version.new("5.1.x")
end end
def self.activerecord51? # :nodoc:
defined?(ActiveRecord) && ActiveRecord.gem_version >= Gem::Version.new("5.1.x")
end
# Default way to set up Devise. Run rails generate devise_install to create # Default way to set up Devise. Run rails generate devise_install to create
# a fresh initializer with all configuration values. # a fresh initializer with all configuration values.
def self.setup def self.setup

View File

@ -253,7 +253,7 @@ module Devise
generate_confirmation_token && save(validate: false) generate_confirmation_token && save(validate: false)
end end
if Devise.rails51? if Devise.activerecord51?
def postpone_email_change_until_confirmation_and_regenerate_confirmation_token def postpone_email_change_until_confirmation_and_regenerate_confirmation_token
@reconfirmation_required = true @reconfirmation_required = true
self.unconfirmed_email = self.email self.unconfirmed_email = self.email
@ -271,7 +271,7 @@ module Devise
end end
end end
if Devise.rails51? if Devise.activerecord51?
def postpone_email_change? def postpone_email_change?
postpone = self.class.reconfirmable && postpone = self.class.reconfirmable &&
will_save_change_to_email? && will_save_change_to_email? &&

View File

@ -137,7 +137,7 @@ module Devise
encrypted_password[0,29] if encrypted_password encrypted_password[0,29] if encrypted_password
end end
if Devise.rails51? if Devise.activerecord51?
# Send notification to user when email changes. # Send notification to user when email changes.
def send_email_changed_notification def send_email_changed_notification
send_devise_notification(:email_changed, to: email_before_last_save) send_devise_notification(:email_changed, to: email_before_last_save)
@ -165,7 +165,7 @@ module Devise
Devise::Encryptor.digest(self.class, password) Devise::Encryptor.digest(self.class, password)
end end
if Devise.rails51? if Devise.activerecord51?
def send_email_changed_notification? def send_email_changed_notification?
self.class.send_email_changed_notification && saved_change_to_email? self.class.send_email_changed_notification && saved_change_to_email?
end end
@ -175,7 +175,7 @@ module Devise
end end
end end
if Devise.rails51? if Devise.activerecord51?
def send_password_change_notification? def send_password_change_notification?
self.class.send_password_change_notification && saved_change_to_encrypted_password? self.class.send_password_change_notification && saved_change_to_encrypted_password?
end end

View File

@ -97,7 +97,7 @@ module Devise
send_devise_notification(:reset_password_instructions, token, {}) send_devise_notification(:reset_password_instructions, token, {})
end end
if Devise.rails51? if Devise.activerecord51?
def clear_reset_password_token? def clear_reset_password_token?
encrypted_password_changed = respond_to?(:will_save_change_to_encrypted_password?) && will_save_change_to_encrypted_password? encrypted_password_changed = respond_to?(:will_save_change_to_encrypted_password?) && will_save_change_to_encrypted_password?
authentication_keys_changed = self.class.authentication_keys.any? do |attribute| authentication_keys_changed = self.class.authentication_keys.any? do |attribute|

View File

@ -27,7 +27,7 @@ module Devise
base.class_eval do base.class_eval do
validates_presence_of :email, if: :email_required? validates_presence_of :email, if: :email_required?
if Devise.rails51? if Devise.activerecord51?
validates_uniqueness_of :email, allow_blank: true, if: :will_save_change_to_email? validates_uniqueness_of :email, allow_blank: true, if: :will_save_change_to_email?
validates_format_of :email, with: email_regexp, allow_blank: true, if: :will_save_change_to_email? validates_format_of :email, with: email_regexp, allow_blank: true, if: :will_save_change_to_email?
else else