activity_notification 0.0.8 → 0.0.9
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +11 -1
- data/README.md +63 -28
- data/activity_notification.gemspec +4 -2
- data/app/controllers/activity_notification/notifications_controller.rb +1 -1
- data/app/controllers/activity_notification/notifications_with_devise_controller.rb +10 -10
- data/app/views/activity_notification/notifications/default/_index.html.erb +9 -4
- data/lib/activity_notification.rb +7 -6
- data/lib/activity_notification/apis/notification_api.rb +14 -15
- data/lib/activity_notification/common.rb +15 -7
- data/lib/activity_notification/config.rb +2 -0
- data/lib/activity_notification/helpers/view_helpers.rb +5 -4
- data/lib/activity_notification/mailers/helpers.rb +9 -9
- data/lib/activity_notification/models.rb +16 -0
- data/lib/activity_notification/models/{notifiable.rb → concerns/notifiable.rb} +15 -10
- data/lib/activity_notification/models/{notifier.rb → concerns/notifier.rb} +6 -0
- data/lib/activity_notification/models/{target.rb → concerns/target.rb} +34 -17
- data/lib/activity_notification/renderable.rb +2 -1
- data/lib/activity_notification/roles/acts_as_notifiable.rb +38 -23
- data/lib/activity_notification/roles/acts_as_notifier.rb +11 -0
- data/lib/activity_notification/roles/acts_as_target.rb +9 -18
- data/lib/activity_notification/version.rb +1 -1
- data/lib/generators/activity_notification/{migration → active_record}/migration_generator.rb +5 -4
- data/lib/generators/activity_notification/controllers_generator.rb +1 -1
- data/lib/generators/activity_notification/install_generator.rb +3 -6
- data/lib/generators/activity_notification/{notification → models}/notification_generator.rb +5 -4
- data/lib/generators/activity_notification/views_generator.rb +20 -22
- data/lib/generators/templates/active_record/migration.rb +1 -1
- data/lib/generators/templates/activity_notification.rb +13 -3
- data/{config → lib/generators/templates}/locales/en.yml +0 -0
- data/lib/generators/templates/notification/notification.rb +4 -1
- data/spec/concerns/{notification_api_spec.rb → apis/notification_api_spec.rb} +169 -45
- data/spec/concerns/common_spec.rb +150 -0
- data/spec/concerns/models/notifiable_spec.rb +435 -0
- data/spec/concerns/models/notifier_spec.rb +23 -0
- data/spec/concerns/models/target_spec.rb +579 -0
- data/spec/concerns/renderable_spec.rb +110 -0
- data/spec/controllers/notifications_controller_shared_examples.rb +457 -0
- data/spec/controllers/notifications_controller_spec.rb +12 -0
- data/spec/controllers/notifications_with_devise_controller_spec.rb +81 -0
- data/spec/factories/admins.rb +5 -0
- data/spec/factories/articles.rb +1 -1
- data/spec/factories/comments.rb +1 -1
- data/spec/factories/dummy/dummy_notifiable.rb +4 -0
- data/spec/factories/dummy/dummy_notifier.rb +4 -0
- data/spec/factories/dummy/dummy_target.rb +4 -0
- data/spec/factories/notifications.rb +1 -1
- data/spec/factories/users.rb +7 -1
- data/spec/generators/active_record/migration_generator_spec.rb +41 -0
- data/spec/generators/controllers_generator_spec.rb +62 -0
- data/spec/generators/install_generator_spec.rb +43 -0
- data/spec/generators/models/notification_generator_spec.rb +41 -0
- data/spec/generators/views_generator_spec.rb +111 -0
- data/spec/helpers/polymorphic_helpers_spec.rb +89 -0
- data/spec/helpers/view_helpers_spec.rb +258 -0
- data/spec/mailers/mailer_spec.rb +98 -0
- data/spec/models/dummy/dummy_notifiable_spec.rb +6 -0
- data/spec/models/dummy/dummy_notifier_spec.rb +6 -0
- data/spec/models/dummy/dummy_target_spec.rb +6 -0
- data/spec/models/notification_spec.rb +5 -4
- data/spec/rails_app/app/assets/javascripts/application.js +2 -0
- data/spec/rails_app/app/controllers/articles_controller.rb +62 -0
- data/spec/rails_app/app/controllers/comments_controller.rb +34 -0
- data/spec/rails_app/app/models/admin.rb +8 -0
- data/spec/rails_app/app/models/article.rb +6 -6
- data/spec/rails_app/app/models/comment.rb +2 -2
- data/spec/rails_app/app/models/dummy/dummy_base.rb +2 -0
- data/spec/rails_app/app/models/dummy/dummy_notifiable.rb +4 -0
- data/spec/rails_app/app/models/dummy/dummy_notifier.rb +4 -0
- data/spec/rails_app/app/models/dummy/dummy_target.rb +4 -0
- data/spec/rails_app/app/models/user.rb +5 -5
- data/spec/rails_app/app/views/activity_notification/notifications/default/custom/_path_test.html.erb +1 -0
- data/spec/rails_app/app/views/activity_notification/notifications/default/custom/_test.html.erb +1 -0
- data/spec/rails_app/app/views/activity_notification/notifications/users/_custom_index.html.erb +1 -0
- data/spec/rails_app/app/views/activity_notification/notifications/users/custom/_test.html.erb +1 -0
- data/spec/rails_app/app/views/articles/_form.html.erb +20 -0
- data/spec/rails_app/app/views/articles/edit.html.erb +6 -0
- data/spec/rails_app/app/views/articles/index.html.erb +67 -0
- data/spec/rails_app/app/views/articles/new.html.erb +5 -0
- data/spec/rails_app/app/views/articles/show.html.erb +38 -0
- data/spec/rails_app/app/views/layouts/_header.html.erb +8 -0
- data/spec/rails_app/app/views/layouts/application.html.erb +3 -4
- data/spec/rails_app/config/initializers/activity_notification.rb +13 -3
- data/spec/rails_app/config/initializers/devise.rb +274 -274
- data/spec/rails_app/config/locales/activity_notification.en.yml +20 -0
- data/spec/rails_app/config/locales/devise.en.yml +62 -0
- data/spec/rails_app/config/routes.rb +6 -2
- data/spec/rails_app/db/migrate/20160715050433_create_test_tables.rb +6 -2
- data/spec/rails_app/db/schema.rb +8 -0
- data/spec/rails_app/db/seeds.rb +43 -0
- data/spec/roles/acts_as_notifiable_spec.rb +32 -0
- data/spec/roles/acts_as_notifier_spec.rb +17 -0
- data/spec/roles/acts_as_target_spec.rb +40 -0
- data/spec/spec_helper.rb +18 -14
- metadata +136 -12
@@ -0,0 +1,23 @@
|
|
1
|
+
shared_examples_for :notifier do
|
2
|
+
let(:test_class_name) { described_class.to_s.underscore.split('/').last.to_sym }
|
3
|
+
let(:test_instance) { create(test_class_name) }
|
4
|
+
|
5
|
+
describe "with association" do
|
6
|
+
it "has many sent_notifications" do
|
7
|
+
notification_1 = create(:notification, notifier: test_instance)
|
8
|
+
notification_2 = create(:notification, notifier: test_instance)
|
9
|
+
expect(test_instance.sent_notifications.count).to eq(2)
|
10
|
+
expect(test_instance.sent_notifications.earliest).to eq(notification_1)
|
11
|
+
expect(test_instance.sent_notifications.latest).to eq(notification_2)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "as public class methods" do
|
16
|
+
describe "#available_as_notifier?" do
|
17
|
+
it "returns true" do
|
18
|
+
expect(described_class.available_as_notifier?).to be_truthy
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,579 @@
|
|
1
|
+
shared_examples_for :target do
|
2
|
+
let(:test_class_name) { described_class.to_s.underscore.split('/').last.to_sym }
|
3
|
+
let(:test_instance) { create(test_class_name) }
|
4
|
+
let(:test_notifiable) { create(:dummy_notifiable) }
|
5
|
+
|
6
|
+
describe "with association" do
|
7
|
+
it "has many notifications" do
|
8
|
+
notification_1 = create(:notification, target: test_instance)
|
9
|
+
notification_2 = create(:notification, target: test_instance)
|
10
|
+
expect(test_instance.notifications.count).to eq(2)
|
11
|
+
expect(test_instance.notifications.earliest).to eq(notification_1)
|
12
|
+
expect(test_instance.notifications.latest).to eq(notification_2)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "as public class methods" do
|
17
|
+
describe "#available_as_target?" do
|
18
|
+
it "returns true" do
|
19
|
+
expect(described_class.available_as_target?).to be_truthy
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#set_target_class_defaults" do
|
24
|
+
it "set parameter fields as default" do
|
25
|
+
described_class.set_target_class_defaults
|
26
|
+
expect(described_class._notification_email).to eq(nil)
|
27
|
+
expect(described_class._notification_email_allowed).to eq(ActivityNotification.config.email_enabled)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "as public instance methods" do
|
33
|
+
before do
|
34
|
+
ActivityNotification::Notification.delete_all
|
35
|
+
described_class.set_target_class_defaults
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#mailer_to" do
|
39
|
+
context "without any configuration" do
|
40
|
+
it "returns nil" do
|
41
|
+
expect(test_instance.mailer_to).to be_nil
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "configured with a field" do
|
46
|
+
it "returns specified value" do
|
47
|
+
described_class._notification_email = 'test@example.com'
|
48
|
+
expect(test_instance.mailer_to).to eq('test@example.com')
|
49
|
+
end
|
50
|
+
|
51
|
+
it "returns specified symbol of field" do
|
52
|
+
described_class._notification_email = :email
|
53
|
+
expect(test_instance.mailer_to).to eq(test_instance.email)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "returns specified symbol of method" do
|
57
|
+
module AdditionalMethods
|
58
|
+
def custom_notification_email
|
59
|
+
'test@example.com'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
test_instance.extend(AdditionalMethods)
|
63
|
+
described_class._notification_email = :custom_notification_email
|
64
|
+
expect(test_instance.mailer_to).to eq('test@example.com')
|
65
|
+
end
|
66
|
+
|
67
|
+
it "returns specified lambda with single notifiable argument" do
|
68
|
+
described_class._notification_email = ->(target){ 'test@example.com' }
|
69
|
+
expect(test_instance.mailer_to).to eq('test@example.com')
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "#notification_email_allowed?" do
|
75
|
+
context "without any configuration" do
|
76
|
+
it "returns ActivityNotification.config.email_enabled" do
|
77
|
+
expect(test_instance.notification_email_allowed?(test_notifiable, 'dummy_key'))
|
78
|
+
.to eq(ActivityNotification.config.email_enabled)
|
79
|
+
end
|
80
|
+
|
81
|
+
it "returns false as default" do
|
82
|
+
expect(test_instance.notification_email_allowed?(test_notifiable, 'dummy_key')).to be_falsey
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context "configured with a field" do
|
87
|
+
it "returns specified value" do
|
88
|
+
described_class._notification_email_allowed = true
|
89
|
+
expect(test_instance.notification_email_allowed?(test_notifiable, 'dummy_key')).to eq(true)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "returns specified symbol without argument" do
|
93
|
+
module AdditionalMethods
|
94
|
+
def custom_notification_email_allowed?
|
95
|
+
true
|
96
|
+
end
|
97
|
+
end
|
98
|
+
test_instance.extend(AdditionalMethods)
|
99
|
+
described_class._notification_email_allowed = :custom_notification_email_allowed?
|
100
|
+
expect(test_instance.notification_email_allowed?(test_notifiable, 'dummy_key')).to eq(true)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "returns specified symbol with target and key arguments" do
|
104
|
+
module AdditionalMethods
|
105
|
+
def custom_notification_email_allowed?(notifiable, key)
|
106
|
+
true
|
107
|
+
end
|
108
|
+
end
|
109
|
+
test_instance.extend(AdditionalMethods)
|
110
|
+
described_class._notification_email_allowed = :custom_notification_email_allowed?
|
111
|
+
expect(test_instance.notification_email_allowed?(test_notifiable, 'dummy_key')).to eq(true)
|
112
|
+
end
|
113
|
+
|
114
|
+
it "returns specified lambda with single target argument" do
|
115
|
+
described_class._notification_email_allowed = ->(target){ true }
|
116
|
+
expect(test_instance.notification_email_allowed?(test_notifiable, 'dummy_key')).to eq(true)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "returns specified lambda with target, notifiable and key arguments" do
|
120
|
+
described_class._notification_email_allowed = ->(target, notifiable, key){ true }
|
121
|
+
expect(test_instance.notification_email_allowed?(test_notifiable, 'dummy_key')).to eq(true)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe "#authenticated_with_devise?" do
|
127
|
+
context "without any configuration" do
|
128
|
+
context "when the current device resource and called target are defferent class instance" do
|
129
|
+
it "raises TypeError" do
|
130
|
+
expect { test_instance.authenticated_with_devise?(test_notifiable) }
|
131
|
+
.to raise_error(TypeError, /Defferent type of .+ has been passed to .+ You have to override .+ /)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
context "when the current device resource equals called target" do
|
136
|
+
it "returns true" do
|
137
|
+
expect(test_instance.authenticated_with_devise?(test_instance)).to be_truthy
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
context "when the current device resource does not equal called target" do
|
142
|
+
it "returns false" do
|
143
|
+
expect(test_instance.authenticated_with_devise?(create(test_class_name))).to be_falsey
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
context "configured with a field" do
|
149
|
+
context "when the current device resource and called target are defferent class instance" do
|
150
|
+
it "raises TypeError" do
|
151
|
+
described_class._notification_devise_resource = test_notifiable
|
152
|
+
expect { test_instance.authenticated_with_devise?(test_instance) }
|
153
|
+
.to raise_error(TypeError, /Defferent type of .+ has been passed to .+ You have to override .+ /)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
context "when the current device resource equals called target" do
|
158
|
+
it "returns true" do
|
159
|
+
described_class._notification_devise_resource = test_notifiable
|
160
|
+
expect(test_instance.authenticated_with_devise?(test_notifiable)).to be_truthy
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
context "when the current device resource does not equal called target" do
|
165
|
+
it "returns false" do
|
166
|
+
described_class._notification_devise_resource = test_instance
|
167
|
+
expect(test_instance.authenticated_with_devise?(create(test_class_name))).to be_falsey
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
describe "#unopened_notification_count" do
|
174
|
+
it "returns count of unopened notification index" do
|
175
|
+
create(:notification, target: test_instance)
|
176
|
+
create(:notification, target: test_instance)
|
177
|
+
expect(test_instance.unopened_notification_count).to eq(2)
|
178
|
+
end
|
179
|
+
|
180
|
+
it "returns count of unopened notification index (owner only)" do
|
181
|
+
group_owner = create(:notification, target: test_instance, group_owner: nil)
|
182
|
+
create(:notification, target: test_instance, group_owner: nil)
|
183
|
+
group_member = create(:notification, target: test_instance, group_owner: group_owner)
|
184
|
+
expect(test_instance.unopened_notification_count).to eq(2)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
describe "#has_unopened_notifications?" do
|
189
|
+
context "when the target has no unopened notifications" do
|
190
|
+
it "returns false" do
|
191
|
+
expect(test_instance.has_unopened_notifications?).to be_falsey
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
context "when the target has unopened notifications" do
|
196
|
+
it "returns true" do
|
197
|
+
create(:notification, target: test_instance)
|
198
|
+
expect(test_instance.has_unopened_notifications?).to be_truthy
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
describe "#notification_index" do
|
204
|
+
context "when the target has no notifications" do
|
205
|
+
it "returns empty records" do
|
206
|
+
expect(test_instance.notification_index).to be_empty
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
context "when the target has unopened notifications" do
|
211
|
+
before do
|
212
|
+
create(:notification, target: test_instance)
|
213
|
+
create(:notification, target: test_instance)
|
214
|
+
end
|
215
|
+
|
216
|
+
it "calls unopened_notification_index" do
|
217
|
+
expect(test_instance).to receive(:unopened_notification_index)
|
218
|
+
test_instance.notification_index
|
219
|
+
end
|
220
|
+
|
221
|
+
context "without limit" do
|
222
|
+
it "returns the same as unopened_notification_index" do
|
223
|
+
expect(test_instance.notification_index).to eq(test_instance.unopened_notification_index)
|
224
|
+
expect(test_instance.notification_index.size).to eq(2)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
context "with limit" do
|
229
|
+
it "returns the same as unopened_notification_index with limit" do
|
230
|
+
expect(test_instance.notification_index(1)).to eq(test_instance.unopened_notification_index(1))
|
231
|
+
expect(test_instance.notification_index(1).size).to eq(1)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
context "when the target has no unopened notifications" do
|
237
|
+
before do
|
238
|
+
create(:notification, target: test_instance, opened_at: DateTime.now)
|
239
|
+
create(:notification, target: test_instance, opened_at: DateTime.now)
|
240
|
+
end
|
241
|
+
|
242
|
+
it "calls unopened_notification_index" do
|
243
|
+
expect(test_instance).to receive(:opened_notification_index)
|
244
|
+
test_instance.notification_index
|
245
|
+
end
|
246
|
+
|
247
|
+
context "without limit" do
|
248
|
+
it "returns the same as opened_notification_index" do
|
249
|
+
expect(test_instance.notification_index).to eq(test_instance.opened_notification_index)
|
250
|
+
expect(test_instance.notification_index.size).to eq(2)
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
context "with limit" do
|
255
|
+
it "returns the same as opened_notification_index with limit" do
|
256
|
+
expect(test_instance.notification_index(1)).to eq(test_instance.opened_notification_index(1))
|
257
|
+
expect(test_instance.notification_index(1).size).to eq(1)
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
describe "#unopened_notification_index" do
|
264
|
+
context "when the target has no notifications" do
|
265
|
+
it "returns empty records" do
|
266
|
+
expect(test_instance.unopened_notification_index).to be_empty
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
context "when the target has unopened notifications" do
|
271
|
+
before do
|
272
|
+
@notification_1 = create(:notification, target: test_instance)
|
273
|
+
@notification_2 = create(:notification, target: test_instance)
|
274
|
+
end
|
275
|
+
|
276
|
+
context "without limit" do
|
277
|
+
it "returns unopened notification index" do
|
278
|
+
expect(test_instance.unopened_notification_index.size).to eq(2)
|
279
|
+
expect(test_instance.unopened_notification_index.last).to eq(@notification_1)
|
280
|
+
expect(test_instance.unopened_notification_index.first).to eq(@notification_2)
|
281
|
+
end
|
282
|
+
|
283
|
+
it "returns unopened notification index (owner only)" do
|
284
|
+
group_member = create(:notification, target: test_instance, group_owner: @notification_1)
|
285
|
+
expect(test_instance.unopened_notification_index.size).to eq(2)
|
286
|
+
expect(test_instance.unopened_notification_index.last).to eq(@notification_1)
|
287
|
+
expect(test_instance.unopened_notification_index.first).to eq(@notification_2)
|
288
|
+
end
|
289
|
+
|
290
|
+
it "returns unopened notification index (unopened only)" do
|
291
|
+
notification_3 = create(:notification, target: test_instance, opened_at: DateTime.now)
|
292
|
+
expect(test_instance.unopened_notification_index.size).to eq(2)
|
293
|
+
expect(test_instance.unopened_notification_index.last).to eq(@notification_1)
|
294
|
+
expect(test_instance.unopened_notification_index.first).to eq(@notification_2)
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
context "with limit" do
|
299
|
+
it "returns unopened notification index with limit" do
|
300
|
+
expect(test_instance.unopened_notification_index(1).size).to eq(1)
|
301
|
+
expect(test_instance.unopened_notification_index(1).first).to eq(@notification_2)
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
context "when the target has no unopened notifications" do
|
307
|
+
before do
|
308
|
+
create(:notification, target: test_instance, group_owner: nil, opened_at: DateTime.now)
|
309
|
+
create(:notification, target: test_instance, group_owner: nil, opened_at: DateTime.now)
|
310
|
+
end
|
311
|
+
|
312
|
+
it "returns empty records" do
|
313
|
+
expect(test_instance.unopened_notification_index).to be_empty
|
314
|
+
end
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
describe "#opened_notification_index" do
|
319
|
+
context "when the target has no notifications" do
|
320
|
+
it "returns empty records" do
|
321
|
+
expect(test_instance.opened_notification_index(1)).to be_empty
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
context "when the target has opened notifications" do
|
326
|
+
before do
|
327
|
+
@notification_1 = create(:notification, target: test_instance, opened_at: DateTime.now)
|
328
|
+
@notification_2 = create(:notification, target: test_instance, opened_at: DateTime.now)
|
329
|
+
end
|
330
|
+
|
331
|
+
context "without limit" do
|
332
|
+
it "uses ActivityNotification.config.opened_limit as limit" do
|
333
|
+
configured_opened_limit = ActivityNotification.config.opened_limit
|
334
|
+
ActivityNotification.config.opened_limit = 1
|
335
|
+
expect(test_instance.opened_notification_index(1).size).to eq(1)
|
336
|
+
expect(test_instance.opened_notification_index(1).first).to eq(@notification_2)
|
337
|
+
ActivityNotification.config.opened_limit = configured_opened_limit
|
338
|
+
end
|
339
|
+
|
340
|
+
it "returns opened notification index" do
|
341
|
+
expect(test_instance.opened_notification_index.size).to eq(2)
|
342
|
+
expect(test_instance.opened_notification_index.last).to eq(@notification_1)
|
343
|
+
expect(test_instance.opened_notification_index.first).to eq(@notification_2)
|
344
|
+
end
|
345
|
+
|
346
|
+
it "returns opened notification index (owner only)" do
|
347
|
+
group_member = create(:notification, target: test_instance, group_owner: @notification_1, opened_at: DateTime.now)
|
348
|
+
expect(test_instance.opened_notification_index.size).to eq(2)
|
349
|
+
expect(test_instance.opened_notification_index.last).to eq(@notification_1)
|
350
|
+
expect(test_instance.opened_notification_index.first).to eq(@notification_2)
|
351
|
+
end
|
352
|
+
|
353
|
+
it "returns opened notification index (opened only)" do
|
354
|
+
notification_3 = create(:notification, target: test_instance)
|
355
|
+
expect(test_instance.opened_notification_index.size).to eq(2)
|
356
|
+
expect(test_instance.opened_notification_index.last).to eq(@notification_1)
|
357
|
+
expect(test_instance.opened_notification_index.first).to eq(@notification_2)
|
358
|
+
end
|
359
|
+
end
|
360
|
+
|
361
|
+
context "with limit" do
|
362
|
+
it "returns opened notification index with limit" do
|
363
|
+
expect(test_instance.opened_notification_index(1).size).to eq(1)
|
364
|
+
expect(test_instance.opened_notification_index(1).first).to eq(@notification_2)
|
365
|
+
end
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
context "when the target has no opened notifications" do
|
370
|
+
before do
|
371
|
+
create(:notification, target: test_instance, group_owner: nil)
|
372
|
+
create(:notification, target: test_instance, group_owner: nil)
|
373
|
+
end
|
374
|
+
|
375
|
+
it "returns empty records" do
|
376
|
+
expect(test_instance.opened_notification_index).to be_empty
|
377
|
+
end
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
381
|
+
|
382
|
+
# Wrapper methods of Notification class methods
|
383
|
+
|
384
|
+
describe "#notify_to" do
|
385
|
+
it "is an alias of ActivityNotification::Notification.notify_to" do
|
386
|
+
expect(ActivityNotification::Notification).to receive(:notify_to)
|
387
|
+
test_instance.notify_to create(:user)
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
391
|
+
describe "#open_all_notifications" do
|
392
|
+
it "is an alias of ActivityNotification::Notification.open_all_of" do
|
393
|
+
expect(ActivityNotification::Notification).to receive(:open_all_of)
|
394
|
+
test_instance.open_all_notifications
|
395
|
+
end
|
396
|
+
end
|
397
|
+
|
398
|
+
|
399
|
+
# Methods to be overriden
|
400
|
+
|
401
|
+
describe "#notification_index_with_attributes" do
|
402
|
+
context "when the target has no notifications" do
|
403
|
+
it "returns empty records" do
|
404
|
+
expect(test_instance.notification_index_with_attributes).to be_empty
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
context "when the target has unopened notifications" do
|
409
|
+
before do
|
410
|
+
create(:notification, target: test_instance)
|
411
|
+
create(:notification, target: test_instance)
|
412
|
+
end
|
413
|
+
|
414
|
+
it "calls unopened_notification_index_with_attributes" do
|
415
|
+
expect(test_instance).to receive(:unopened_notification_index_with_attributes)
|
416
|
+
test_instance.notification_index_with_attributes
|
417
|
+
end
|
418
|
+
|
419
|
+
context "without limit" do
|
420
|
+
it "returns the same as unopened_notification_index_with_attributes" do
|
421
|
+
expect(test_instance.notification_index_with_attributes).to eq(test_instance.unopened_notification_index_with_attributes)
|
422
|
+
expect(test_instance.notification_index_with_attributes.size).to eq(2)
|
423
|
+
end
|
424
|
+
end
|
425
|
+
|
426
|
+
context "with limit" do
|
427
|
+
it "returns the same as unopened_notification_index_with_attributes with limit" do
|
428
|
+
expect(test_instance.notification_index_with_attributes(1)).to eq(test_instance.unopened_notification_index_with_attributes(1))
|
429
|
+
expect(test_instance.notification_index_with_attributes(1).size).to eq(1)
|
430
|
+
end
|
431
|
+
end
|
432
|
+
end
|
433
|
+
|
434
|
+
context "when the target has no unopened notifications" do
|
435
|
+
before do
|
436
|
+
create(:notification, target: test_instance, opened_at: DateTime.now)
|
437
|
+
create(:notification, target: test_instance, opened_at: DateTime.now)
|
438
|
+
end
|
439
|
+
|
440
|
+
it "calls unopened_notification_index_with_attributes" do
|
441
|
+
expect(test_instance).to receive(:opened_notification_index_with_attributes)
|
442
|
+
test_instance.notification_index_with_attributes
|
443
|
+
end
|
444
|
+
|
445
|
+
context "without limit" do
|
446
|
+
it "returns the same as opened_notification_index_with_attributes" do
|
447
|
+
expect(test_instance.notification_index_with_attributes).to eq(test_instance.opened_notification_index_with_attributes)
|
448
|
+
expect(test_instance.notification_index_with_attributes.size).to eq(2)
|
449
|
+
end
|
450
|
+
end
|
451
|
+
|
452
|
+
context "with limit" do
|
453
|
+
it "returns the same as opened_notification_index_with_attributes with limit" do
|
454
|
+
expect(test_instance.notification_index_with_attributes(1)).to eq(test_instance.opened_notification_index_with_attributes(1))
|
455
|
+
expect(test_instance.notification_index_with_attributes(1).size).to eq(1)
|
456
|
+
end
|
457
|
+
end
|
458
|
+
end
|
459
|
+
end
|
460
|
+
|
461
|
+
describe "#unopened_notification_index_with_attributes" do
|
462
|
+
it "calls unopened_notification_index" do
|
463
|
+
expect(test_instance).to receive(:unopened_notification_index)
|
464
|
+
test_instance.unopened_notification_index_with_attributes
|
465
|
+
end
|
466
|
+
|
467
|
+
context "when the target has unopened notifications with no group members" do
|
468
|
+
context "with no group members" do
|
469
|
+
before do
|
470
|
+
create(:notification, target: test_instance)
|
471
|
+
create(:notification, target: test_instance)
|
472
|
+
end
|
473
|
+
|
474
|
+
it "calls with_target, with_notifiable and with_notifier" do
|
475
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:target)
|
476
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifiable)
|
477
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifier)
|
478
|
+
test_instance.unopened_notification_index_with_attributes
|
479
|
+
end
|
480
|
+
|
481
|
+
it "does not call with_group" do
|
482
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:target)
|
483
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifiable)
|
484
|
+
expect(ActiveRecord::Base).not_to receive(:includes).with(:group)
|
485
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifier)
|
486
|
+
test_instance.unopened_notification_index_with_attributes
|
487
|
+
end
|
488
|
+
end
|
489
|
+
|
490
|
+
context "with group members" do
|
491
|
+
before do
|
492
|
+
group_owner = create(:notification, target: test_instance, group_owner: nil)
|
493
|
+
create(:notification, target: test_instance, group_owner: nil)
|
494
|
+
group_member = create(:notification, target: test_instance, group_owner: group_owner)
|
495
|
+
end
|
496
|
+
|
497
|
+
it "calls with_group" do
|
498
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:target)
|
499
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifiable)
|
500
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:group)
|
501
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifier)
|
502
|
+
test_instance.unopened_notification_index_with_attributes
|
503
|
+
end
|
504
|
+
end
|
505
|
+
end
|
506
|
+
|
507
|
+
context "when the target has no unopened notifications" do
|
508
|
+
before do
|
509
|
+
create(:notification, target: test_instance, opened_at: DateTime.now)
|
510
|
+
create(:notification, target: test_instance, opened_at: DateTime.now)
|
511
|
+
end
|
512
|
+
|
513
|
+
it "returns empty records" do
|
514
|
+
expect(test_instance.unopened_notification_index_with_attributes).to be_empty
|
515
|
+
end
|
516
|
+
end
|
517
|
+
end
|
518
|
+
|
519
|
+
describe "#opened_notification_index_with_attributes" do
|
520
|
+
it "calls opened_notification_index" do
|
521
|
+
expect(test_instance).to receive(:opened_notification_index)
|
522
|
+
test_instance.opened_notification_index_with_attributes
|
523
|
+
end
|
524
|
+
|
525
|
+
context "when the target has opened notifications with no group members" do
|
526
|
+
context "with no group members" do
|
527
|
+
before do
|
528
|
+
create(:notification, target: test_instance, opened_at: DateTime.now)
|
529
|
+
create(:notification, target: test_instance, opened_at: DateTime.now)
|
530
|
+
end
|
531
|
+
|
532
|
+
it "calls with_target, with_notifiable and with_notifier" do
|
533
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:target)
|
534
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifiable)
|
535
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifier)
|
536
|
+
test_instance.opened_notification_index_with_attributes
|
537
|
+
end
|
538
|
+
|
539
|
+
it "does not call with_group" do
|
540
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:target)
|
541
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifiable)
|
542
|
+
expect(ActiveRecord::Base).not_to receive(:includes).with(:group)
|
543
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifier)
|
544
|
+
test_instance.opened_notification_index_with_attributes
|
545
|
+
end
|
546
|
+
end
|
547
|
+
|
548
|
+
context "with group members" do
|
549
|
+
before do
|
550
|
+
group_owner = create(:notification, target: test_instance, group_owner: nil, opened_at: DateTime.now)
|
551
|
+
create(:notification, target: test_instance, group_owner: nil, opened_at: DateTime.now)
|
552
|
+
group_member = create(:notification, target: test_instance, group_owner: group_owner, opened_at: DateTime.now)
|
553
|
+
end
|
554
|
+
|
555
|
+
it "calls with_group" do
|
556
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:target)
|
557
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifiable)
|
558
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:group)
|
559
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifier)
|
560
|
+
test_instance.opened_notification_index_with_attributes
|
561
|
+
end
|
562
|
+
end
|
563
|
+
end
|
564
|
+
|
565
|
+
context "when the target has no opened notifications" do
|
566
|
+
before do
|
567
|
+
create(:notification, target: test_instance)
|
568
|
+
create(:notification, target: test_instance)
|
569
|
+
end
|
570
|
+
|
571
|
+
it "returns empty records" do
|
572
|
+
expect(test_instance.opened_notification_index_with_attributes).to be_empty
|
573
|
+
end
|
574
|
+
end
|
575
|
+
end
|
576
|
+
|
577
|
+
end
|
578
|
+
|
579
|
+
end
|