Skip to content

Commit

Permalink
Add Devise.activerecord51?
Browse files Browse the repository at this point in the history
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.
lucasmazza committed May 10, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 399774a commit 3e1c9e3
Showing 5 changed files with 11 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/devise.rb
Original file line number Diff line number Diff line change
@@ -294,6 +294,10 @@ def self.rails51? # :nodoc:
Rails.gem_version >= Gem::Version.new("5.1.x")
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
# a fresh initializer with all configuration values.
def self.setup
4 changes: 2 additions & 2 deletions lib/devise/models/confirmable.rb
Original file line number Diff line number Diff line change
@@ -253,7 +253,7 @@ def generate_confirmation_token!
generate_confirmation_token && save(validate: false)
end

if Devise.rails51?
if Devise.activerecord51?
def postpone_email_change_until_confirmation_and_regenerate_confirmation_token
@reconfirmation_required = true
self.unconfirmed_email = self.email
@@ -271,7 +271,7 @@ def postpone_email_change_until_confirmation_and_regenerate_confirmation_token
end
end

if Devise.rails51?
if Devise.activerecord51?
def postpone_email_change?
postpone = self.class.reconfirmable &&
will_save_change_to_email? &&
6 changes: 3 additions & 3 deletions lib/devise/models/database_authenticatable.rb
Original file line number Diff line number Diff line change
@@ -137,7 +137,7 @@ def authenticatable_salt
encrypted_password[0,29] if encrypted_password
end

if Devise.rails51?
if Devise.activerecord51?
# Send notification to user when email changes.
def send_email_changed_notification
send_devise_notification(:email_changed, to: email_before_last_save)
@@ -165,7 +165,7 @@ def password_digest(password)
Devise::Encryptor.digest(self.class, password)
end

if Devise.rails51?
if Devise.activerecord51?
def send_email_changed_notification?
self.class.send_email_changed_notification && saved_change_to_email?
end
@@ -175,7 +175,7 @@ def send_email_changed_notification?
end
end

if Devise.rails51?
if Devise.activerecord51?
def send_password_change_notification?
self.class.send_password_change_notification && saved_change_to_encrypted_password?
end
2 changes: 1 addition & 1 deletion lib/devise/models/recoverable.rb
Original file line number Diff line number Diff line change
@@ -97,7 +97,7 @@ def send_reset_password_instructions_notification(token)
send_devise_notification(:reset_password_instructions, token, {})
end

if Devise.rails51?
if Devise.activerecord51?
def clear_reset_password_token?
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|
2 changes: 1 addition & 1 deletion lib/devise/models/validatable.rb
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ def self.included(base)

base.class_eval do
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_format_of :email, with: email_regexp, allow_blank: true, if: :will_save_change_to_email?
else

1 comment on commit 3e1c9e3

@estebanutz
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lib/devise/models/validatable.rb I found examples like this "xx@x." which I agree are pretty strange passes through as valid email. Could we use what's on the rails docs? unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i

Please sign in to comment.