activity_notification 1.6.1 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.md +41 -3
  4. data/app/jobs/activity_notification/notify_all_job.rb +9 -0
  5. data/app/jobs/activity_notification/notify_job.rb +9 -0
  6. data/app/jobs/activity_notification/notify_to_job.rb +9 -0
  7. data/lib/activity_notification/apis/notification_api.rb +114 -18
  8. data/lib/activity_notification/config.rb +20 -0
  9. data/lib/activity_notification/models/concerns/notifiable.rb +63 -0
  10. data/lib/activity_notification/models/concerns/target.rb +23 -2
  11. data/lib/activity_notification/roles/acts_as_notifiable.rb +8 -0
  12. data/lib/activity_notification/version.rb +1 -1
  13. data/lib/generators/templates/activity_notification.rb +6 -0
  14. data/lib/generators/templates/migrations/migration.rb +5 -5
  15. data/spec/concerns/apis/notification_api_spec.rb +107 -0
  16. data/spec/concerns/models/notifiable_spec.rb +25 -4
  17. data/spec/concerns/models/subscriber_spec.rb +11 -11
  18. data/spec/concerns/models/target_spec.rb +9 -2
  19. data/spec/generators/migration/migration_generator_spec.rb +37 -4
  20. data/spec/jobs/notify_all_job_spec.rb +23 -0
  21. data/spec/jobs/notify_job_spec.rb +23 -0
  22. data/spec/jobs/notify_to_job_spec.rb +23 -0
  23. data/spec/rails_app/app/controllers/comments_controller.rb +4 -1
  24. data/spec/rails_app/app/models/dummy/dummy_base.rb +1 -0
  25. data/spec/rails_app/app/models/dummy/dummy_group.rb +1 -0
  26. data/spec/rails_app/app/models/dummy/dummy_notifiable.rb +1 -0
  27. data/spec/rails_app/app/models/dummy/dummy_notifiable_target.rb +1 -0
  28. data/spec/rails_app/app/models/dummy/dummy_notifier.rb +1 -0
  29. data/spec/rails_app/app/models/dummy/dummy_subscriber.rb +1 -0
  30. data/spec/rails_app/app/models/dummy/dummy_target.rb +1 -0
  31. data/spec/rails_app/config/initializers/activity_notification.rb +6 -0
  32. data/spec/rails_app/db/migrate/{20160715050433_create_test_tables.rb → 20160716000000_create_test_tables.rb} +1 -1
  33. data/spec/rails_app/db/migrate/{20160715050420_create_activity_notification_tables.rb → 20181209000000_create_activity_notification_tables.rb} +3 -3
  34. data/spec/rails_app/db/schema.rb +43 -43
  35. data/spec/roles/acts_as_notifiable_spec.rb +45 -0
  36. metadata +15 -6
@@ -0,0 +1,23 @@
1
+ describe ActivityNotification::NotifyAllJob, type: :job do
2
+ before do
3
+ ActiveJob::Base.queue_adapter = :test
4
+ ActiveJob::Base.queue_adapter.enqueued_jobs.clear
5
+ @author_user = create(:confirmed_user)
6
+ @user = create(:confirmed_user)
7
+ @article = create(:article, user: @author_user)
8
+ @comment = create(:comment, article: @article, user: @user)
9
+ end
10
+
11
+ describe "#perform_later" do
12
+ it "generates notifications" do
13
+ expect {
14
+ ActivityNotification::NotifyAllJob.perform_later([@author_user, @user], @comment)
15
+ }.to have_enqueued_job
16
+ end
17
+
18
+ it "generates notifications once" do
19
+ ActivityNotification::NotifyAllJob.perform_later([@author_user, @user], @comment)
20
+ expect(ActivityNotification::NotifyAllJob).to have_been_enqueued.exactly(:once)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ describe ActivityNotification::NotifyJob, type: :job do
2
+ before do
3
+ ActiveJob::Base.queue_adapter = :test
4
+ ActiveJob::Base.queue_adapter.enqueued_jobs.clear
5
+ @author_user = create(:confirmed_user)
6
+ @user = create(:confirmed_user)
7
+ @article = create(:article, user: @author_user)
8
+ @comment = create(:comment, article: @article, user: @user)
9
+ end
10
+
11
+ describe "#perform_later" do
12
+ it "generates notifications" do
13
+ expect {
14
+ ActivityNotification::NotifyJob.perform_later('users', @comment)
15
+ }.to have_enqueued_job
16
+ end
17
+
18
+ it "generates notifications once" do
19
+ ActivityNotification::NotifyJob.perform_later('users', @comment)
20
+ expect(ActivityNotification::NotifyJob).to have_been_enqueued.exactly(:once)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ describe ActivityNotification::NotifyToJob, type: :job do
2
+ before do
3
+ ActiveJob::Base.queue_adapter = :test
4
+ ActiveJob::Base.queue_adapter.enqueued_jobs.clear
5
+ @author_user = create(:confirmed_user)
6
+ @user = create(:confirmed_user)
7
+ @article = create(:article, user: @author_user)
8
+ @comment = create(:comment, article: @article, user: @user)
9
+ end
10
+
11
+ describe "#perform_later" do
12
+ it "generates notification" do
13
+ expect {
14
+ ActivityNotification::NotifyToJob.perform_later(@user, @comment)
15
+ }.to have_enqueued_job
16
+ end
17
+
18
+ it "generates notification once" do
19
+ ActivityNotification::NotifyToJob.perform_later(@user, @comment)
20
+ expect(ActivityNotification::NotifyToJob).to have_been_enqueued.exactly(:once)
21
+ end
22
+ end
23
+ end
@@ -7,7 +7,10 @@ class CommentsController < ApplicationController
7
7
  @comment.user = current_user
8
8
 
9
9
  if @comment.save
10
- @comment.notify :users
10
+ @comment.notify_later :users, key: 'comment.create.later'
11
+ # @comment.notify :users, key: 'comment.create.later', notify_later: true
12
+ @comment.notify_now :users, key: 'comment.create.now'
13
+ # @comment.notify :users, key: 'comment.create.now'
11
14
  redirect_to @comment.article, notice: 'Comment was successfully created.'
12
15
  else
13
16
  redirect_to @comment.article
@@ -5,6 +5,7 @@ else
5
5
  class Dummy::DummyBase
6
6
  include Mongoid::Document
7
7
  include Mongoid::Timestamps
8
+ include GlobalID::Identification
8
9
  include ActivityNotification::Models
9
10
  end
10
11
  end
@@ -7,6 +7,7 @@ else
7
7
  class Dummy::DummyGroup
8
8
  include Mongoid::Document
9
9
  include Mongoid::Timestamps
10
+ include GlobalID::Identification
10
11
  include ActivityNotification::Models
11
12
  include ActivityNotification::Group
12
13
  field :title, type: String
@@ -7,6 +7,7 @@ else
7
7
  class Dummy::DummyNotifiable
8
8
  include Mongoid::Document
9
9
  include Mongoid::Timestamps
10
+ include GlobalID::Identification
10
11
  include ActivityNotification::Models
11
12
  include ActivityNotification::Notifiable
12
13
  field :title, type: String
@@ -8,6 +8,7 @@ else
8
8
  class Dummy::DummyNotifiableTarget
9
9
  include Mongoid::Document
10
10
  include Mongoid::Timestamps
11
+ include GlobalID::Identification
11
12
  field :email, type: String, default: ""
12
13
  field :name, type: String
13
14
 
@@ -7,6 +7,7 @@ else
7
7
  class Dummy::DummyNotifier
8
8
  include Mongoid::Document
9
9
  include Mongoid::Timestamps
10
+ include GlobalID::Identification
10
11
  include ActivityNotification::Models
11
12
  include ActivityNotification::Notifier
12
13
  field :name, type: String
@@ -7,6 +7,7 @@ else
7
7
  class Dummy::DummySubscriber
8
8
  include Mongoid::Document
9
9
  include Mongoid::Timestamps
10
+ include GlobalID::Identification
10
11
  include ActivityNotification::Models
11
12
  acts_as_target email: 'dummy@example.com', email_allowed: true, batch_email_allowed: true, subscription_allowed: true
12
13
  end
@@ -7,6 +7,7 @@ else
7
7
  class Dummy::DummyTarget
8
8
  include Mongoid::Document
9
9
  include Mongoid::Timestamps
10
+ include GlobalID::Identification
10
11
  include ActivityNotification::Models
11
12
  include ActivityNotification::Target
12
13
  field :email, type: String, default: ""
@@ -41,6 +41,9 @@ ActivityNotification.configure do |config|
41
41
  # Configure the parent class responsible to send e-mails.
42
42
  # config.parent_mailer = 'ActionMailer::Base'
43
43
 
44
+ # Configure the parent job class for delayed notifications.
45
+ # config.parent_job = 'ActiveJob::Base'
46
+
44
47
  # Configure the parent class for activity_notification controllers.
45
48
  # config.parent_controller = 'ApplicationController'
46
49
 
@@ -50,4 +53,7 @@ ActivityNotification.configure do |config|
50
53
  # Configure default limit number of opened notifications you can get from opened* scope
51
54
  config.opened_index_limit = 10
52
55
 
56
+ # Configure ActiveJob queue name for delayed notifications.
57
+ config.active_job_queue = :activity_notification
58
+
53
59
  end
@@ -1,4 +1,4 @@
1
- class CreateTestTables < ActiveRecord::Migration[5.1]
1
+ class CreateTestTables < ActiveRecord::Migration[5.2]
2
2
  def change
3
3
  create_table :users do |t|
4
4
  # Devise
@@ -1,5 +1,5 @@
1
1
  # Migration responsible for creating a table with notifications
2
- class CreateActivityNotificationTables < ActiveRecord::Migration[5.1]
2
+ class CreateActivityNotificationTables < ActiveRecord::Migration[5.2]
3
3
  # Create tables
4
4
  def change
5
5
  create_table :notifications do |t|
@@ -12,7 +12,7 @@ class CreateActivityNotificationTables < ActiveRecord::Migration[5.1]
12
12
  t.text :parameters
13
13
  t.datetime :opened_at
14
14
 
15
- t.timestamps
15
+ t.timestamps null: false
16
16
  end
17
17
 
18
18
  create_table :subscriptions do |t|
@@ -26,7 +26,7 @@ class CreateActivityNotificationTables < ActiveRecord::Migration[5.1]
26
26
  t.datetime :unsubscribed_to_email_at
27
27
  t.text :optional_targets
28
28
 
29
- t.timestamps
29
+ t.timestamps null: false
30
30
  end
31
31
  add_index :subscriptions, [:target_type, :target_id, :key], unique: true
32
32
  end
@@ -10,51 +10,51 @@
10
10
  #
11
11
  # It's strongly recommended that you check this file into your version control system.
12
12
 
13
- ActiveRecord::Schema.define(version: 20160715050433) do
13
+ ActiveRecord::Schema.define(version: 2018_12_09_000000) do
14
14
 
15
15
  create_table "admins", force: :cascade do |t|
16
- t.integer "user_id"
17
- t.string "phone_number"
18
- t.string "slack_username"
19
- t.datetime "created_at"
20
- t.datetime "updated_at"
16
+ t.integer "user_id"
17
+ t.string "phone_number"
18
+ t.string "slack_username"
19
+ t.datetime "created_at", null: false
20
+ t.datetime "updated_at", null: false
21
21
  t.index ["user_id"], name: "index_admins_on_user_id"
22
22
  end
23
23
 
24
24
  create_table "articles", force: :cascade do |t|
25
- t.integer "user_id"
26
- t.string "title"
27
- t.string "body"
28
- t.datetime "created_at"
29
- t.datetime "updated_at"
25
+ t.integer "user_id"
26
+ t.string "title"
27
+ t.string "body"
28
+ t.datetime "created_at", null: false
29
+ t.datetime "updated_at", null: false
30
30
  t.index ["user_id"], name: "index_articles_on_user_id"
31
31
  end
32
32
 
33
33
  create_table "comments", force: :cascade do |t|
34
- t.integer "user_id"
35
- t.integer "article_id"
36
- t.string "body"
37
- t.datetime "created_at"
38
- t.datetime "updated_at"
34
+ t.integer "user_id"
35
+ t.integer "article_id"
36
+ t.string "body"
37
+ t.datetime "created_at", null: false
38
+ t.datetime "updated_at", null: false
39
39
  t.index ["article_id"], name: "index_comments_on_article_id"
40
40
  t.index ["user_id"], name: "index_comments_on_user_id"
41
41
  end
42
42
 
43
43
  create_table "notifications", force: :cascade do |t|
44
- t.string "target_type", null: false
45
- t.integer "target_id", null: false
46
- t.string "notifiable_type", null: false
47
- t.integer "notifiable_id", null: false
48
- t.string "key", null: false
49
- t.string "group_type"
50
- t.integer "group_id"
51
- t.integer "group_owner_id"
52
- t.string "notifier_type"
53
- t.integer "notifier_id"
54
- t.text "parameters"
44
+ t.string "target_type", null: false
45
+ t.integer "target_id", null: false
46
+ t.string "notifiable_type", null: false
47
+ t.integer "notifiable_id", null: false
48
+ t.string "key", null: false
49
+ t.string "group_type"
50
+ t.integer "group_id"
51
+ t.integer "group_owner_id"
52
+ t.string "notifier_type"
53
+ t.integer "notifier_id"
54
+ t.text "parameters"
55
55
  t.datetime "opened_at"
56
- t.datetime "created_at"
57
- t.datetime "updated_at"
56
+ t.datetime "created_at", null: false
57
+ t.datetime "updated_at", null: false
58
58
  t.index ["group_owner_id"], name: "index_notifications_on_group_owner_id"
59
59
  t.index ["group_type", "group_id"], name: "index_notifications_on_group_type_and_group_id"
60
60
  t.index ["notifiable_type", "notifiable_id"], name: "index_notifications_on_notifiable_type_and_notifiable_id"
@@ -63,32 +63,32 @@ ActiveRecord::Schema.define(version: 20160715050433) do
63
63
  end
64
64
 
65
65
  create_table "subscriptions", force: :cascade do |t|
66
- t.string "target_type", null: false
67
- t.integer "target_id", null: false
68
- t.string "key", null: false
69
- t.boolean "subscribing", default: true, null: false
70
- t.boolean "subscribing_to_email", default: true, null: false
66
+ t.string "target_type", null: false
67
+ t.integer "target_id", null: false
68
+ t.string "key", null: false
69
+ t.boolean "subscribing", default: true, null: false
70
+ t.boolean "subscribing_to_email", default: true, null: false
71
71
  t.datetime "subscribed_at"
72
72
  t.datetime "unsubscribed_at"
73
73
  t.datetime "subscribed_to_email_at"
74
74
  t.datetime "unsubscribed_to_email_at"
75
- t.text "optional_targets"
76
- t.datetime "created_at"
77
- t.datetime "updated_at"
75
+ t.text "optional_targets"
76
+ t.datetime "created_at", null: false
77
+ t.datetime "updated_at", null: false
78
78
  t.index ["key"], name: "index_subscriptions_on_key"
79
79
  t.index ["target_type", "target_id", "key"], name: "index_subscriptions_on_target_type_and_target_id_and_key", unique: true
80
80
  t.index ["target_type", "target_id"], name: "index_subscriptions_on_target_type_and_target_id"
81
81
  end
82
82
 
83
83
  create_table "users", force: :cascade do |t|
84
- t.string "email", default: "", null: false
85
- t.string "encrypted_password", default: "", null: false
86
- t.string "confirmation_token"
84
+ t.string "email", default: "", null: false
85
+ t.string "encrypted_password", default: "", null: false
86
+ t.string "confirmation_token"
87
87
  t.datetime "confirmed_at"
88
88
  t.datetime "confirmation_sent_at"
89
- t.string "name"
90
- t.datetime "created_at"
91
- t.datetime "updated_at"
89
+ t.string "name"
90
+ t.datetime "created_at", null: false
91
+ t.datetime "updated_at", null: false
92
92
  t.index ["email"], name: "index_users_on_email"
93
93
  end
94
94
 
@@ -1,4 +1,5 @@
1
1
  describe ActivityNotification::ActsAsNotifiable do
2
+ include ActiveJob::TestHelper
2
3
  let(:dummy_model_class) { Dummy::DummyBase }
3
4
  let(:dummy_notifiable_class) { Dummy::DummyNotifiable }
4
5
  let(:user_target) { create(:confirmed_user) }
@@ -193,6 +194,50 @@ describe ActivityNotification::ActsAsNotifiable do
193
194
  end
194
195
  end
195
196
  end
197
+
198
+ context "with :notify_later option" do
199
+ before do
200
+ ActiveJob::Base.queue_adapter = :test
201
+ dummy_notifiable_class.acts_as_notifiable :users, targets: [user_target], tracked: { notify_later: true }
202
+ @created_notifiable = dummy_notifiable_class.create
203
+ end
204
+
205
+ context "creation" do
206
+ it "generates notifications later when notifiable is created" do
207
+ expect {
208
+ @created_notifiable = dummy_notifiable_class.create
209
+ }.to have_enqueued_job(ActivityNotification::NotifyJob)
210
+ end
211
+
212
+ it "creates notification records later when notifiable is created" do
213
+ perform_enqueued_jobs do
214
+ @created_notifiable = dummy_notifiable_class.create
215
+ end
216
+ expect(user_target.notifications.filtered_by_instance(@created_notifiable).count).to eq(1)
217
+ end
218
+ end
219
+
220
+ context "update" do
221
+ before do
222
+ user_target.notifications.delete_all
223
+ expect(user_target.notifications.count).to eq(0)
224
+ @notifiable.update(created_at: @notifiable.updated_at)
225
+ end
226
+
227
+ it "generates notifications later when notifiable is created" do
228
+ expect {
229
+ @notifiable.update(created_at: @notifiable.updated_at)
230
+ }.to have_enqueued_job(ActivityNotification::NotifyJob)
231
+ end
232
+
233
+ it "creates notification records later when notifiable is created" do
234
+ perform_enqueued_jobs do
235
+ @notifiable.update(created_at: @notifiable.updated_at)
236
+ end
237
+ expect(user_target.notifications.filtered_by_instance(@notifiable).count).to eq(1)
238
+ end
239
+ end
240
+ end
196
241
  end
197
242
 
198
243
  context "with :dependent_notifications option" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activity_notification
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shota Yamazaki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-19 00:00:00.000000000 Z
11
+ date: 2018-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -258,6 +258,9 @@ files:
258
258
  - app/controllers/activity_notification/notifications_with_devise_controller.rb
259
259
  - app/controllers/activity_notification/subscriptions_controller.rb
260
260
  - app/controllers/activity_notification/subscriptions_with_devise_controller.rb
261
+ - app/jobs/activity_notification/notify_all_job.rb
262
+ - app/jobs/activity_notification/notify_job.rb
263
+ - app/jobs/activity_notification/notify_to_job.rb
261
264
  - app/mailers/activity_notification/mailer.rb
262
265
  - app/views/activity_notification/mailer/default/batch_default.html.erb
263
266
  - app/views/activity_notification/mailer/default/batch_default.text.erb
@@ -383,6 +386,9 @@ files:
383
386
  - spec/generators/views_generator_spec.rb
384
387
  - spec/helpers/polymorphic_helpers_spec.rb
385
388
  - spec/helpers/view_helpers_spec.rb
389
+ - spec/jobs/notify_all_job_spec.rb
390
+ - spec/jobs/notify_job_spec.rb
391
+ - spec/jobs/notify_to_job_spec.rb
386
392
  - spec/mailers/mailer_spec.rb
387
393
  - spec/models/dummy/dummy_group_spec.rb
388
394
  - spec/models/dummy/dummy_notifiable_spec.rb
@@ -463,8 +469,8 @@ files:
463
469
  - spec/rails_app/config/mongoid.yml
464
470
  - spec/rails_app/config/routes.rb
465
471
  - spec/rails_app/config/secrets.yml
466
- - spec/rails_app/db/migrate/20160715050420_create_activity_notification_tables.rb
467
- - spec/rails_app/db/migrate/20160715050433_create_test_tables.rb
472
+ - spec/rails_app/db/migrate/20160716000000_create_test_tables.rb
473
+ - spec/rails_app/db/migrate/20181209000000_create_activity_notification_tables.rb
468
474
  - spec/rails_app/db/schema.rb
469
475
  - spec/rails_app/db/seeds.rb
470
476
  - spec/rails_app/lib/custom_optional_targets/console_output.rb
@@ -540,6 +546,9 @@ test_files:
540
546
  - spec/generators/views_generator_spec.rb
541
547
  - spec/helpers/polymorphic_helpers_spec.rb
542
548
  - spec/helpers/view_helpers_spec.rb
549
+ - spec/jobs/notify_all_job_spec.rb
550
+ - spec/jobs/notify_job_spec.rb
551
+ - spec/jobs/notify_to_job_spec.rb
543
552
  - spec/mailers/mailer_spec.rb
544
553
  - spec/models/dummy/dummy_group_spec.rb
545
554
  - spec/models/dummy/dummy_notifiable_spec.rb
@@ -620,8 +629,8 @@ test_files:
620
629
  - spec/rails_app/config/mongoid.yml
621
630
  - spec/rails_app/config/routes.rb
622
631
  - spec/rails_app/config/secrets.yml
623
- - spec/rails_app/db/migrate/20160715050420_create_activity_notification_tables.rb
624
- - spec/rails_app/db/migrate/20160715050433_create_test_tables.rb
632
+ - spec/rails_app/db/migrate/20160716000000_create_test_tables.rb
633
+ - spec/rails_app/db/migrate/20181209000000_create_activity_notification_tables.rb
625
634
  - spec/rails_app/db/schema.rb
626
635
  - spec/rails_app/db/seeds.rb
627
636
  - spec/rails_app/lib/custom_optional_targets/console_output.rb