devise-async 0.10.2 → 1.0.0beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +5 -13
  2. data/.gitignore +3 -4
  3. data/.rspec +2 -0
  4. data/.travis.yml +4 -5
  5. data/CHANGELOG.md +4 -1
  6. data/README.md +4 -0
  7. data/devise-async.gemspec +17 -24
  8. data/lib/devise/async.rb +5 -34
  9. data/lib/devise/async/model.rb +8 -30
  10. data/lib/devise/async/version.rb +1 -1
  11. data/spec/devise/async/model_spec.rb +95 -0
  12. data/spec/devise/async_spec.rb +13 -0
  13. data/spec/rails_helper.rb +23 -0
  14. data/spec/spec_helper.rb +22 -0
  15. data/{test → spec}/support/rails_app.rb +3 -2
  16. data/{test → spec}/support/rails_app/app/models/admin.rb +0 -0
  17. data/{test → spec}/support/rails_app/config/database.yml +0 -0
  18. data/{test → spec}/support/rails_app/config/initializers/devise.rb +0 -1
  19. data/{test → spec}/support/rails_app/config/routes.rb +1 -1
  20. data/spec/support/rails_app/db/schema.rb +30 -0
  21. data/spec/support/test_helpers.rb +19 -0
  22. metadata +76 -206
  23. data/lib/devise/async/backend.rb +0 -13
  24. data/lib/devise/async/backend/backburner.rb +0 -25
  25. data/lib/devise/async/backend/base.rb +0 -49
  26. data/lib/devise/async/backend/delayed_job.rb +0 -15
  27. data/lib/devise/async/backend/que.rb +0 -19
  28. data/lib/devise/async/backend/queue_classic.rb +0 -20
  29. data/lib/devise/async/backend/resque.rb +0 -18
  30. data/lib/devise/async/backend/sidekiq.rb +0 -15
  31. data/lib/devise/async/backend/sucker_punch.rb +0 -23
  32. data/lib/devise/async/backend/torquebox.rb +0 -14
  33. data/lib/devise/async/worker.rb +0 -35
  34. data/test/devise/async/backend/backburner_test.rb +0 -33
  35. data/test/devise/async/backend/base_test.rb +0 -70
  36. data/test/devise/async/backend/delayed_job_test.rb +0 -36
  37. data/test/devise/async/backend/que_test.rb +0 -21
  38. data/test/devise/async/backend/queue_classic_test.rb +0 -29
  39. data/test/devise/async/backend/resque_test.rb +0 -27
  40. data/test/devise/async/backend/sidekiq_test.rb +0 -27
  41. data/test/devise/async/backend/sucker_punch_test.rb +0 -21
  42. data/test/devise/async/backend/torquebox_test.rb +0 -21
  43. data/test/devise/async/backend_test.rb +0 -37
  44. data/test/devise/async/model_test.rb +0 -100
  45. data/test/devise/async/worker_test.rb +0 -56
  46. data/test/devise/async_test.rb +0 -31
  47. data/test/support/my_mailer.rb +0 -2
  48. data/test/support/rails_app/app/models/user.rb +0 -5
  49. data/test/support/rails_app/app/models/user_with_mailer.rb +0 -6
  50. data/test/support/rails_app/config/initializers/devise_async.rb +0 -3
  51. data/test/support/rails_app/db/schema.rb +0 -68
  52. data/test/support/test_helpers.rb +0 -38
  53. data/test/test_helper.rb +0 -31
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MzljMDZlODk5ZjdkZTQ5ZjFkNzQzZTRiYmFiZTcxNjJlMmFjMTM4Ng==
5
- data.tar.gz: !binary |-
6
- YzY1OGRhYjMyNDMzMjI4MTM3OTM5ZmZkNTc2NzRjOTM2NWViNTk1Mg==
2
+ SHA1:
3
+ metadata.gz: 8d0faaf77bf8419f09993b61edb2b44e0bca1a7e
4
+ data.tar.gz: 200860ed39d65593dc110ddc46927cf8e736dd7e
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NjU2YWU2YTgwMDU5MWZkMDIzMTgxZmVhMDExOTg2MmM3ODNmZWE5NDg1OTFi
10
- NzQ4ODM1NjkwMTgwMmM0MTUzMzRjNDJiMjQ2NTgzMzg1MTc4MWRlMDU3MmM4
11
- YmMxZWJjZmM2MjdkZWZjMGVjMWFjMGZiODcyODQwM2U5YjQwZWQ=
12
- data.tar.gz: !binary |-
13
- NWVhNTRiZDdhODExMjVhMjgzN2FjMWI3YzIyNjIzYmUyMzg3OGJjNGI3NDNj
14
- MzE2OTA4Mzk3NGFhMmY5NzdlZjk2MDdhNWUxNDM4YjM1ODE0MTQzOWU3N2Uz
15
- OGNjMDU1MmY3MWY2MTA2Yjk2ZDNmNjFkOTI5ZGIwZmM1M2RhMDU=
6
+ metadata.gz: 14dbc73e63bbf8b6286a335cf1a2dd4b8c244e0bd2e70925feefe50652230c43b38232ee1672b5b0759488ab35cd0950ca7276eb149d9ef71ab80459950c5ef3
7
+ data.tar.gz: 32ab6b13c47b1059a826b49009b53c430578acce8c0e7ef48c47ae7c84cf8597122a099a5820b4e254ccb4176f29cd766b38ccfb6f94c42bd2fb505e11314849
data/.gitignore CHANGED
@@ -13,8 +13,7 @@ lib/bundler/man
13
13
  pkg
14
14
  rdoc
15
15
  spec/reports
16
- test/tmp
17
- test/version_tmp
16
+ spec/examples.txt
17
+ spec/support/rails_app/log/*
18
+ spec/support/rails_app/db/*.sqlite3
18
19
  tmp
19
- test/support/rails_app/log/*
20
- test/support/rails_app/db/*.sqlite3
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require rails_helper
@@ -1,9 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
-
5
- services:
6
- - redis-server
3
+ - 2.2.5
4
+ - 2.3.1
5
+ - 2.4.1
7
6
 
8
7
  before_install:
9
- - gem install bundler -v 1.12.5
8
+ - gem install bundler -v 1.15
@@ -1,4 +1,7 @@
1
- ## Unreleased
1
+ ## 1.0.0beta
2
+
3
+ * Adds support for Rails 5
4
+ * Reduces this gem to use ActiveJob as an abstraction to various backends
2
5
 
3
6
  ## 0.10.2
4
7
 
data/README.md CHANGED
@@ -128,6 +128,10 @@ Older versions of Devise are supported in the [devise_2_1](https://github.com/mh
128
128
 
129
129
  Please refer to that branch README for further info.
130
130
 
131
+ ## Devise >= 4.0
132
+
133
+ The current state of this gem does not support Devise 4.0 and up. Have a look into [the Github issue](https://github.com/mhfs/devise-async/issues/94) addressing this. If you are in need for a solution to send Devise messages delayed [a switch to ActiveJob](https://github.com/plataformatec/devise#activejob-integration) is strongly advised.
134
+
131
135
  ## Contributing
132
136
 
133
137
  1. Fork it
@@ -1,38 +1,31 @@
1
- # -*- encoding: utf-8 -*-
2
1
  require File.expand_path('../lib/devise/async/version', __FILE__)
3
2
 
4
3
  Gem::Specification.new do |gem|
5
- gem.name = "devise-async"
4
+ gem.name = 'devise-async'
6
5
  gem.version = Devise::Async::VERSION
7
6
  gem.platform = Gem::Platform::RUBY
8
- gem.authors = ["Marcelo Silveira"]
9
- gem.email = ["marcelo@mhfs.com.br"]
10
- gem.description = %q{Send Devise's emails in background. Supports Backburner, Resque, Sidekiq, Delayed::Job, QueueClassic, Que, Sucker Punch and Torquebox.}
11
- gem.summary = %q{Devise Async provides an easy way to configure Devise to send its emails asynchronously using your preferred queuing backend. It supports Backburner, Resque, Sidekiq, Delayed::Job, QueueClassic, Que, Sucker Punch and Torquebox.}
12
- gem.homepage = "https://github.com/mhfs/devise-async/"
13
- gem.license = "MIT"
7
+ gem.authors = ['Sebastian Oelke', 'Marcelo Silveira']
8
+ gem.email = ['dev@soelke.de', 'marcelo@mhfs.com.br']
9
+ gem.summary = %q{Deliver Devise's emails in the background using ActiveJob.}
10
+ gem.description = %q{Devise Async provides an easy way to configure Devise to send its emails asynchronously using ActiveJob.}
11
+ gem.homepage = 'https://github.com/mhfs/devise-async/'
12
+ gem.license = 'MIT'
14
13
 
15
14
  gem.files = `git ls-files`.split($\)
16
15
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
16
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
17
 
19
- gem.require_paths = ["lib"]
18
+ gem.require_paths = ['lib']
20
19
 
21
20
 
22
- gem.add_dependency "devise", ">= 3.2", "< 4.0"
21
+ gem.add_runtime_dependency 'devise', '>= 4.0'
22
+ gem.add_runtime_dependency 'activejob', '>= 5.0'
23
23
 
24
- gem.add_development_dependency "minitest"
25
- gem.add_development_dependency "activerecord", ">= 3.2"
26
- gem.add_development_dependency "actionpack", ">= 3.2"
27
- gem.add_development_dependency "actionmailer", ">= 3.2"
28
- gem.add_development_dependency "sqlite3", "~> 1.3"
29
- gem.add_development_dependency "resque", "~> 1.20"
30
- gem.add_development_dependency "sidekiq", "~> 2.17"
31
- gem.add_development_dependency "delayed_job_active_record", "~> 0.3"
32
- gem.add_development_dependency "queue_classic", "~> 2.0"
33
- gem.add_development_dependency "backburner", "~> 0.4"
34
- gem.add_development_dependency "mocha", "~> 0.11"
35
- gem.add_development_dependency "torquebox-no-op", "~> 2.3"
36
- gem.add_development_dependency "sucker_punch", "~> 1.0.5"
37
- gem.add_development_dependency "que", "~> 0.8"
24
+ gem.add_development_dependency 'activerecord', '>= 5.0'
25
+ gem.add_development_dependency 'actionpack', '>= 5.0'
26
+ gem.add_development_dependency 'actionmailer', '>= 5.0'
27
+ gem.add_development_dependency 'rspec', '~> 3.6'
28
+ gem.add_development_dependency 'rspec-rails', '~> 3.6'
29
+ gem.add_development_dependency 'sqlite3', '~> 1.3'
30
+ gem.add_development_dependency 'pry'
38
31
  end
@@ -1,37 +1,9 @@
1
- require "devise"
2
- require "active_support/dependencies"
3
- require "devise/async/version"
1
+ require 'devise'
2
+ require 'devise/async/model'
3
+ require 'devise/async/version'
4
4
 
5
5
  module Devise
6
6
  module Async
7
- autoload :Worker, "devise/async/worker"
8
- autoload :Backend, "devise/async/backend"
9
- autoload :Model, "devise/async/model"
10
-
11
- module Backend
12
- autoload :Base, "devise/async/backend/base"
13
- autoload :Backburner, "devise/async/backend/backburner"
14
- autoload :Resque, "devise/async/backend/resque"
15
- autoload :Sidekiq, "devise/async/backend/sidekiq"
16
- autoload :DelayedJob, "devise/async/backend/delayed_job"
17
- autoload :QueueClassic, "devise/async/backend/queue_classic"
18
- autoload :Torquebox, "devise/async/backend/torquebox"
19
- autoload :SuckerPunch, "devise/async/backend/sucker_punch"
20
- autoload :Que, "devise/async/backend/que"
21
- end
22
-
23
- # Defines the queue backend to be used. Resque by default.
24
- mattr_accessor :backend
25
- @@backend = :resque
26
-
27
- # Defines the queue in which the background job will be enqueued. Default is :mailer.
28
- mattr_accessor :queue
29
- @@queue = :mailer
30
-
31
- # Defines the priority in which the background job will be enqueued. Defaults to the default of the backend you are using.
32
- mattr_accessor :priority
33
- @@priority = nil
34
-
35
7
  # Defines the enabled configuration that if set to false the emails will be sent synchronously
36
8
  mattr_accessor :enabled
37
9
  @@enabled = true
@@ -41,8 +13,7 @@ module Devise
41
13
  # Example:
42
14
  #
43
15
  # Devise::Async.setup do |config|
44
- # config.backend = :resque
45
- # config.queue = :my_custom_queue
16
+ # config.enabled = false
46
17
  # end
47
18
  def self.setup
48
19
  yield self
@@ -51,4 +22,4 @@ module Devise
51
22
  end
52
23
 
53
24
  # Register devise-async model in Devise
54
- Devise.add_module(:async, :model => 'devise/async/model')
25
+ Devise.add_module(:async, model: 'devise/async/model')
@@ -27,31 +27,20 @@ module Devise
27
27
  def send_devise_notification(notification, *args)
28
28
  return super unless Devise::Async.enabled
29
29
 
30
- # The current locale has to be remembered until the actual sending
31
- # of an email because it is scoped to the current thread. Hence,
32
- # using asynchronous mechanisms that use another thread to send an
33
- # email the currently used locale will be gone later.
34
- args = args_with_current_locale(args)
35
-
36
- # If the record is dirty we keep pending notifications to be enqueued
37
- # by the callback and avoid before commit job processing.
38
- if changed?
39
- devise_pending_notifications << [ notification, args ]
40
- # If the record isn't dirty (aka has already been saved) enqueue right away
41
- # because the callback has already been triggered.
30
+ if new_record? || changed?
31
+ devise_pending_notifications << [notification, args]
42
32
  else
43
- Devise::Async::Worker.enqueue(notification, self.class.name, self.id.to_s, *args)
33
+ deliver_mail_later(notification, self, args)
44
34
  end
45
35
  end
46
36
 
47
37
  # Send all pending notifications.
48
38
  def send_devise_pending_notifications
49
39
  devise_pending_notifications.each do |notification, args|
50
- # Use `id.to_s` to avoid problems with mongoid 2.4.X ids being serialized
51
- # wrong with YAJL.
52
- Devise::Async::Worker.enqueue(notification, self.class.name, self.id.to_s, *args)
40
+ deliver_mail_later(notification, self, args)
53
41
  end
54
- @devise_pending_notifications = []
42
+
43
+ @devise_pending_notifications.clear
55
44
  end
56
45
 
57
46
  def devise_pending_notifications
@@ -60,20 +49,9 @@ module Devise
60
49
 
61
50
  private
62
51
 
63
- def args_with_current_locale(args)
64
- # The default_locale is taken in any case. Hence, the args do not have
65
- # to be adapted if default_locale and current locale are equal.
66
- args = add_current_locale_to_args(args) if I18n.locale != I18n.default_locale
67
- args
52
+ def deliver_mail_later(notification, model, args)
53
+ devise_mailer.send(notification, model, *args).deliver_later
68
54
  end
69
-
70
- def add_current_locale_to_args(args)
71
- # Devise expects a hash as the last parameter for Mailer methods.
72
- opts = args.last.is_a?(Hash) ? args.pop : {}
73
- opts['locale'] = I18n.locale
74
- args.push(opts)
75
- end
76
-
77
55
  end
78
56
  end
79
57
  end
@@ -1,5 +1,5 @@
1
1
  module Devise
2
2
  module Async
3
- VERSION = "0.10.2"
3
+ VERSION = '1.0.0beta'
4
4
  end
5
5
  end
@@ -0,0 +1,95 @@
1
+ RSpec.describe Devise::Models::Async do
2
+ before :each do
3
+ ActiveJob::Base.queue_adapter = :test
4
+ end
5
+
6
+ context 'with unchanged model' do
7
+ subject { create_admin }
8
+
9
+ before :each do
10
+ subject
11
+ end
12
+
13
+ it 'enqueues notifications immediately when the model did not change' do
14
+ expect(ActionMailer::DeliveryJob).to have_been_enqueued
15
+ end
16
+
17
+ it 'forwards the correct data to the job' do
18
+ job_data = ActiveJob::Base.queue_adapter.enqueued_jobs.first[:args]
19
+ expected_job_data = ['Devise::Mailer', 'confirmation_instructions', subject.send(:confirmation_token)]
20
+
21
+ expect(job_data).to include(*expected_job_data)
22
+ end
23
+ end
24
+
25
+ context 'with changed model' do
26
+ subject do
27
+ admin[:username] = "changed_username"
28
+ admin.send_confirmation_instructions
29
+
30
+ admin.send(:devise_pending_notifications)
31
+ end
32
+
33
+ let!(:admin) { create_admin }
34
+
35
+ context 'without saving the model' do
36
+ it 'accumulates a pending notification to be sent after commit' do
37
+ expect(subject).to eq([
38
+ [:confirmation_instructions, [admin.send(:confirmation_token), {}]]
39
+ ])
40
+ end
41
+
42
+ it 'does not enqueue another job' do
43
+ expect {
44
+ subject
45
+ }.to_not have_enqueued_job(ActionMailer::DeliveryJob)
46
+ end
47
+ end
48
+
49
+ context 'with saving the model' do
50
+ let(:save_admin) { admin.save }
51
+
52
+ it 'accumulates a pending notification to be sent after commit' do
53
+ expect(subject).to eq([
54
+ [:confirmation_instructions, [admin.send(:confirmation_token), {}]]
55
+ ])
56
+ end
57
+
58
+ it 'does enqueue another job' do
59
+ subject
60
+
61
+ expect {
62
+ save_admin
63
+ }.to have_enqueued_job(ActionMailer::DeliveryJob)
64
+ end
65
+
66
+ it 'forwards the correct data to the job' do
67
+ subject
68
+ save_admin
69
+
70
+ job_data = ActiveJob::Base.queue_adapter.enqueued_jobs.first[:args]
71
+ expected_job_data = ['Devise::Mailer', 'confirmation_instructions', admin.send(:confirmation_token)]
72
+
73
+ expect(job_data).to include(*expected_job_data)
74
+ end
75
+ end
76
+ end
77
+
78
+ context 'when devise async is disabled' do
79
+ around :each do |example|
80
+ Devise::Async.enabled = false
81
+ example.run
82
+ Devise::Async.enabled = true
83
+ end
84
+
85
+ it 'does not enqueue a job' do
86
+ expect {
87
+ create_admin
88
+ }.to_not have_enqueued_job(ActionMailer::DeliveryJob)
89
+ end
90
+
91
+ it 'does not accumulate pending notifications' do
92
+ expect(create_admin.send(:devise_pending_notifications)).to be_empty
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,13 @@
1
+ RSpec.describe Devise::Async do
2
+ it 'yields self when setup is called' do
3
+ described_class.setup { |config| expect(config).to eq(Devise::Async) }
4
+ end
5
+
6
+ it 'stores enabled config' do
7
+ initial_enabled = described_class.enabled
8
+
9
+ described_class.enabled = false
10
+ expect(described_class.enabled).to eq(false)
11
+ described_class.enabled = initial_enabled
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+
3
+ require 'devise'
4
+ require 'devise/async'
5
+ require 'rails/all'
6
+
7
+ require 'spec_helper'
8
+ require 'rspec/rails'
9
+ require 'pry'
10
+
11
+ require 'support/rails_app'
12
+ require 'support/test_helpers'
13
+
14
+ RSpec.configure do |config|
15
+ config.infer_spec_type_from_file_location!
16
+ config.filter_rails_from_backtrace!
17
+
18
+ config.include TestHelpers
19
+
20
+ config.before :each do
21
+ load File.dirname(__FILE__) + '/support/rails_app/db/schema.rb'
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ RSpec.configure do |config|
2
+ config.expect_with :rspec do |expectations|
3
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
4
+ end
5
+
6
+ config.mock_with :rspec do |mocks|
7
+ mocks.verify_partial_doubles = true
8
+ end
9
+
10
+ config.shared_context_metadata_behavior = :apply_to_host_groups
11
+ config.filter_run_when_matching :focus
12
+ config.example_status_persistence_file_path = "spec/examples.txt"
13
+ config.disable_monkey_patching!
14
+ config.warnings = true
15
+ config.order = :random
16
+
17
+ if config.files_to_run.one?
18
+ config.default_formatter = 'doc'
19
+ end
20
+
21
+ Kernel.srand config.seed
22
+ end
@@ -4,10 +4,11 @@ ActiveRecord::Migration.verbose = false
4
4
  module Devise
5
5
  module Async
6
6
  class RailsApp < ::Rails::Application
7
- config.root = File.dirname(__FILE__) + "/rails_app"
7
+ config.root = File.dirname(__FILE__) + '/rails_app'
8
8
  config.active_support.deprecation = :log
9
- config.action_mailer.default_url_options = { :host => "localhost:3000" }
9
+ config.action_mailer.default_url_options = { host: 'localhost:3000' }
10
10
  config.action_mailer.delivery_method = :test
11
+ config.eager_load = false
11
12
  end
12
13
  end
13
14
  end
@@ -8,7 +8,6 @@ Devise.setup do |config|
8
8
 
9
9
  # Configure the class responsible to send e-mails.
10
10
  # config.mailer = "Devise::Mailer"
11
- # config.mailer = "Devise::Mailer"
12
11
 
13
12
  # ==> ORM configuration
14
13
  # Load and configure the ORM. Supports :active_record (default) and
@@ -1,3 +1,3 @@
1
1
  Rails.application.routes.draw do
2
- devise_for :users
2
+ devise_for :admins
3
3
  end