Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add feature to deliver later #696

Merged
merged 6 commits into from
Jun 22, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
use deliver later if enabled and rails support it
  • Loading branch information
wendy0402 committed Jun 9, 2015
commit f7653f17856c029683b01b435c829803c28aa826
7 changes: 7 additions & 0 deletions lib/generators/sorcery/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,13 @@
#
# user.activation_mailer_disabled =

# when true sorcery will send email activation
# asynchronously using deliver_later if exists
# otherwise deliver synchronously
# Default: `false`
#
# user.deliver_later_enabled =


# activation needed email method on your mailer class.
# Default: `:activation_needed_email`
Expand Down
6 changes: 3 additions & 3 deletions lib/sorcery/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ def generic_send_email(method, mailer)
mail = config.send(mailer).send(config.send(method),self)
if defined?(ActionMailer) and config.send(mailer).kind_of?(Class) and config.send(mailer) < ActionMailer::Base
# Rails 4.2 deprecates #deliver
if mail.respond_to?(:deliver_now)
mail.deliver_now
if config.deliver_later_enabled && mail.respond_to?(:deliver_later)
mail.deliver_later
else
mail.deliver
mail.respond_to?(:deliver_now) ? mail.deliver_now : mail.deliver
end
end
end
Expand Down
11 changes: 8 additions & 3 deletions lib/sorcery/model/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,22 @@ class Config
:before_authenticate, # an array of method names to call before authentication
# completes. used internally.

:deliver_later_enabled, # when true sorcery will send email activation
# asynchronously using deliver_later if exists
# otherwise deliver synchronously


:after_config # an array of method names to call after configuration by user.
# used internally.

attr_reader :encryption_provider, # change default encryption_provider.
:custom_encryption_provider, # use an external encryption class.
:encryption_algorithm # encryption algorithm name. See 'encryption_algorithm=' below
# for available options.

def initialize
@defaults = {
:@submodules => [],
:@username_attribute_names => [:email],
:@username_attribute_names => [:email],
:@password_attribute_name => :password,
:@downcase_username_before_authenticating => false,
:@email_attribute_name => :email,
Expand All @@ -54,7 +58,8 @@ def initialize
:@stretches => nil,
:@subclasses_inherit_config => false,
:@before_authenticate => [],
:@after_config => []
:@after_config => [],
:@deliver_later_enabled => false,
}
reset!
end
Expand Down
29 changes: 29 additions & 0 deletions spec/shared_examples/user_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
expect(User.sorcery_config.stretches).to eq stretches
end

it "enables configuration option 'deliver_later_enabled" do
sorcery_model_property_set(:deliver_later_enabled, true)
expect(User.sorcery_config.deliver_later_enabled).to eq true
end

it 'respond to username=' do
expect(User.new).to respond_to(:username=)
end
Expand Down Expand Up @@ -267,6 +272,30 @@ class Admin2 < User; end
end
end

describe "generic send email" do
before do
@mail = double('mail')
allow(::SorceryMailer).to receive(:activation_success_email).and_return(@mail)

sorcery_reload!([:user_activation, :user_activation_mailer, :activation_needed_email_method_name, :deliver_later_enabled],
user_activation_mailer: SorceryMailer ,activation_needed_email_method_name: nil, deliver_later_enabled: true)
user = create_new_user
end

it "using deliver_later if enabled and rails support" do
allow(@mail).to receive(:respond_to?).with(:deliver_later) { true }
expect(@mail).to receive(:deliver_later).once
user.activate!
end

it "using deliver_now if rails not support" do
allow(@mail).to receive(:respond_to?).with(:deliver_later) { false }
allow(@mail).to receive(:respond_to?).with(:deliver_now) { true }
expect(@mail).to receive(:deliver_now).once
user.activate!
end
end

describe "special encryption cases" do
before(:all) do
sorcery_reload!()
Expand Down