Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add #skip_confirmation_notification! to Confirmable #2296

Merged
merged 1 commit into from

4 participants

@gregates

This pull request adds an instance method, #skip_confirmation_notification!, which sets a flag on a confirmable object that will skip the send_on_create_confirmation_instructions callback, allowing you to manually send_confirmation_instructions at your leisure.

I have an app in which I want to create a confirmable user record as part of a transaction, and only send the confirmation e-mail if the entire transaction succeeds. #skip_confirmation! is not the correct solution for my use case, because it does not only skip sending the confirmation e-mail, but it also immediately confirms the user account.

This stackoverflow post and devise issue #2198 also suggest that there are other use cases in which this behavior would be desirable.

This is my first attempt at contributing to an open source project, and I'm pretty new to software development generally, so I would appreciate any feedback on ways this pull request could be improved.

@lucasmazza
Owner

The code looks good, so @gregates could you add an entry to the CHANGELOG about it? :smile:

@josevalim josevalim merged commit 81c6d70 into from
@gregates

I was just about to when it got merged! Still want me to add a changelog entry?

@lucasmazza
Owner

@gregates feel free to send another PR.

@gregates gregates referenced this pull request from a commit
@gregates gregates Changelog for #2296 77b960f
@gregates gregates referenced this pull request
Merged

Changelog for #2296 #2305

@jraczak

Anyone having any trouble with this? Getting a no_method_error tagging this onto a Facebook registration. :cofirmable is set in model, used .new and .save instead of create. From what I can tell should be working. Any thoughts?

@josevalim
Owner

pls check the changelog. was this feature released? if so, are you using a version that contains it?

@jraczak
@jraczak

I see it in master but maybe not appearing in a release yet. Any ETA on 2.2.4/releasing this feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 22, 2013
  1. @gregates
This page is out of date. Refresh to see the latest.
View
12 lib/devise/models/confirmable.rb
@@ -34,7 +34,7 @@ module Confirmable
included do
before_create :generate_confirmation_token, :if => :confirmation_required?
- after_create :send_on_create_confirmation_instructions, :if => :confirmation_required?
+ after_create :send_on_create_confirmation_instructions, :if => :send_confirmation_notification?
before_update :postpone_email_change_until_confirmation, :if => :postpone_email_change?
after_update :send_confirmation_instructions, :if => :reconfirmation_required?
end
@@ -119,6 +119,12 @@ def skip_confirmation!
self.confirmed_at = Time.now.utc
end
+ # Skips sending the confirmation notification email after_create. Unlike
+ # #skip_confirmation!, record still requires confirmation.
+ def skip_confirmation_notification!
+ @skip_confirmation_notification = true
+ end
+
# If you don't want reconfirmation to be sent, neither a code
# to be generated, call skip_reconfirmation!
def skip_reconfirmation!
@@ -223,6 +229,10 @@ def reconfirmation_required?
self.class.reconfirmable && @reconfirmation_required
end
+ def send_confirmation_notification?
+ confirmation_required? && !@skip_confirmation_notification
+ end
+
module ClassMethods
# Attempt to find a user by its email. If a record is found, send new
# confirmation instructions to it. If not, try searching for a user by unconfirmed_email
View
10 test/models/confirmable_test.rb
@@ -104,6 +104,16 @@ def setup
end
end
+ test 'should skip confirmation e-mail without confirming if skip_confirmation_notification! is invoked' do
+ user = new_user
+ user.skip_confirmation_notification!
+
+ assert_email_not_sent do
+ user.save!
+ assert !user.confirmed?
+ end
+ end
+
test 'should find a user to send confirmation instructions' do
user = create_user
confirmation_user = User.send_confirmation_instructions(:email => user.email)
Something went wrong with that request. Please try again.