activity_notification 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +11 -1
  3. data/README.md +63 -28
  4. data/activity_notification.gemspec +4 -2
  5. data/app/controllers/activity_notification/notifications_controller.rb +1 -1
  6. data/app/controllers/activity_notification/notifications_with_devise_controller.rb +10 -10
  7. data/app/views/activity_notification/notifications/default/_index.html.erb +9 -4
  8. data/lib/activity_notification.rb +7 -6
  9. data/lib/activity_notification/apis/notification_api.rb +14 -15
  10. data/lib/activity_notification/common.rb +15 -7
  11. data/lib/activity_notification/config.rb +2 -0
  12. data/lib/activity_notification/helpers/view_helpers.rb +5 -4
  13. data/lib/activity_notification/mailers/helpers.rb +9 -9
  14. data/lib/activity_notification/models.rb +16 -0
  15. data/lib/activity_notification/models/{notifiable.rb → concerns/notifiable.rb} +15 -10
  16. data/lib/activity_notification/models/{notifier.rb → concerns/notifier.rb} +6 -0
  17. data/lib/activity_notification/models/{target.rb → concerns/target.rb} +34 -17
  18. data/lib/activity_notification/renderable.rb +2 -1
  19. data/lib/activity_notification/roles/acts_as_notifiable.rb +38 -23
  20. data/lib/activity_notification/roles/acts_as_notifier.rb +11 -0
  21. data/lib/activity_notification/roles/acts_as_target.rb +9 -18
  22. data/lib/activity_notification/version.rb +1 -1
  23. data/lib/generators/activity_notification/{migration → active_record}/migration_generator.rb +5 -4
  24. data/lib/generators/activity_notification/controllers_generator.rb +1 -1
  25. data/lib/generators/activity_notification/install_generator.rb +3 -6
  26. data/lib/generators/activity_notification/{notification → models}/notification_generator.rb +5 -4
  27. data/lib/generators/activity_notification/views_generator.rb +20 -22
  28. data/lib/generators/templates/active_record/migration.rb +1 -1
  29. data/lib/generators/templates/activity_notification.rb +13 -3
  30. data/{config → lib/generators/templates}/locales/en.yml +0 -0
  31. data/lib/generators/templates/notification/notification.rb +4 -1
  32. data/spec/concerns/{notification_api_spec.rb → apis/notification_api_spec.rb} +169 -45
  33. data/spec/concerns/common_spec.rb +150 -0
  34. data/spec/concerns/models/notifiable_spec.rb +435 -0
  35. data/spec/concerns/models/notifier_spec.rb +23 -0
  36. data/spec/concerns/models/target_spec.rb +579 -0
  37. data/spec/concerns/renderable_spec.rb +110 -0
  38. data/spec/controllers/notifications_controller_shared_examples.rb +457 -0
  39. data/spec/controllers/notifications_controller_spec.rb +12 -0
  40. data/spec/controllers/notifications_with_devise_controller_spec.rb +81 -0
  41. data/spec/factories/admins.rb +5 -0
  42. data/spec/factories/articles.rb +1 -1
  43. data/spec/factories/comments.rb +1 -1
  44. data/spec/factories/dummy/dummy_notifiable.rb +4 -0
  45. data/spec/factories/dummy/dummy_notifier.rb +4 -0
  46. data/spec/factories/dummy/dummy_target.rb +4 -0
  47. data/spec/factories/notifications.rb +1 -1
  48. data/spec/factories/users.rb +7 -1
  49. data/spec/generators/active_record/migration_generator_spec.rb +41 -0
  50. data/spec/generators/controllers_generator_spec.rb +62 -0
  51. data/spec/generators/install_generator_spec.rb +43 -0
  52. data/spec/generators/models/notification_generator_spec.rb +41 -0
  53. data/spec/generators/views_generator_spec.rb +111 -0
  54. data/spec/helpers/polymorphic_helpers_spec.rb +89 -0
  55. data/spec/helpers/view_helpers_spec.rb +258 -0
  56. data/spec/mailers/mailer_spec.rb +98 -0
  57. data/spec/models/dummy/dummy_notifiable_spec.rb +6 -0
  58. data/spec/models/dummy/dummy_notifier_spec.rb +6 -0
  59. data/spec/models/dummy/dummy_target_spec.rb +6 -0
  60. data/spec/models/notification_spec.rb +5 -4
  61. data/spec/rails_app/app/assets/javascripts/application.js +2 -0
  62. data/spec/rails_app/app/controllers/articles_controller.rb +62 -0
  63. data/spec/rails_app/app/controllers/comments_controller.rb +34 -0
  64. data/spec/rails_app/app/models/admin.rb +8 -0
  65. data/spec/rails_app/app/models/article.rb +6 -6
  66. data/spec/rails_app/app/models/comment.rb +2 -2
  67. data/spec/rails_app/app/models/dummy/dummy_base.rb +2 -0
  68. data/spec/rails_app/app/models/dummy/dummy_notifiable.rb +4 -0
  69. data/spec/rails_app/app/models/dummy/dummy_notifier.rb +4 -0
  70. data/spec/rails_app/app/models/dummy/dummy_target.rb +4 -0
  71. data/spec/rails_app/app/models/user.rb +5 -5
  72. data/spec/rails_app/app/views/activity_notification/notifications/default/custom/_path_test.html.erb +1 -0
  73. data/spec/rails_app/app/views/activity_notification/notifications/default/custom/_test.html.erb +1 -0
  74. data/spec/rails_app/app/views/activity_notification/notifications/users/_custom_index.html.erb +1 -0
  75. data/spec/rails_app/app/views/activity_notification/notifications/users/custom/_test.html.erb +1 -0
  76. data/spec/rails_app/app/views/articles/_form.html.erb +20 -0
  77. data/spec/rails_app/app/views/articles/edit.html.erb +6 -0
  78. data/spec/rails_app/app/views/articles/index.html.erb +67 -0
  79. data/spec/rails_app/app/views/articles/new.html.erb +5 -0
  80. data/spec/rails_app/app/views/articles/show.html.erb +38 -0
  81. data/spec/rails_app/app/views/layouts/_header.html.erb +8 -0
  82. data/spec/rails_app/app/views/layouts/application.html.erb +3 -4
  83. data/spec/rails_app/config/initializers/activity_notification.rb +13 -3
  84. data/spec/rails_app/config/initializers/devise.rb +274 -274
  85. data/spec/rails_app/config/locales/activity_notification.en.yml +20 -0
  86. data/spec/rails_app/config/locales/devise.en.yml +62 -0
  87. data/spec/rails_app/config/routes.rb +6 -2
  88. data/spec/rails_app/db/migrate/20160715050433_create_test_tables.rb +6 -2
  89. data/spec/rails_app/db/schema.rb +8 -0
  90. data/spec/rails_app/db/seeds.rb +43 -0
  91. data/spec/roles/acts_as_notifiable_spec.rb +32 -0
  92. data/spec/roles/acts_as_notifier_spec.rb +17 -0
  93. data/spec/roles/acts_as_target_spec.rb +40 -0
  94. data/spec/spec_helper.rb +18 -14
  95. metadata +136 -12
@@ -3,7 +3,7 @@ require 'rails/generators/base'
3
3
  module ActivityNotification
4
4
  module Generators
5
5
  class ControllersGenerator < Rails::Generators::Base
6
- CONTROLLERS = %w(notifications notifications_with_devise).freeze
6
+ CONTROLLERS = ['notifications', 'notifications_with_devise'].freeze
7
7
 
8
8
  desc <<-DESC.strip_heredoc
9
9
  Create inherited ActivityNotification controllers in your app/controllers folder.
@@ -6,7 +6,7 @@ module ActivityNotification
6
6
 
7
7
  class InstallGenerator < Rails::Generators::Base
8
8
  source_root File.expand_path("../../templates", __FILE__)
9
- ``
9
+
10
10
  desc "Creates a ActivityNotification initializer and copy locale files to your application."
11
11
  class_option :orm
12
12
 
@@ -14,7 +14,7 @@ module ActivityNotification
14
14
 
15
15
  #TODO suport other orm e.g. mongoid
16
16
  unless options[:orm] == :active_record
17
- raise MissingORMError, <<-ERROR.strip_heredoc
17
+ raise TypeError, <<-ERROR.strip_heredoc
18
18
  Currently ActivityNotification is only supported with Active Record ORM.
19
19
 
20
20
  Be sure to have an Active Record ORM loaded in your
@@ -30,16 +30,13 @@ module ActivityNotification
30
30
  end
31
31
 
32
32
  def copy_locale
33
- copy_file "../../../config/locales/en.yml", "config/locales/activity_notification.en.yml"
33
+ template "locales/en.yml", "config/locales/activity_notification.en.yml"
34
34
  end
35
35
 
36
36
  def show_readme
37
37
  readme "README" if behavior == :invoke
38
38
  end
39
39
 
40
- def rails_4?
41
- Rails::VERSION::MAJOR == 4
42
- end
43
40
  end
44
41
  end
45
42
  end
@@ -6,11 +6,12 @@ module ActivityNotification
6
6
  class NotificationGenerator < ActiveRecord::Generators::Base
7
7
  source_root File.expand_path("../../../templates/notification", __FILE__)
8
8
 
9
- argument :name, type: :string, default: 'notification'
9
+ argument :name, type: :string, default: 'Notification'
10
10
 
11
- # Create model in project's folder
12
- def generate_files
13
- copy_file 'notification.rb', "app/models/#{name}.rb"
11
+ # Create model in application directory
12
+ def create_models
13
+ @model_name = name
14
+ template 'notification.rb', "app/models/#{name.underscore}.rb"
14
15
  end
15
16
  end
16
17
  end
@@ -3,41 +3,39 @@ require 'rails/generators/base'
3
3
  module ActivityNotification
4
4
  module Generators
5
5
  # Include this module in your generator to generate ActivityNotification views.
6
- # `copy_views` is the main method and by default copies all views
7
- # with forms.
6
+ # `copy_views` is the main method and by default copies all views of ActivityNotification.
8
7
  class ViewsGenerator < Rails::Generators::Base
8
+ VIEWS = [:notifications, :mailer].freeze
9
+
9
10
  source_root File.expand_path("../../../../app/views/activity_notification", __FILE__)
10
11
  desc "Copies default ActivityNotification views to your application."
11
12
 
12
13
  argument :target, required: false, default: nil,
13
- desc: "The target to copy views to"
14
- class_option :views, aliases: "-v", type: :array, desc: "Select specific view directories to generate (notifications, mailer)"
14
+ desc: "The target to copy views to"
15
+ class_option :views, aliases: "-v", type: :array,
16
+ desc: "Select specific view directories to generate (notifications, mailer)"
15
17
  public_task :copy_views
16
18
 
17
19
  def copy_views
18
- if options[:views]
19
- options[:views].each do |directory|
20
- view_directory directory.to_sym
21
- end
22
- else
23
- view_directory :notifications
24
- view_directory :mailer
20
+ target_views = options[:views] || VIEWS
21
+ target_views.each do |directory|
22
+ view_directory directory.to_sym
25
23
  end
26
24
  end
27
25
 
28
26
  protected
29
27
 
30
- def view_directory(name, _target_path = nil)
31
- directory "#{name.to_s}/default", _target_path || "#{target_path}/#{name}/#{plural_target || :default}"
32
- end
33
-
34
- def target_path
35
- @target_path ||= "app/views/activity_notification"
36
- end
37
-
38
- def plural_target
39
- @plural_target ||= target.presence && target.to_s.underscore.pluralize
40
- end
28
+ def view_directory(name, _target_path = nil)
29
+ directory "#{name.to_s}/default", _target_path || "#{target_path}/#{name}/#{plural_target || :default}"
30
+ end
31
+
32
+ def target_path
33
+ @target_path ||= "app/views/activity_notification"
34
+ end
35
+
36
+ def plural_target
37
+ @plural_target ||= target.presence && target.to_s.underscore.pluralize
38
+ end
41
39
  end
42
40
 
43
41
  end
@@ -1,5 +1,5 @@
1
1
  # Migration responsible for creating a table with notifications
2
- class CreateNotifications < ActiveRecord::Migration
2
+ class <%= @migration_name %> < ActiveRecord::Migration
3
3
  # Create table
4
4
  def change
5
5
  create_table :notifications do |t|
@@ -1,9 +1,17 @@
1
1
  ActivityNotification.configure do |config|
2
2
 
3
- # Table name to store notification data
3
+ # Configure if all activity notifications are enabled
4
+ # Set false when you want to turn off activity notifications
5
+ config.enabled = true
6
+
7
+ # Configure table name to store notification data
4
8
  config.table_name = "notifications"
5
9
 
6
- # ==> Mailer Configuration
10
+ # Configure if email notification is enabled as default
11
+ # Note that you can configure them for each model by acts_as roles.
12
+ # Set true when you want to turn on email notifications as default
13
+ config.email_enabled = false
14
+
7
15
  # Configure the e-mail address which will be shown in ActivityNotification::Mailer,
8
16
  # note that it will be overwritten if you use your own mailer class with default "from" parameter.
9
17
  config.mailer_sender = 'please-change-me-at-config-initializers-activity_notification@example.com'
@@ -13,6 +21,8 @@ ActivityNotification.configure do |config|
13
21
 
14
22
  # Configure the parent class responsible to send e-mails.
15
23
  # config.parent_mailer = 'ActionMailer::Base'
16
-
24
+
25
+ # Configure default limit number of opened notifications you can get from opened* scope
17
26
  config.opened_limit = 10
27
+
18
28
  end
File without changes
@@ -1,3 +1,6 @@
1
1
  # Notification model for customisation & custom methods
2
- class Notification < ActivityNotification::Notification
2
+ class <%= @model_name %> < ActivityNotification::Notification
3
+
4
+ # Write custom methods or override methods here
5
+
3
6
  end
@@ -1,7 +1,7 @@
1
1
  shared_examples_for :notification_api do
2
2
  include ActiveJob::TestHelper
3
3
  let(:test_class_name) { described_class.to_s.underscore.split('/').last.to_sym }
4
- let(:test_instance) { create(test_class_name, target: create(:user, confirmed_at: DateTime.now)) }
4
+ let(:test_instance) { create(test_class_name) }
5
5
  before do
6
6
  ActiveJob::Base.queue_adapter = :test
7
7
  ActivityNotification::Mailer.deliveries.clear
@@ -11,9 +11,9 @@ shared_examples_for :notification_api do
11
11
  describe "as public class methods" do
12
12
  before do
13
13
  described_class.delete_all
14
- @author_user = create(:user, confirmed_at: DateTime.now)
15
- @user_1 = create(:user, confirmed_at: DateTime.now)
16
- @user_2 = create(:user, confirmed_at: DateTime.now)
14
+ @author_user = create(:confirmed_user)
15
+ @user_1 = create(:confirmed_user)
16
+ @user_2 = create(:confirmed_user)
17
17
  @article = create(:article, user: @author_user)
18
18
  @comment_1 = create(:comment, article: @article, user: @user_1)
19
19
  @comment_2 = create(:comment, article: @article, user: @user_2)
@@ -22,9 +22,22 @@ shared_examples_for :notification_api do
22
22
  expect(@user_2.notifications.count).to eq(0)
23
23
  end
24
24
 
25
- describe "notify" do
25
+ describe "#notify" do
26
+ it "returns array of created notifications" do
27
+ notifications = described_class.notify(:users, @comment_2)
28
+ expect(notifications).to be_a Array
29
+ expect(notifications.size).to eq(2)
30
+ if notifications[0].target == @author_user
31
+ validate_expected_notification(notifications[0], @author_user, @comment_2)
32
+ validate_expected_notification(notifications[1], @user_1, @comment_2)
33
+ else
34
+ validate_expected_notification(notifications[0], @user_1, @comment_2)
35
+ validate_expected_notification(notifications[1], @author_user, @comment_2)
36
+ end
37
+ end
38
+
26
39
  it "creates notification records" do
27
- described_class.notify(User, @comment_2)
40
+ described_class.notify(:users, @comment_2)
28
41
  expect(@author_user.notifications.unopened_only.count).to eq(1)
29
42
  expect(@user_1.notifications.unopened_only.count).to eq(1)
30
43
  expect(@user_2.notifications.unopened_only.count).to eq(0)
@@ -34,7 +47,7 @@ shared_examples_for :notification_api do
34
47
  it "sends notification email later" do
35
48
  expect {
36
49
  perform_enqueued_jobs do
37
- described_class.notify(User, @comment_2)
50
+ described_class.notify(:users, @comment_2)
38
51
  end
39
52
  }.to change { ActivityNotification::Mailer.deliveries.size }.by(2)
40
53
  expect(ActivityNotification::Mailer.deliveries.size).to eq(2)
@@ -44,14 +57,14 @@ shared_examples_for :notification_api do
44
57
 
45
58
  it "sends notification email with active job queue" do
46
59
  expect {
47
- described_class.notify(User, @comment_2)
60
+ described_class.notify(:users, @comment_2)
48
61
  }.to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :size).by(2)
49
62
  end
50
63
  end
51
64
 
52
65
  context "with send_later false" do
53
66
  it "sends notification email now" do
54
- described_class.notify(User, @comment_2, send_later: false)
67
+ described_class.notify(:users, @comment_2, send_later: false)
55
68
  expect(ActivityNotification::Mailer.deliveries.size).to eq(2)
56
69
  expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(@user_1.email)
57
70
  expect(ActivityNotification::Mailer.deliveries.last.to[0]).to eq(@author_user.email)
@@ -59,7 +72,15 @@ shared_examples_for :notification_api do
59
72
  end
60
73
  end
61
74
 
62
- describe "notify_all" do
75
+ describe "#notify_all" do
76
+ it "returns array of created notifications" do
77
+ notifications = described_class.notify_all([@author_user, @user_1], @comment_2)
78
+ expect(notifications).to be_a Array
79
+ expect(notifications.size).to eq(2)
80
+ validate_expected_notification(notifications[0], @author_user, @comment_2)
81
+ validate_expected_notification(notifications[1], @user_1, @comment_2)
82
+ end
83
+
63
84
  it "creates notification records" do
64
85
  described_class.notify_all([@author_user, @user_1], @comment_2)
65
86
  expect(@author_user.notifications.unopened_only.count).to eq(1)
@@ -96,7 +117,12 @@ shared_examples_for :notification_api do
96
117
  end
97
118
  end
98
119
 
99
- describe "notify_to" do
120
+ describe "#notify_to" do
121
+ it "returns reated notification" do
122
+ notification = described_class.notify_to(@user_1, @comment_2)
123
+ validate_expected_notification(notification, @user_1, @comment_2)
124
+ end
125
+
100
126
  it "creates notification records" do
101
127
  described_class.notify_to(@user_1, @comment_2)
102
128
  expect(@user_1.notifications.unopened_only.count).to eq(1)
@@ -276,7 +302,7 @@ shared_examples_for :notification_api do
276
302
  end
277
303
  end
278
304
 
279
- describe "open_all_of" do
305
+ describe "#open_all_of" do
280
306
  before do
281
307
  described_class.notify_to(@user_1, @comment_2)
282
308
  described_class.notify_to(@user_1, @comment_2)
@@ -284,6 +310,10 @@ shared_examples_for :notification_api do
284
310
  expect(@user_1.notifications.opened_only!.count).to eq(0)
285
311
  end
286
312
 
313
+ it "returns the number of opened notification records" do
314
+ expect(described_class.open_all_of(@user_1)).to eq(2)
315
+ end
316
+
287
317
  it "opens all notifications of the target" do
288
318
  described_class.open_all_of(@user_1)
289
319
  expect(@user_1.notifications.unopened_only.count).to eq(0)
@@ -297,10 +327,10 @@ shared_examples_for :notification_api do
297
327
  end
298
328
  end
299
329
 
300
- describe "group_member_exists?" do
330
+ describe "#group_member_exists?" do
301
331
  context "when specified notifications have any group members" do
302
332
  let(:owner_notifications) do
303
- target = create(:user)
333
+ target = create(:confirmed_user)
304
334
  group_owner = create(:notification, target: target, group_owner: nil)
305
335
  create(:notification, target: target, group_owner: nil)
306
336
  group_member = create(:notification, target: target, group_owner: group_owner)
@@ -315,7 +345,7 @@ shared_examples_for :notification_api do
315
345
 
316
346
  context "when specified notifications have no group members" do
317
347
  let(:owner_notifications) do
318
- target = create(:user)
348
+ target = create(:confirmed_user)
319
349
  group_owner = create(:notification, target: target, group_owner: nil)
320
350
  create(:notification, target: target, group_owner: nil)
321
351
  target.notifications.group_owners_only
@@ -328,8 +358,8 @@ shared_examples_for :notification_api do
328
358
  end
329
359
  end
330
360
 
331
- describe "available_options" do
332
- it "returns list of available_options" do
361
+ describe "#available_options" do
362
+ it "returns list of available options in notify api" do
333
363
  expect(described_class.available_options)
334
364
  .to eq([:key, :group, :parameters, :notifier, :send_email, :send_later])
335
365
  end
@@ -337,7 +367,7 @@ shared_examples_for :notification_api do
337
367
  end
338
368
 
339
369
  describe "as private class methods" do
340
- describe "store_notification" do
370
+ describe "#store_notification" do
341
371
  it "is defined as private method" do
342
372
  expect(described_class.respond_to?(:store_notification)).to be_falsey
343
373
  expect(described_class.respond_to?(:store_notification, true)).to be_truthy
@@ -346,7 +376,7 @@ shared_examples_for :notification_api do
346
376
  end
347
377
 
348
378
  describe "as public instance methods" do
349
- describe "send_notification_email" do
379
+ describe "#send_notification_email" do
350
380
  context "as default" do
351
381
  it "sends notification email later" do
352
382
  expect(ActivityNotification::Mailer.deliveries.size).to eq(0)
@@ -376,7 +406,20 @@ shared_examples_for :notification_api do
376
406
  end
377
407
  end
378
408
 
379
- describe "open!" do
409
+ describe "#open!" do
410
+ before do
411
+ described_class.delete_all
412
+ end
413
+
414
+ it "returns the number of opened notification records" do
415
+ expect(test_instance.open!).to eq(1)
416
+ end
417
+
418
+ it "returns the number of opened notification records including group members" do
419
+ create(test_class_name, group_owner: test_instance, opened_at: nil)
420
+ expect(test_instance.open!).to eq(2)
421
+ end
422
+
380
423
  context "as default" do
381
424
  it "open notification with current time" do
382
425
  expect(test_instance.opened_at.blank?).to be_truthy
@@ -386,6 +429,18 @@ shared_examples_for :notification_api do
386
429
  expect(test_instance.opened_at).to eq(DateTime.now)
387
430
  Timecop.return
388
431
  end
432
+
433
+ #TODO
434
+ # it "open group member notifications with current time" do
435
+ # group_member = create(test_class_name, group_owner: test_instance)
436
+ # expect(group_member.opened_at.blank?).to be_truthy
437
+ # Timecop.freeze(DateTime.now)
438
+ # test_instance.open!
439
+ # group_member = group_member.reload
440
+ # expect(group_member.opened_at.blank?).to be_falsey
441
+ # expect(group_member.opened_at).to eq(DateTime.now)
442
+ # Timecop.return
443
+ # end
389
444
  end
390
445
 
391
446
  context "with an argument" do
@@ -396,10 +451,21 @@ shared_examples_for :notification_api do
396
451
  expect(test_instance.opened_at.blank?).to be_falsey
397
452
  expect(test_instance.opened_at).to eq(datetime)
398
453
  end
454
+
455
+ #TODO
456
+ # it "open group member notifications with current time" do
457
+ # group_member = create(test_class_name, group_owner: test_instance)
458
+ # expect(group_member.opened_at.blank?).to be_truthy
459
+ # datetime = DateTime.now - 1.months
460
+ # test_instance.open!(datetime)
461
+ # group_member = group_member.reload
462
+ # expect(group_member.opened_at.blank?).to be_falsey
463
+ # expect(group_member.opened_at).to eq(datetime)
464
+ # end
399
465
  end
400
466
  end
401
467
 
402
- describe "unopened?" do
468
+ describe "#unopened?" do
403
469
  context "when opened_at is blank" do
404
470
  it "returns true" do
405
471
  expect(test_instance.unopened?).to be_truthy
@@ -414,7 +480,7 @@ shared_examples_for :notification_api do
414
480
  end
415
481
  end
416
482
 
417
- describe "opened?" do
483
+ describe "#opened?" do
418
484
  context "when opened_at is blank" do
419
485
  it "returns false" do
420
486
  expect(test_instance.opened?).to be_falsey
@@ -429,7 +495,7 @@ shared_examples_for :notification_api do
429
495
  end
430
496
  end
431
497
 
432
- describe "group_owner?" do
498
+ describe "#group_owner?" do
433
499
  context "when the notification is group owner" do
434
500
  it "returns true" do
435
501
  expect(test_instance.group_owner?).to be_truthy
@@ -444,7 +510,7 @@ shared_examples_for :notification_api do
444
510
  end
445
511
  end
446
512
 
447
- describe "group_member?" do
513
+ describe "#group_member?" do
448
514
  context "when the notification is group owner" do
449
515
  it "returns false" do
450
516
  expect(test_instance.group_member?).to be_falsey
@@ -459,7 +525,7 @@ shared_examples_for :notification_api do
459
525
  end
460
526
  end
461
527
 
462
- describe "group_member_exists?" do
528
+ describe "#group_member_exists?" do
463
529
  context "when the notification is group owner and has no group members" do
464
530
  it "returns false" do
465
531
  expect(test_instance.group_member_exists?).to be_falsey
@@ -481,46 +547,97 @@ shared_examples_for :notification_api do
481
547
  end
482
548
  end
483
549
 
484
- describe "group_member_count" do
485
- context "when the notification is group owner and has no group members" do
486
- it "returns 0" do
487
- expect(test_instance.group_member_count).to eq(0)
550
+ describe "#group_member_count" do
551
+ context "for unopened notification" do
552
+ context "when the notification is group owner and has no group members" do
553
+ it "returns 0" do
554
+ expect(test_instance.group_member_count).to eq(0)
555
+ end
488
556
  end
489
- end
490
557
 
491
- context "when the notification is group owner and has group members" do
492
- it "returns member count" do
493
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
494
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
495
- expect(test_instance.group_member_count).to eq(2)
558
+ context "when the notification is group owner and has group members" do
559
+ it "returns member count" do
560
+ create(test_class_name, target: test_instance.target, group_owner: test_instance)
561
+ create(test_class_name, target: test_instance.target, group_owner: test_instance)
562
+ expect(test_instance.group_member_count).to eq(2)
563
+ end
564
+ end
565
+
566
+ context "when the notification belongs to group" do
567
+ it "returns member count" do
568
+ group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance)
569
+ create(test_class_name, target: test_instance.target, group_owner: test_instance)
570
+ expect(group_member.group_member_count).to eq(2)
571
+ end
496
572
  end
497
573
  end
498
574
 
499
- context "when the notification belongs to group" do
500
- it "returns member count" do
501
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance)
502
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
503
- expect(group_member.group_member_count).to eq(2)
575
+ context "for opened notification" do
576
+ context "when the notification is group owner and has no group members" do
577
+ it "returns 0" do
578
+ test_instance.open!
579
+ expect(test_instance.group_member_count).to eq(0)
580
+ end
581
+ end
582
+
583
+ context "as default" do
584
+ context "when the notification is group owner and has group members" do
585
+ it "returns member count" do
586
+ create(test_class_name, target: test_instance.target, group_owner: test_instance)
587
+ create(test_class_name, target: test_instance.target, group_owner: test_instance)
588
+ test_instance.open!
589
+ expect(test_instance.group_member_count).to eq(2)
590
+ end
591
+ end
592
+
593
+ context "when the notification belongs to group" do
594
+ it "returns member count" do
595
+ group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance)
596
+ create(test_class_name, target: test_instance.target, group_owner: test_instance)
597
+ test_instance.open!
598
+ expect(group_member.group_member_count).to eq(2)
599
+ end
600
+ end
601
+ end
602
+
603
+ context "with limit" do
604
+ context "when the notification is group owner and has group members" do
605
+ it "returns member count by limit" do
606
+ create(test_class_name, target: test_instance.target, group_owner: test_instance)
607
+ create(test_class_name, target: test_instance.target, group_owner: test_instance)
608
+ test_instance.open!
609
+ expect(test_instance.group_member_count(0)).to eq(0)
610
+ end
611
+ end
612
+
613
+ context "when the notification belongs to group" do
614
+ it "returns member count by limit" do
615
+ group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance)
616
+ create(test_class_name, target: test_instance.target, group_owner: test_instance)
617
+ test_instance.open!
618
+ expect(group_member.group_member_count(0)).to eq(0)
619
+ end
620
+ end
504
621
  end
505
622
  end
506
623
  end
507
624
 
508
- describe "notifiale_path" do
625
+ describe "#notifiable_path" do
509
626
  it "returns notifiable.notifiable_path" do
510
- expect(test_instance.notifiale_path).to eq(test_instance.notifiable.notifiable_path(test_instance.target_type))
627
+ expect(test_instance.notifiable_path).to eq(test_instance.notifiable.notifiable_path(test_instance.target_type))
511
628
  end
512
629
  end
513
630
  end
514
631
 
515
- describe "with protected instance methods" do
516
- describe "unopened_group_member_count" do
632
+ describe "as protected instance methods" do
633
+ describe "#unopened_group_member_count" do
517
634
  it "is defined as protected method" do
518
635
  expect(test_instance.respond_to?(:unopened_group_member_count)).to be_falsey
519
636
  expect(test_instance.respond_to?(:unopened_group_member_count, true)).to be_truthy
520
637
  end
521
638
  end
522
639
 
523
- describe "opened_group_member_count" do
640
+ describe "#opened_group_member_count" do
524
641
  it "is defined as protected method" do
525
642
  expect(test_instance.respond_to?(:opened_group_member_count)).to be_falsey
526
643
  expect(test_instance.respond_to?(:opened_group_member_count, true)).to be_truthy
@@ -528,4 +645,11 @@ shared_examples_for :notification_api do
528
645
  end
529
646
  end
530
647
 
648
+ private
649
+ def validate_expected_notification(notification, target, notifiable)
650
+ expect(notification).to be_a described_class
651
+ expect(notification.target).to eq(target)
652
+ expect(notification.notifiable).to eq(notifiable)
653
+ end
654
+
531
655
  end