activity_notification 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +41 -5
  3. data/CHANGELOG.md +20 -0
  4. data/Gemfile +4 -1
  5. data/Gemfile.lock +81 -70
  6. data/README.md +152 -12
  7. data/activity_notification.gemspec +6 -4
  8. data/gemfiles/Gemfile.rails-4.2.lock +27 -22
  9. data/gemfiles/Gemfile.rails-5.0.lock +26 -21
  10. data/gemfiles/Gemfile.rails-5.1 +15 -0
  11. data/gemfiles/Gemfile.rails-5.1.lock +229 -0
  12. data/lib/activity_notification/apis/notification_api.rb +2 -2
  13. data/lib/activity_notification/apis/subscription_api.rb +7 -7
  14. data/lib/activity_notification/controllers/common_controller.rb +2 -2
  15. data/lib/activity_notification/helpers/view_helpers.rb +1 -1
  16. data/lib/activity_notification/models/concerns/notifiable.rb +19 -1
  17. data/lib/activity_notification/models/concerns/target.rb +5 -3
  18. data/lib/activity_notification/orm/active_record/notification.rb +3 -1
  19. data/lib/activity_notification/orm/active_record/subscription.rb +3 -1
  20. data/lib/activity_notification/orm/mongoid.rb +2 -2
  21. data/lib/activity_notification/renderable.rb +1 -1
  22. data/lib/activity_notification/roles/acts_as_notifiable.rb +98 -17
  23. data/lib/activity_notification/version.rb +1 -1
  24. data/spec/concerns/apis/notification_api_spec.rb +9 -8
  25. data/spec/concerns/apis/subscription_api_spec.rb +28 -28
  26. data/spec/concerns/models/notifier_spec.rb +1 -1
  27. data/spec/concerns/models/subscriber_spec.rb +7 -7
  28. data/spec/concerns/models/target_spec.rb +20 -20
  29. data/spec/controllers/notifications_controller_shared_examples.rb +19 -8
  30. data/spec/controllers/subscriptions_controller_shared_examples.rb +16 -5
  31. data/spec/models/notification_spec.rb +4 -4
  32. data/spec/models/subscription_spec.rb +16 -12
  33. data/spec/rails_app/app/controllers/articles_controller.rb +1 -1
  34. data/spec/rails_app/app/controllers/comments_controller.rb +0 -1
  35. data/spec/rails_app/app/models/admin.rb +29 -8
  36. data/spec/rails_app/app/models/article.rb +45 -13
  37. data/spec/rails_app/app/models/comment.rb +107 -42
  38. data/spec/rails_app/app/models/user.rb +45 -12
  39. data/spec/rails_app/app/views/layouts/_header.html.erb +1 -0
  40. data/spec/rails_app/config/database.yml +26 -15
  41. data/spec/rails_app/config/initializers/devise.rb +5 -1
  42. data/spec/rails_app/db/migrate/20160715050420_create_activity_notification_tables.rb +1 -1
  43. data/spec/rails_app/db/migrate/20160715050433_create_test_tables.rb +1 -1
  44. data/spec/rails_app/db/seeds.rb +31 -8
  45. data/spec/roles/acts_as_notifiable_spec.rb +154 -1
  46. data/spec/spec_helper.rb +7 -2
  47. metadata +47 -17
@@ -5,7 +5,7 @@ shared_examples_for :notifier do
5
5
  describe "with association" do
6
6
  it "has many sent_notifications" do
7
7
  notification_1 = create(:notification, notifier: test_instance)
8
- notification_2 = create(:notification, notifier: test_instance)
8
+ notification_2 = create(:notification, notifier: test_instance, created_at: notification_1.created_at + 10.second)
9
9
  expect(test_instance.sent_notifications.count).to eq(2)
10
10
  expect(test_instance.sent_notifications.earliest).to eq(notification_1)
11
11
  expect(test_instance.sent_notifications.latest).to eq(notification_2)
@@ -11,11 +11,11 @@ shared_examples_for :subscriber do
11
11
  describe "with association" do
12
12
  it "has many subscriptions" do
13
13
  subscription_1 = create(:subscription, target: test_instance, key: 'subscription_key_1')
14
- subscription_2 = create(:subscription, target: test_instance, key: 'subscription_key_2')
14
+ subscription_2 = create(:subscription, target: test_instance, key: 'subscription_key_2', created_at: subscription_1.created_at + 10.second)
15
15
  expect(test_instance.subscriptions.count).to eq(2)
16
16
  expect(test_instance.subscriptions.earliest_order.first).to eq(subscription_1)
17
17
  expect(test_instance.subscriptions.latest_order.first).to eq(subscription_2)
18
- expect(test_instance.subscriptions).to eq(ActivityNotification::Subscription.filtered_by_target(test_instance))
18
+ expect(test_instance.subscriptions.latest_order).to eq(ActivityNotification::Subscription.filtered_by_target(test_instance).latest_order)
19
19
  end
20
20
  end
21
21
 
@@ -182,7 +182,7 @@ shared_examples_for :subscriber do
182
182
  context "when the target has subscriptions" do
183
183
  before do
184
184
  @subscription2 = create(:subscription, target: test_instance, key: 'subscription_key_2')
185
- @subscription1 = create(:subscription, target: test_instance, key: 'subscription_key_1')
185
+ @subscription1 = create(:subscription, target: test_instance, key: 'subscription_key_1', created_at: @subscription2.created_at + 10.second)
186
186
  end
187
187
 
188
188
  context "without any options" do
@@ -221,7 +221,7 @@ shared_examples_for :subscriber do
221
221
  context 'with custom_filter options' do
222
222
  it "returns filtered notifications only" do
223
223
  if ActivityNotification.config.orm == :active_record
224
- options = { custom_filter: ["key = ?", 'subscription_key_2'] }
224
+ options = { custom_filter: ["subscriptions.key = ?", 'subscription_key_2'] }
225
225
  expect(test_instance.subscription_index(options)[0]).to eq(@subscription2)
226
226
  expect(test_instance.subscription_index(options).size).to eq(1)
227
227
  end
@@ -258,8 +258,8 @@ shared_examples_for :subscriber do
258
258
 
259
259
  context "when the target has notifications" do
260
260
  before do
261
- create(:notification, target: test_instance, key: 'notification_key_2')
262
- create(:notification, target: test_instance, key: 'notification_key_1')
261
+ notification = create(:notification, target: test_instance, key: 'notification_key_2')
262
+ create(:notification, target: test_instance, key: 'notification_key_1', created_at: notification.created_at + 10.second)
263
263
  create(:subscription, target: test_instance, key: 'notification_key_1')
264
264
  end
265
265
 
@@ -323,7 +323,7 @@ shared_examples_for :subscriber do
323
323
  context 'with custom_filter options' do
324
324
  it "returns filtered notifications only" do
325
325
  if ActivityNotification.config.orm == :active_record
326
- options = { custom_filter: ["key = ?", 'notification_key_2'] }
326
+ options = { custom_filter: ["notifications.key = ?", 'notification_key_2'] }
327
327
  expect(test_instance.notification_keys(options)[0]).to eq('notification_key_2')
328
328
  expect(test_instance.notification_keys(options).size).to eq(1)
329
329
  end
@@ -6,7 +6,7 @@ shared_examples_for :target do
6
6
  describe "with association" do
7
7
  it "has many notifications" do
8
8
  notification_1 = create(:notification, target: test_instance)
9
- notification_2 = create(:notification, target: test_instance)
9
+ notification_2 = create(:notification, target: test_instance, created_at: notification_1.created_at + 10.second)
10
10
  expect(test_instance.notifications.count).to eq(2)
11
11
  expect(test_instance.notifications.earliest).to eq(notification_1)
12
12
  expect(test_instance.notifications.latest).to eq(notification_2)
@@ -108,15 +108,15 @@ shared_examples_for :target do
108
108
  @target_2 = create(test_class_name)
109
109
  @target_3 = create(test_class_name)
110
110
  notification_1 = create(:notification, target: @target_1)
111
- @notification_2 = create(:notification, target: @target_1)
112
- notification_3 = create(:notification, target: @target_1, group_owner: @notification_2)
113
- @notification_4 = create(:notification, target: @target_1, group_owner: @notification_2)
114
- notification_5 = create(:notification, target: @target_2)
115
- notification_6 = create(:notification, target: @target_2)
111
+ @notification_2 = create(:notification, target: @target_1, created_at: notification_1.created_at + 10.second)
112
+ notification_3 = create(:notification, target: @target_1, group_owner: @notification_2, created_at: notification_1.created_at + 20.second)
113
+ @notification_4 = create(:notification, target: @target_1, group_owner: @notification_2, created_at: notification_1.created_at + 30.second)
114
+ notification_5 = create(:notification, target: @target_2, created_at: notification_1.created_at + 40.second)
115
+ notification_6 = create(:notification, target: @target_2, created_at: notification_1.created_at + 50.second)
116
116
  notification_6.open!
117
- notification_7 = create(:notification, target: @target_3)
117
+ notification_7 = create(:notification, target: @target_3, created_at: notification_1.created_at + 60.second)
118
118
  notification_7.open!
119
- notification_8 = create(:notification, target: test_notifiable)
119
+ notification_8 = create(:notification, target: test_notifiable, created_at: notification_1.created_at + 70.second)
120
120
  end
121
121
 
122
122
  context "as default" do
@@ -517,9 +517,9 @@ shared_examples_for :target do
517
517
  @group = create(:article)
518
518
  @key = 'test.key.1'
519
519
  @notification2 = create(:notification, target: test_instance, notifiable: @notifiable)
520
- @notification1 = create(:notification, target: test_instance, notifiable: create(:comment), group: @group)
521
- @member1 = create(:notification, target: test_instance, notifiable: create(:comment), group_owner: @notification1)
522
- @notification3 = create(:notification, target: test_instance, notifiable: create(:article), key: @key)
520
+ @notification1 = create(:notification, target: test_instance, notifiable: create(:comment), group: @group, created_at: @notification2.created_at + 10.second)
521
+ @member1 = create(:notification, target: test_instance, notifiable: create(:comment), group_owner: @notification1, created_at: @notification2.created_at + 20.second)
522
+ @notification3 = create(:notification, target: test_instance, notifiable: create(:article), key: @key, created_at: @notification2.created_at + 30.second)
523
523
  @notification3.open!
524
524
  end
525
525
 
@@ -615,7 +615,7 @@ shared_examples_for :target do
615
615
  context 'with custom_filter options' do
616
616
  it "returns filtered notifications only" do
617
617
  if ActivityNotification.config.orm == :active_record
618
- options = { custom_filter: ["key = ?", @key] }
618
+ options = { custom_filter: ["notifications.key = ?", @key] }
619
619
  expect(test_instance.notification_index(options)[0]).to eq(@notification3)
620
620
  expect(test_instance.notification_index(options).size).to eq(1)
621
621
  end
@@ -629,8 +629,8 @@ shared_examples_for :target do
629
629
 
630
630
  context "when the target has no unopened notifications" do
631
631
  before do
632
- create(:notification, target: test_instance, opened_at: Time.current)
633
- create(:notification, target: test_instance, opened_at: Time.current)
632
+ notification = create(:notification, target: test_instance, opened_at: Time.current)
633
+ create(:notification, target: test_instance, opened_at: Time.current, created_at: notification.created_at + 10.second)
634
634
  end
635
635
 
636
636
  it "calls unopened_notification_index" do
@@ -665,7 +665,7 @@ shared_examples_for :target do
665
665
  context "when the target has unopened notifications" do
666
666
  before do
667
667
  @notification_1 = create(:notification, target: test_instance)
668
- @notification_2 = create(:notification, target: test_instance)
668
+ @notification_2 = create(:notification, target: test_instance, created_at: @notification_1.created_at + 10.second)
669
669
  end
670
670
 
671
671
  context "without limit" do
@@ -721,7 +721,7 @@ shared_examples_for :target do
721
721
  context "when the target has opened notifications" do
722
722
  before do
723
723
  @notification_1 = create(:notification, target: test_instance, opened_at: Time.current)
724
- @notification_2 = create(:notification, target: test_instance, opened_at: Time.current)
724
+ @notification_2 = create(:notification, target: test_instance, opened_at: Time.current, created_at: @notification_1.created_at + 10.second)
725
725
  end
726
726
 
727
727
  context "without limit" do
@@ -808,8 +808,8 @@ shared_examples_for :target do
808
808
  @group = create(:article)
809
809
  @key = 'test.key.1'
810
810
  @notification2 = create(:notification, target: test_instance, notifiable: @notifiable)
811
- @notification1 = create(:notification, target: test_instance, notifiable: create(:comment), group: @group)
812
- @notification3 = create(:notification, target: test_instance, notifiable: create(:article), key: @key)
811
+ @notification1 = create(:notification, target: test_instance, notifiable: create(:comment), group: @group, created_at: @notification2.created_at + 10.second)
812
+ @notification3 = create(:notification, target: test_instance, notifiable: create(:article), key: @key, created_at: @notification2.created_at + 20.second)
813
813
  @notification3.open!
814
814
  end
815
815
 
@@ -884,8 +884,8 @@ shared_examples_for :target do
884
884
 
885
885
  context "when the target has no unopened notifications" do
886
886
  before do
887
- create(:notification, target: test_instance, opened_at: Time.current)
888
- create(:notification, target: test_instance, opened_at: Time.current)
887
+ notification = create(:notification, target: test_instance, opened_at: Time.current)
888
+ create(:notification, target: test_instance, opened_at: Time.current, created_at: notification.created_at + 10.second)
889
889
  end
890
890
 
891
891
  it "calls unopened_notification_index_with_attributes" do
@@ -57,9 +57,15 @@ shared_examples_for :notification_controller do
57
57
  end
58
58
 
59
59
  it "raises ActiveRecord::RecordNotFound" do
60
- expect {
61
- get_with_compatibility :index, target_params.merge({ typed_target_param => 0 }), valid_session
62
- }.to raise_error(ActiveRecord::RecordNotFound)
60
+ if ENV['AN_TEST_DB'] == 'mongodb'
61
+ expect {
62
+ get_with_compatibility :index, target_params.merge({ typed_target_param => 0 }), valid_session
63
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
64
+ else
65
+ expect {
66
+ get_with_compatibility :index, target_params.merge({ typed_target_param => 0 }), valid_session
67
+ }.to raise_error(ActiveRecord::RecordNotFound)
68
+ end
63
69
  end
64
70
  end
65
71
 
@@ -74,8 +80,13 @@ shared_examples_for :notification_controller do
74
80
  end
75
81
 
76
82
  it "returns json format" do
77
- expect(JSON.parse(response.body).first)
78
- .to include("target_id" => test_target.id, "target_type" => test_target.to_class_name)
83
+ if ActivityNotification.config.orm == :active_record
84
+ expect(JSON.parse(response.body).first)
85
+ .to include("target_id" => test_target.id, "target_type" => test_target.to_class_name)
86
+ else
87
+ expect(JSON.parse(response.body).first)
88
+ .to include("target_id" => test_target.id.to_s, "target_type" => test_target.to_class_name)
89
+ end
79
90
  end
80
91
  end
81
92
 
@@ -155,9 +166,9 @@ shared_examples_for :notification_controller do
155
166
  @notifiable = create(:article)
156
167
  @group = create(:article)
157
168
  @key = 'test.key.1'
158
- create(:notification, target: test_target, notifiable: @notifiable)
159
- create(:notification, target: test_target, notifiable: create(:comment), group: @group)
160
- create(:notification, target: test_target, notifiable: create(:article), key: @key).open!
169
+ notification = create(:notification, target: test_target, notifiable: @notifiable)
170
+ create(:notification, target: test_target, notifiable: create(:comment), group: @group, created_at: notification.created_at + 10.second)
171
+ create(:notification, target: test_target, notifiable: create(:article), key: @key, created_at: notification.created_at + 20.second).open!
161
172
  @notification1 = test_target.notification_index[0]
162
173
  @notification2 = test_target.notification_index[1]
163
174
  @notification3 = test_target.notification_index[2]
@@ -69,9 +69,15 @@ shared_examples_for :subscription_controller do
69
69
  end
70
70
 
71
71
  it "raises ActiveRecord::RecordNotFound" do
72
- expect {
73
- get_with_compatibility :index, target_params.merge({ typed_target_param => 0 }), valid_session
74
- }.to raise_error(ActiveRecord::RecordNotFound)
72
+ if ENV['AN_TEST_DB'] == 'mongodb'
73
+ expect {
74
+ get_with_compatibility :index, target_params.merge({ typed_target_param => 0 }), valid_session
75
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
76
+ else
77
+ expect {
78
+ get_with_compatibility :index, target_params.merge({ typed_target_param => 0 }), valid_session
79
+ }.to raise_error(ActiveRecord::RecordNotFound)
80
+ end
75
81
  end
76
82
  end
77
83
 
@@ -87,8 +93,13 @@ shared_examples_for :subscription_controller do
87
93
  end
88
94
 
89
95
  it "returns json format" do
90
- expect(JSON.parse(response.body)["subscriptions"].first)
91
- .to include("target_id" => test_target.id, "target_type" => test_target.to_class_name)
96
+ if ActivityNotification.config.orm == :active_record
97
+ expect(JSON.parse(response.body)["subscriptions"].first)
98
+ .to include("target_id" => test_target.id, "target_type" => test_target.to_class_name)
99
+ else
100
+ expect(JSON.parse(response.body)["subscriptions"].first)
101
+ .to include("target_id" => test_target.id.to_s, "target_type" => test_target.to_class_name)
102
+ end
92
103
  expect(JSON.parse(response.body)["unconfigured_notification_keys"].first)
93
104
  .to eq('test_notification_key')
94
105
  end
@@ -277,7 +277,7 @@ describe ActivityNotification::Notification, type: :model do
277
277
  context 'with custom_filter options' do
278
278
  it "works with filtered_by_options scope" do
279
279
  if ActivityNotification.config.orm == :active_record
280
- notifications = ActivityNotification::Notification.filtered_by_options({ custom_filter: ["key = ?", @key_1] })
280
+ notifications = ActivityNotification::Notification.filtered_by_options({ custom_filter: ["notifications.key = ?", @key_1] })
281
281
  expect(notifications.to_a.size).to eq(1)
282
282
  expect(notifications.first).to eq(@notification_1)
283
283
  end
@@ -301,9 +301,9 @@ describe ActivityNotification::Notification, type: :model do
301
301
  before do
302
302
  ActivityNotification::Notification.delete_all
303
303
  unopened_group_owner = create(:notification, group_owner: nil)
304
- unopened_group_member = create(:notification, group_owner: unopened_group_owner)
305
- opened_group_owner = create(:notification, group_owner: nil, opened_at: Time.current)
306
- opened_group_member = create(:notification, group_owner: opened_group_owner, opened_at: Time.current)
304
+ unopened_group_member = create(:notification, group_owner: unopened_group_owner, created_at: unopened_group_owner.created_at + 10.second)
305
+ opened_group_owner = create(:notification, group_owner: nil, opened_at: Time.current, created_at: unopened_group_owner.created_at + 20.second)
306
+ opened_group_member = create(:notification, group_owner: opened_group_owner, opened_at: Time.current, created_at: unopened_group_owner.created_at + 30.second)
307
307
  @earliest_notification = unopened_group_owner
308
308
  @latest_notification = opened_group_member
309
309
  end
@@ -93,7 +93,7 @@ describe ActivityNotification::Subscription, type: :model do
93
93
  context 'with custom_filter options' do
94
94
  it "works with filtered_by_options scope" do
95
95
  if ActivityNotification.config.orm == :active_record
96
- subscriptions = ActivityNotification::Subscription.filtered_by_options({ custom_filter: ["key = ?", @key_1] })
96
+ subscriptions = ActivityNotification::Subscription.filtered_by_options({ custom_filter: ["subscriptions.key = ?", @key_1] })
97
97
  expect(subscriptions.size).to eq(1)
98
98
  expect(subscriptions.first).to eq(@subscription_1)
99
99
  end
@@ -117,9 +117,9 @@ describe ActivityNotification::Subscription, type: :model do
117
117
  before do
118
118
  ActivityNotification::Subscription.delete_all
119
119
  @subscription_1 = create(:subscription, key: 'key.1')
120
- @subscription_2 = create(:subscription, key: 'key.2')
121
- @subscription_3 = create(:subscription, key: 'key.3')
122
- @subscription_4 = create(:subscription, key: 'key.4')
120
+ @subscription_2 = create(:subscription, key: 'key.2', created_at: @subscription_1.created_at + 10.second)
121
+ @subscription_3 = create(:subscription, key: 'key.3', created_at: @subscription_1.created_at + 20.second)
122
+ @subscription_4 = create(:subscription, key: 'key.4', created_at: @subscription_1.created_at + 30.second)
123
123
  end
124
124
 
125
125
  it "works with latest_order scope" do
@@ -137,17 +137,21 @@ describe ActivityNotification::Subscription, type: :model do
137
137
  end
138
138
 
139
139
  it "works with latest_subscribed_order scope" do
140
- @subscription_2.subscribe
141
- subscriptions = ActivityNotification::Subscription.latest_subscribed_order
142
- expect(subscriptions.size).to eq(4)
143
- expect(subscriptions.first).to eq(@subscription_2)
140
+ Timecop.travel(1.minute.from_now) do
141
+ @subscription_2.subscribe
142
+ subscriptions = ActivityNotification::Subscription.latest_subscribed_order
143
+ expect(subscriptions.size).to eq(4)
144
+ expect(subscriptions.first).to eq(@subscription_2)
145
+ end
144
146
  end
145
147
 
146
148
  it "works with earliest_subscribed_order scope" do
147
- @subscription_3.subscribe
148
- subscriptions = ActivityNotification::Subscription.earliest_subscribed_order
149
- expect(subscriptions.size).to eq(4)
150
- expect(subscriptions.last).to eq(@subscription_3)
149
+ Timecop.travel(1.minute.from_now) do
150
+ @subscription_3.subscribe
151
+ subscriptions = ActivityNotification::Subscription.earliest_subscribed_order
152
+ expect(subscriptions.size).to eq(4)
153
+ expect(subscriptions.last).to eq(@subscription_3)
154
+ end
151
155
  end
152
156
 
153
157
  it "works with key_order scope" do
@@ -52,7 +52,7 @@ class ArticlesController < ApplicationController
52
52
  private
53
53
  # Use callbacks to share common setup or constraints between actions.
54
54
  def set_article
55
- @article = Article.includes(:user).find_by_id(params[:id])
55
+ @article = Article.includes(:user).find(params[:id])
56
56
  end
57
57
 
58
58
  # Only allow a trusted parameter "white list" through.
@@ -8,7 +8,6 @@ class CommentsController < ApplicationController
8
8
 
9
9
  if @comment.save
10
10
  @comment.notify :users
11
- @comment.notify :admins
12
11
  redirect_to @comment.article, notice: 'Comment was successfully created.'
13
12
  else
14
13
  redirect_to @comment.article
@@ -1,9 +1,30 @@
1
- class Admin < ActiveRecord::Base
2
- belongs_to :user
3
- validates :user, presence: true
4
-
5
- acts_as_notification_target email_allowed: false,
6
- subscription_allowed: true,
7
- devise_resource: :user,
8
- printable_name: ->(admin) { "admin (#{admin.user.name})" }
1
+ if ENV['AN_TEST_DB'] != 'mongodb'
2
+ class Admin < ActiveRecord::Base
3
+ belongs_to :user
4
+ validates :user, presence: true
5
+
6
+ acts_as_notification_target email_allowed: false,
7
+ subscription_allowed: true,
8
+ devise_resource: :user,
9
+ printable_name: ->(admin) { "admin (#{admin.user.name})" }
10
+ end
11
+ else
12
+ require 'mongoid'
13
+ class Admin
14
+ include Mongoid::Document
15
+ include Mongoid::Timestamps
16
+ include GlobalID::Identification
17
+
18
+ belongs_to :user
19
+ validates :user, presence: true
20
+
21
+ field :phone_number, type: String
22
+ field :slack_username, type: String
23
+
24
+ include ActivityNotification::Models
25
+ acts_as_notification_target email_allowed: false,
26
+ subscription_allowed: true,
27
+ devise_resource: :user,
28
+ printable_name: ->(admin) { "admin (#{admin.user.name})" }
29
+ end
9
30
  end
@@ -1,17 +1,49 @@
1
- class Article < ActiveRecord::Base
2
- belongs_to :user
3
- has_many :comments, dependent: :destroy
4
- has_many :commented_users, through: :comments, source: :user
5
- validates :user, presence: true
1
+ if ENV['AN_TEST_DB'] != 'mongodb'
2
+ class Article < ActiveRecord::Base
3
+ belongs_to :user
4
+ has_many :comments, dependent: :destroy
5
+ has_many :commented_users, through: :comments, source: :user
6
+ validates :user, presence: true
6
7
 
7
- acts_as_notifiable :users,
8
- targets: ->(article) { User.all.to_a - [article.user] },
9
- notifier: :user, email_allowed: true,
10
- printable_name: ->(article) { "new article \"#{article.title}\"" },
11
- dependent_notifications: :delete_all
12
- acts_as_notification_group printable_name: ->(article) { "article \"#{article.title}\"" }
8
+ acts_as_notifiable :users,
9
+ targets: ->(article) { User.all.to_a - [article.user] },
10
+ notifier: :user, email_allowed: true,
11
+ printable_name: ->(article) { "new article \"#{article.title}\"" },
12
+ dependent_notifications: :delete_all
13
+ acts_as_notification_group printable_name: ->(article) { "article \"#{article.title}\"" }
13
14
 
14
- def author?(user)
15
- self.user == user
15
+ def author?(user)
16
+ self.user == user
17
+ end
18
+ end
19
+ else
20
+ require 'mongoid'
21
+ class Article
22
+ include Mongoid::Document
23
+ include Mongoid::Timestamps
24
+ include GlobalID::Identification
25
+
26
+ belongs_to :user
27
+ has_many :comments, dependent: :destroy
28
+ validates :user, presence: true
29
+
30
+ field :title, type: String
31
+ field :body, type: String
32
+
33
+ include ActivityNotification::Models
34
+ acts_as_notifiable :users,
35
+ targets: ->(article) { User.all.to_a - [article.user] },
36
+ notifier: :user, email_allowed: true,
37
+ printable_name: ->(article) { "new article \"#{article.title}\"" },
38
+ dependent_notifications: :delete_all
39
+ acts_as_notification_group printable_name: ->(article) { "article \"#{article.title}\"" }
40
+
41
+ def commented_users
42
+ User.where(:id.in => comments.pluck(:user_id))
43
+ end
44
+
45
+ def author?(user)
46
+ self.user == user
47
+ end
16
48
  end
17
49
  end
@@ -1,51 +1,116 @@
1
- class Comment < ActiveRecord::Base
2
- belongs_to :article
3
- belongs_to :user
4
- validates :article, presence: true
5
- validates :user, presence: true
6
-
7
- acts_as_notifiable :users,
8
- targets: ->(comment, key) { ([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq },
9
- group: :article, notifier: :user, email_allowed: true,
10
- parameters: { 'test_default_param' => '1' },
11
- notifiable_path: :article_notifiable_path,
12
- printable_name: ->(comment) { "comment \"#{comment.body}\"" },
13
- dependent_notifications: :update_group_and_delete_all
14
-
15
- require 'custom_optional_targets/console_output'
16
- optional_targets = { CustomOptionalTarget::ConsoleOutput => {} }
17
- if ENV['OPTIONAL_TARGET_AMAZON_SNS']
18
- require 'activity_notification/optional_targets/amazon_sns'
19
- if ENV['OPTIONAL_TARGET_AMAZON_SNS_TOPIC_ARN']
1
+ if ENV['AN_TEST_DB'] != 'mongodb'
2
+ class Comment < ActiveRecord::Base
3
+ belongs_to :article
4
+ belongs_to :user
5
+ validates :article, presence: true
6
+ validates :user, presence: true
7
+
8
+ acts_as_notifiable :users,
9
+ targets: ->(comment, key) { ([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq },
10
+ group: :article, notifier: :user, email_allowed: true,
11
+ parameters: { 'test_default_param' => '1' },
12
+ notifiable_path: :article_notifiable_path,
13
+ printable_name: ->(comment) { "comment \"#{comment.body}\"" },
14
+ dependent_notifications: :update_group_and_delete_all
15
+
16
+ require 'custom_optional_targets/console_output'
17
+ optional_targets = { CustomOptionalTarget::ConsoleOutput => {} }
18
+ if ENV['OPTIONAL_TARGET_AMAZON_SNS']
19
+ require 'activity_notification/optional_targets/amazon_sns'
20
+ if ENV['OPTIONAL_TARGET_AMAZON_SNS_TOPIC_ARN']
21
+ optional_targets = optional_targets.merge(
22
+ ActivityNotification::OptionalTarget::AmazonSNS => { topic_arn: ENV['OPTIONAL_TARGET_AMAZON_SNS_TOPIC_ARN'] }
23
+ )
24
+ elsif ENV['OPTIONAL_TARGET_AMAZON_SNS_PHONE_NUMBER']
25
+ optional_targets = optional_targets.merge(
26
+ ActivityNotification::OptionalTarget::AmazonSNS => { phone_number: :phone_number }
27
+ )
28
+ end
29
+ end
30
+ if ENV['OPTIONAL_TARGET_SLACK']
31
+ require 'activity_notification/optional_targets/slack'
20
32
  optional_targets = optional_targets.merge(
21
- ActivityNotification::OptionalTarget::AmazonSNS => { topic_arn: ENV['OPTIONAL_TARGET_AMAZON_SNS_TOPIC_ARN'] }
33
+ ActivityNotification::OptionalTarget::Slack => {
34
+ webhook_url: ENV['OPTIONAL_TARGET_SLACK_WEBHOOK_URL'], target_username: :slack_username,
35
+ channel: ENV['OPTIONAL_TARGET_SLACK_CHANNEL'] || 'activity_notification',
36
+ username: 'ActivityNotification', icon_emoji: ":ghost:"
37
+ }
22
38
  )
23
- elsif ENV['OPTIONAL_TARGET_AMAZON_SNS_PHONE_NUMBER']
39
+ end
40
+ acts_as_notifiable :admins, targets: [Admin.first].compact,
41
+ group: :article, notifier: :user, notifiable_path: :article_notifiable_path,
42
+ tracked: { only: [:create] },
43
+ printable_name: ->(comment) { "comment \"#{comment.body}\"" },
44
+ dependent_notifications: :delete_all,
45
+ optional_targets: optional_targets
46
+
47
+ def article_notifiable_path
48
+ article_path(article)
49
+ end
50
+
51
+ def author?(user)
52
+ self.user == user
53
+ end
54
+ end
55
+ else
56
+ require 'mongoid'
57
+ class Comment
58
+ include Mongoid::Document
59
+ include Mongoid::Timestamps
60
+ include GlobalID::Identification
61
+
62
+ belongs_to :article
63
+ belongs_to :user
64
+ validates :article, presence: true
65
+ validates :user, presence: true
66
+ field :body, type: String
67
+
68
+ include ActivityNotification::Models
69
+ acts_as_notifiable :users,
70
+ targets: ->(comment, key) { ([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq },
71
+ group: :article, notifier: :user, email_allowed: true,
72
+ parameters: { 'test_default_param' => '1' },
73
+ notifiable_path: :article_notifiable_path,
74
+ printable_name: ->(comment) { "comment \"#{comment.body}\"" },
75
+ dependent_notifications: :update_group_and_delete_all
76
+
77
+ require 'custom_optional_targets/console_output'
78
+ optional_targets = { CustomOptionalTarget::ConsoleOutput => {} }
79
+ if ENV['OPTIONAL_TARGET_AMAZON_SNS']
80
+ require 'activity_notification/optional_targets/amazon_sns'
81
+ if ENV['OPTIONAL_TARGET_AMAZON_SNS_TOPIC_ARN']
82
+ optional_targets = optional_targets.merge(
83
+ ActivityNotification::OptionalTarget::AmazonSNS => { topic_arn: ENV['OPTIONAL_TARGET_AMAZON_SNS_TOPIC_ARN'] }
84
+ )
85
+ elsif ENV['OPTIONAL_TARGET_AMAZON_SNS_PHONE_NUMBER']
86
+ optional_targets = optional_targets.merge(
87
+ ActivityNotification::OptionalTarget::AmazonSNS => { phone_number: :phone_number }
88
+ )
89
+ end
90
+ end
91
+ if ENV['OPTIONAL_TARGET_SLACK']
92
+ require 'activity_notification/optional_targets/slack'
24
93
  optional_targets = optional_targets.merge(
25
- ActivityNotification::OptionalTarget::AmazonSNS => { phone_number: :phone_number }
94
+ ActivityNotification::OptionalTarget::Slack => {
95
+ webhook_url: ENV['OPTIONAL_TARGET_SLACK_WEBHOOK_URL'], target_username: :slack_username,
96
+ channel: ENV['OPTIONAL_TARGET_SLACK_CHANNEL'] || 'activity_notification',
97
+ username: 'ActivityNotification', icon_emoji: ":ghost:"
98
+ }
26
99
  )
27
100
  end
28
- end
29
- if ENV['OPTIONAL_TARGET_SLACK']
30
- require 'activity_notification/optional_targets/slack'
31
- optional_targets = optional_targets.merge(
32
- ActivityNotification::OptionalTarget::Slack => {
33
- webhook_url: ENV['OPTIONAL_TARGET_SLACK_WEBHOOK_URL'], target_username: :slack_username,
34
- channel: ENV['OPTIONAL_TARGET_SLACK_CHANNEL'] || 'activity_notification',
35
- username: 'ActivityNotification', icon_emoji: ":ghost:"
36
- }
37
- )
38
- end
39
- acts_as_notifiable :admins, targets: Admin.all,
40
- group: :article, notifier: :user, notifiable_path: :article_notifiable_path,
41
- printable_name: ->(comment) { "comment \"#{comment.body}\"" }, dependent_notifications: :delete_all,
42
- optional_targets: optional_targets
101
+ acts_as_notifiable :admins, targets: [Admin.first].compact,
102
+ group: :article, notifier: :user, notifiable_path: :article_notifiable_path,
103
+ tracked: { only: [:create] },
104
+ printable_name: ->(comment) { "comment \"#{comment.body}\"" },
105
+ dependent_notifications: :delete_all,
106
+ optional_targets: optional_targets
43
107
 
44
- def article_notifiable_path
45
- article_path(article)
46
- end
108
+ def article_notifiable_path
109
+ article_path(article)
110
+ end
47
111
 
48
- def author?(user)
49
- self.user == user
112
+ def author?(user)
113
+ self.user == user
114
+ end
50
115
  end
51
116
  end