activity_notification 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop.yml +1 -1
  4. data/CHANGELOG.md +23 -11
  5. data/Gemfile.lock +52 -40
  6. data/README.md +177 -14
  7. data/activity_notification.gemspec +3 -1
  8. data/app/controllers/activity_notification/subscriptions_controller.rb +48 -2
  9. data/app/views/activity_notification/optional_targets/default/base/_default.text.erb +10 -0
  10. data/app/views/activity_notification/optional_targets/default/slack/_default.text.erb +6 -0
  11. data/app/views/activity_notification/subscriptions/default/_notification_keys.html.erb +19 -0
  12. data/app/views/activity_notification/subscriptions/default/_subscription.html.erb +32 -3
  13. data/app/views/activity_notification/subscriptions/default/index.html.erb +4 -0
  14. data/app/views/activity_notification/subscriptions/default/show.html.erb +5 -0
  15. data/app/views/activity_notification/subscriptions/default/subscribe_to_optional_target.js.erb +6 -0
  16. data/app/views/activity_notification/subscriptions/default/unsubscribe_to_optional_target.js.erb +6 -0
  17. data/gemfiles/Gemfile.rails-4.2.lock +15 -3
  18. data/gemfiles/Gemfile.rails-5.0.lock +47 -35
  19. data/lib/activity_notification.rb +1 -0
  20. data/lib/activity_notification/apis/notification_api.rb +83 -26
  21. data/lib/activity_notification/apis/subscription_api.rb +93 -8
  22. data/lib/activity_notification/common.rb +6 -2
  23. data/lib/activity_notification/helpers/view_helpers.rb +43 -0
  24. data/lib/activity_notification/models/concerns/notifiable.rb +73 -28
  25. data/lib/activity_notification/models/concerns/subscriber.rb +34 -7
  26. data/lib/activity_notification/models/concerns/target.rb +25 -13
  27. data/lib/activity_notification/models/subscription.rb +13 -2
  28. data/lib/activity_notification/optional_targets/amazon_sns.rb +42 -0
  29. data/lib/activity_notification/optional_targets/base.rb +79 -0
  30. data/lib/activity_notification/optional_targets/slack.rb +33 -0
  31. data/lib/activity_notification/rails/routes.rb +30 -20
  32. data/lib/activity_notification/roles/acts_as_notifiable.rb +70 -11
  33. data/lib/activity_notification/version.rb +1 -1
  34. data/lib/generators/activity_notification/views_generator.rb +2 -2
  35. data/lib/generators/templates/migrations/migration.rb +2 -2
  36. data/spec/concerns/apis/notification_api_spec.rb +97 -0
  37. data/spec/concerns/apis/subscription_api_spec.rb +206 -41
  38. data/spec/concerns/common_spec.rb +7 -2
  39. data/spec/concerns/models/notifiable_spec.rb +88 -2
  40. data/spec/concerns/models/subscriber_spec.rb +114 -13
  41. data/spec/concerns/models/target_spec.rb +17 -0
  42. data/spec/controllers/subscriptions_controller_shared_examples.rb +251 -28
  43. data/spec/helpers/view_helpers_spec.rb +56 -0
  44. data/spec/optional_targets/amazon_sns_spec.rb +46 -0
  45. data/spec/optional_targets/base_spec.rb +43 -0
  46. data/spec/optional_targets/slack_spec.rb +46 -0
  47. data/spec/rails_app/app/controllers/comments_controller.rb +1 -0
  48. data/spec/rails_app/app/models/admin.rb +1 -2
  49. data/spec/rails_app/app/models/article.rb +2 -3
  50. data/spec/rails_app/app/models/comment.rb +19 -7
  51. data/spec/rails_app/app/views/activity_notification/optional_targets/admins/amazon_sns/comment/_default.text.erb +8 -0
  52. data/spec/rails_app/db/migrate/20160715050420_create_activity_notification_tables.rb +1 -1
  53. data/spec/rails_app/db/migrate/20160715050433_create_test_tables.rb +2 -0
  54. data/spec/rails_app/db/schema.rb +3 -1
  55. data/spec/rails_app/db/seeds.rb +1 -1
  56. data/spec/rails_app/lib/custom_optional_targets/console_output.rb +13 -0
  57. data/spec/rails_app/lib/custom_optional_targets/wrong_target.rb +10 -0
  58. data/spec/roles/acts_as_notifiable_spec.rb +124 -2
  59. metadata +49 -4
  60. data/spec/rails_app/app/models/notification.rb +0 -6
@@ -48,12 +48,17 @@ shared_examples_for :common do
48
48
  end
49
49
 
50
50
  context "with Proc" do
51
- it "returns specified lambda with context(model) arguments" do
51
+ it "returns specified lambda without argument" do
52
+ test_proc = ->{ 1 }
53
+ expect(ActivityNotification.resolve_value(test_instance, test_proc)).to eq(1)
54
+ end
55
+
56
+ it "returns specified lambda with context(model) arguments" do
52
57
  test_proc = ->(model){ model == test_instance ? 1 : 0 }
53
58
  expect(ActivityNotification.resolve_value(test_instance, test_proc)).to eq(1)
54
59
  end
55
60
 
56
- it "returns specified lambda with controller and context(model) arguments" do
61
+ it "returns specified lambda with controller and context(model) arguments" do
57
62
  test_proc = ->(controller, model){ controller == 'StubController' and model == test_instance ? 1 : 0 }
58
63
  expect(ActivityNotification.resolve_value(test_instance, test_proc)).to eq(1)
59
64
  end
@@ -493,13 +493,99 @@ shared_examples_for :notifiable do
493
493
  expect(test_instance.printable_notifiable_name(test_target, 'dummy_key')).to eq('test_printable_name')
494
494
  end
495
495
 
496
- it "returns specified lambda with single target argument" do
497
- described_class._printable_notifiable_name[:users] = ->(target){ 'test_printable_name' }
496
+ it "returns specified lambda with notifiable, target and key argument" do
497
+ described_class._printable_notifiable_name[:users] = ->(notifiable, target, key){ 'test_printable_name' }
498
498
  expect(test_instance.printable_notifiable_name(test_target, 'dummy_key')).to eq('test_printable_name')
499
499
  end
500
500
  end
501
501
  end
502
502
 
503
+ describe "#optional_targets" do
504
+ require 'custom_optional_targets/console_output'
505
+
506
+ context "without any configuration" do
507
+ it "returns blank array" do
508
+ expect(test_instance.optional_targets(test_target, 'dummy_key')).to eq([])
509
+ end
510
+ end
511
+
512
+ context "configured with a field" do
513
+ before do
514
+ @optional_target_instance = CustomOptionalTarget::ConsoleOutput.new
515
+ end
516
+
517
+ it "returns specified value" do
518
+ described_class._optional_targets[:users] = [@optional_target_instance]
519
+ expect(test_instance.optional_targets(User, 'dummy_key')).to eq([@optional_target_instance])
520
+ end
521
+
522
+ it "returns specified symbol of method" do
523
+ module AdditionalMethods
524
+ def custom_optional_targets
525
+ [ActivityNotification::OptionalTarget::Base.new]
526
+ end
527
+ end
528
+ test_instance.extend(AdditionalMethods)
529
+ described_class._optional_targets[:users] = :custom_optional_targets
530
+ expect(test_instance.optional_targets(User, 'dummy_key').size).to eq(1)
531
+ expect(test_instance.optional_targets(User, 'dummy_key').first).to be_a(ActivityNotification::OptionalTarget::Base)
532
+ end
533
+
534
+ it "returns specified lambda with no arguments" do
535
+ described_class._optional_targets[:users] = ->{ [ActivityNotification::OptionalTarget::Base.new] }
536
+ expect(test_instance.optional_targets(User, 'dummy_key').first).to be_a(ActivityNotification::OptionalTarget::Base)
537
+ end
538
+
539
+ it "returns specified lambda with notifiable and key argument" do
540
+ described_class._optional_targets[:users] = ->(notifiable, key){ key == 'dummy_key' ? [ActivityNotification::OptionalTarget::Base.new] : [] }
541
+ expect(test_instance.optional_targets(User)).to eq([])
542
+ expect(test_instance.optional_targets(User, 'dummy_key').first).to be_a(ActivityNotification::OptionalTarget::Base)
543
+ end
544
+ end
545
+ end
546
+
547
+ describe "#optional_target_names" do
548
+ require 'custom_optional_targets/console_output'
549
+
550
+ context "without any configuration" do
551
+ it "returns blank array" do
552
+ expect(test_instance.optional_target_names(test_target, 'dummy_key')).to eq([])
553
+ end
554
+ end
555
+
556
+ context "configured with a field" do
557
+ before do
558
+ @optional_target_instance = CustomOptionalTarget::ConsoleOutput.new
559
+ end
560
+
561
+ it "returns specified value" do
562
+ described_class._optional_targets[:users] = [@optional_target_instance]
563
+ expect(test_instance.optional_target_names(User, 'dummy_key')).to eq([:console_output])
564
+ end
565
+
566
+ it "returns specified symbol of method" do
567
+ module AdditionalMethods
568
+ def custom_optional_targets
569
+ [ActivityNotification::OptionalTarget::Base.new]
570
+ end
571
+ end
572
+ test_instance.extend(AdditionalMethods)
573
+ described_class._optional_targets[:users] = :custom_optional_targets
574
+ expect(test_instance.optional_target_names(User, 'dummy_key')).to eq([:base])
575
+ end
576
+
577
+ it "returns specified lambda with no arguments" do
578
+ described_class._optional_targets[:users] = ->{ [@optional_target_instance] }
579
+ expect(test_instance.optional_target_names(User, 'dummy_key')).to eq([:console_output])
580
+ end
581
+
582
+ it "returns specified lambda with notifiable and key argument" do
583
+ described_class._optional_targets[:users] = ->(notifiable, key){ key == 'dummy_key' ? [@optional_target_instance] : [] }
584
+ expect(test_instance.optional_target_names(User, 'dummy_key')).to eq([:console_output])
585
+ end
586
+ end
587
+ end
588
+
503
589
  describe "#notify" do
504
590
  it "is an alias of ActivityNotification::Notification.notify" do
505
591
  expect(ActivityNotification::Notification).to receive(:notify)
@@ -89,8 +89,8 @@ shared_examples_for :subscriber do
89
89
  it "creates a new subscription" do
90
90
  params = { key: 'key_1' }
91
91
  new_subscription = test_instance.create_subscription(params)
92
- expect(new_subscription.subscribing).to be_truthy
93
- expect(new_subscription.subscribing_to_email).to be_truthy
92
+ expect(new_subscription.subscribing?).to be_truthy
93
+ expect(new_subscription.subscribing_to_email?).to be_truthy
94
94
  expect(test_instance.subscriptions.reload.size).to eq(1)
95
95
  end
96
96
  end
@@ -99,8 +99,8 @@ shared_examples_for :subscriber do
99
99
  it "creates a new subscription" do
100
100
  params = { key: 'key_1', subscribing: false }
101
101
  new_subscription = test_instance.create_subscription(params)
102
- expect(new_subscription.subscribing).to be_falsey
103
- expect(new_subscription.subscribing_to_email).to be_falsey
102
+ expect(new_subscription.subscribing?).to be_falsey
103
+ expect(new_subscription.subscribing_to_email?).to be_falsey
104
104
  expect(test_instance.subscriptions.reload.size).to eq(1)
105
105
  end
106
106
  end
@@ -109,8 +109,8 @@ shared_examples_for :subscriber do
109
109
  it "creates a new subscription" do
110
110
  params = { key: 'key_1', subscribing_to_email: false }
111
111
  new_subscription = test_instance.create_subscription(params)
112
- expect(new_subscription.subscribing).to be_truthy
113
- expect(new_subscription.subscribing_to_email).to be_falsey
112
+ expect(new_subscription.subscribing?).to be_truthy
113
+ expect(new_subscription.subscribing_to_email?).to be_falsey
114
114
  expect(test_instance.subscriptions.reload.size).to eq(1)
115
115
  end
116
116
  end
@@ -119,8 +119,8 @@ shared_examples_for :subscriber do
119
119
  it "creates a new subscription" do
120
120
  params = { key: 'key_1', subscribing: true, subscribing_to_email: false }
121
121
  new_subscription = test_instance.create_subscription(params)
122
- expect(new_subscription.subscribing).to be_truthy
123
- expect(new_subscription.subscribing_to_email).to be_falsey
122
+ expect(new_subscription.subscribing?).to be_truthy
123
+ expect(new_subscription.subscribing_to_email?).to be_falsey
124
124
  expect(test_instance.subscriptions.reload.size).to eq(1)
125
125
  end
126
126
  end
@@ -133,6 +133,47 @@ shared_examples_for :subscriber do
133
133
  expect(test_instance.subscriptions.reload).to be_empty
134
134
  end
135
135
  end
136
+
137
+
138
+
139
+ context "with true as optional_targets params" do
140
+ it "creates a new subscription" do
141
+ params = { key: 'key_1', optional_targets: { subscribing_to_console_output: true } }
142
+ new_subscription = test_instance.create_subscription(params)
143
+ expect(new_subscription.subscribing?).to be_truthy
144
+ expect(new_subscription.subscribing_to_optional_target?(:console_output)).to be_truthy
145
+ expect(test_instance.subscriptions.reload.size).to eq(1)
146
+ end
147
+ end
148
+
149
+ context "with false as optional_targets params" do
150
+ it "creates a new subscription" do
151
+ params = { key: 'key_1', optional_targets: { subscribing_to_console_output: false } }
152
+ new_subscription = test_instance.create_subscription(params)
153
+ expect(new_subscription.subscribing?).to be_truthy
154
+ expect(new_subscription.subscribing_to_optional_target?(:console_output)).to be_falsey
155
+ expect(test_instance.subscriptions.reload.size).to eq(1)
156
+ end
157
+ end
158
+
159
+ context "with true as subscribing and false as optional_targets params" do
160
+ it "creates a new subscription" do
161
+ params = { key: 'key_1', subscribing: true, optional_targets: { subscribing_to_console_output: false } }
162
+ new_subscription = test_instance.create_subscription(params)
163
+ expect(new_subscription.subscribing?).to be_truthy
164
+ expect(new_subscription.subscribing_to_optional_target?(:console_output)).to be_falsey
165
+ expect(test_instance.subscriptions.reload.size).to eq(1)
166
+ end
167
+ end
168
+
169
+ context "with false as subscribing and true as optional_targets params" do
170
+ it "does not create a new subscription since it is invalid" do
171
+ params = { key: 'key_1', subscribing: false, optional_targets: { subscribing_to_console_output: true } }
172
+ new_subscription = test_instance.create_subscription(params)
173
+ expect(new_subscription).to be_nil
174
+ expect(test_instance.subscriptions.reload).to be_empty
175
+ end
176
+ end
136
177
  end
137
178
 
138
179
  describe "#subscription_index" do
@@ -446,7 +487,7 @@ shared_examples_for :subscriber do
446
487
  context "subscribing to notification" do
447
488
  it "returns true" do
448
489
  subscription = test_instance.create_subscription(key: @test_key)
449
- expect(subscription.subscribing).to be_truthy
490
+ expect(subscription.subscribing?).to be_truthy
450
491
  expect(test_instance.subscribes_to_notification?(@test_key)).to be_truthy
451
492
  end
452
493
  end
@@ -454,7 +495,7 @@ shared_examples_for :subscriber do
454
495
  context "unsubscribed to notification" do
455
496
  it "returns false" do
456
497
  subscription = test_instance.create_subscription(key: @test_key, subscribing: false)
457
- expect(subscription.subscribing).to be_falsey
498
+ expect(subscription.subscribing?).to be_falsey
458
499
  expect(test_instance.subscribes_to_notification?(@test_key)).to be_falsey
459
500
  end
460
501
  end
@@ -505,15 +546,15 @@ shared_examples_for :subscriber do
505
546
  context "subscribing to notification email" do
506
547
  it "returns true" do
507
548
  subscription = test_instance.create_subscription(key: @test_key)
508
- expect(subscription.subscribing_to_email).to be_truthy
549
+ expect(subscription.subscribing_to_email?).to be_truthy
509
550
  expect(test_instance.subscribes_to_notification_email?(@test_key)).to be_truthy
510
551
  end
511
552
  end
512
553
 
513
- context "unsubscribed to notification" do
554
+ context "unsubscribed to notification email" do
514
555
  it "returns false" do
515
556
  subscription = test_instance.create_subscription(key: @test_key, subscribing: true, subscribing_to_email: false)
516
- expect(subscription.subscribing_to_email).to be_falsey
557
+ expect(subscription.subscribing_to_email?).to be_falsey
517
558
  expect(test_instance.subscribes_to_notification_email?(@test_key)).to be_falsey
518
559
  end
519
560
  end
@@ -521,5 +562,65 @@ shared_examples_for :subscriber do
521
562
  end
522
563
  end
523
564
 
565
+ describe "#subscribes_to_optional_target?" do
566
+ before do
567
+ @test_key = 'test_key'
568
+ @optional_target_name = :console_output
569
+ end
570
+
571
+ context "when the subscription is not enabled for the target" do
572
+ it "returns true" do
573
+ described_class._notification_subscription_allowed = false
574
+ expect(test_instance.subscribes_to_optional_target?(@test_key, @optional_target_name)).to be_truthy
575
+ end
576
+ end
577
+
578
+ context "when the subscription is enabled for the target" do
579
+ before do
580
+ described_class._notification_subscription_allowed = true
581
+ end
582
+
583
+ context "without configured subscpriotion" do
584
+ context "without subscribe_as_default argument" do
585
+ context "with true as ActivityNotification.config.subscribe_as_default" do
586
+ it "returns true" do
587
+ subscribe_as_default = ActivityNotification.config.subscribe_as_default
588
+ ActivityNotification.config.subscribe_as_default = true
589
+ expect(test_instance.subscribes_to_optional_target?(@test_key, @optional_target_name)).to be_truthy
590
+ ActivityNotification.config.subscribe_as_default = subscribe_as_default
591
+ end
592
+ end
593
+
594
+ context "with false as ActivityNotification.config.subscribe_as_default" do
595
+ it "returns false" do
596
+ subscribe_as_default = ActivityNotification.config.subscribe_as_default
597
+ ActivityNotification.config.subscribe_as_default = false
598
+ expect(test_instance.subscribes_to_optional_target?(@test_key, @optional_target_name)).to be_falsey
599
+ ActivityNotification.config.subscribe_as_default = subscribe_as_default
600
+ end
601
+ end
602
+ end
603
+ end
604
+
605
+ context "with configured subscpriotion" do
606
+ context "subscribing to the specified optional target" do
607
+ it "returns true" do
608
+ subscription = test_instance.create_subscription(key: @test_key, optional_targets: { ActivityNotification::Subscription.to_optional_target_key(@optional_target_name) => true })
609
+ expect(subscription.subscribing_to_optional_target?(@optional_target_name)).to be_truthy
610
+ expect(test_instance.subscribes_to_optional_target?(@test_key, @optional_target_name)).to be_truthy
611
+ end
612
+ end
613
+
614
+ context "unsubscribed to the specified optional target" do
615
+ it "returns false" do
616
+ subscription = test_instance.create_subscription(key: @test_key, subscribing: true, optional_targets: { ActivityNotification::Subscription.to_optional_target_key(@optional_target_name) => false })
617
+ expect(subscription.subscribing_to_optional_target?(@optional_target_name)).to be_falsey
618
+ expect(test_instance.subscribes_to_optional_target?(@test_key, @optional_target_name)).to be_falsey
619
+ end
620
+ end
621
+ end
622
+ end
623
+ end
624
+
524
625
  end
525
626
  end
@@ -1113,6 +1113,23 @@ shared_examples_for :target do
1113
1113
  end
1114
1114
  end
1115
1115
  end
1116
+
1117
+ describe "#subscribes_to_optional_target?" do
1118
+ context "when the subscription is not enabled for the target" do
1119
+ it "returns true" do
1120
+ described_class._notification_subscription_allowed = false
1121
+ expect(test_instance.subscribes_to_optional_target?('test_key', :slack)).to be_truthy
1122
+ end
1123
+ end
1124
+
1125
+ context "when the subscription is enabled for the target" do
1126
+ it "calls Subscriber#_subscribes_to_optional_target?" do
1127
+ described_class._notification_subscription_allowed = true
1128
+ expect(test_instance).to receive(:_subscribes_to_optional_target?)
1129
+ test_instance.subscribes_to_optional_target?('test_key', :slack)
1130
+ end
1131
+ end
1132
+ end
1116
1133
  end
1117
1134
 
1118
1135
  end
@@ -220,7 +220,7 @@ shared_examples_for :subscription_controller do
220
220
  expect(test_target.subscriptions.size).to eq(0)
221
221
  end
222
222
 
223
- context "http direct POST request" do
223
+ context "http direct POST request without optional targets" do
224
224
  before do
225
225
  post_with_compatibility :create, target_params.merge({
226
226
  typed_target_param => test_target,
@@ -245,6 +245,34 @@ shared_examples_for :subscription_controller do
245
245
  end
246
246
  end
247
247
 
248
+ context "http direct POST request with optional targets" do
249
+ before do
250
+ post_with_compatibility :create, target_params.merge({
251
+ typed_target_param => test_target,
252
+ "subscription" => { "key" => "new_subscription_key",
253
+ "subscribing"=> "true",
254
+ "subscribing_to_email"=>"true",
255
+ "optional_targets" => { "subscribing_to_base1" => "true", "subscribing_to_base2" => "false" }
256
+ }
257
+ }), valid_session
258
+ end
259
+
260
+ it "returns 302 as http status code" do
261
+ expect(response.status).to eq(302)
262
+ end
263
+
264
+ it "creates new subscription of the target" do
265
+ expect(test_target.subscriptions.reload.size).to eq(1)
266
+ expect(test_target.subscriptions.reload.first.key).to eq("new_subscription_key")
267
+ expect(test_target.subscriptions.reload.first.subscribing_to_optional_target?("base1")).to be_truthy
268
+ expect(test_target.subscriptions.reload.first.subscribing_to_optional_target?("base2")).to be_falsey
269
+ end
270
+
271
+ it "redirects to :index" do
272
+ expect(response).to redirect_to action: :index
273
+ end
274
+ end
275
+
248
276
  context "http POST request from root_path" do
249
277
  before do
250
278
  request.env["HTTP_REFERER"] = root_path
@@ -403,7 +431,7 @@ shared_examples_for :subscription_controller do
403
431
  before do
404
432
  @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
405
433
  @subscription.unsubscribe
406
- expect(@subscription.subscribing).to be_falsey
434
+ expect(@subscription.subscribing?).to be_falsey
407
435
  post_with_compatibility :subscribe, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
408
436
  end
409
437
 
@@ -412,7 +440,7 @@ shared_examples_for :subscription_controller do
412
440
  end
413
441
 
414
442
  it "updates subscribing to true" do
415
- expect(@subscription.reload.subscribing).to be_truthy
443
+ expect(@subscription.reload.subscribing?).to be_truthy
416
444
  end
417
445
 
418
446
  it "redirects to :index" do
@@ -424,7 +452,7 @@ shared_examples_for :subscription_controller do
424
452
  before do
425
453
  @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
426
454
  @subscription.unsubscribe
427
- expect(@subscription.subscribing).to be_falsey
455
+ expect(@subscription.subscribing?).to be_falsey
428
456
  request.env["HTTP_REFERER"] = root_path
429
457
  post_with_compatibility :subscribe, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
430
458
  end
@@ -434,7 +462,7 @@ shared_examples_for :subscription_controller do
434
462
  end
435
463
 
436
464
  it "updates subscribing to true" do
437
- expect(@subscription.reload.subscribing).to be_truthy
465
+ expect(@subscription.reload.subscribing?).to be_truthy
438
466
  end
439
467
 
440
468
  it "redirects to root_path as request.referer" do
@@ -446,7 +474,7 @@ shared_examples_for :subscription_controller do
446
474
  before do
447
475
  @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
448
476
  @subscription.unsubscribe
449
- expect(@subscription.subscribing).to be_falsey
477
+ expect(@subscription.subscribing?).to be_falsey
450
478
  request.env["HTTP_REFERER"] = root_path
451
479
  xhr_with_compatibility :post, :subscribe, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
452
480
  end
@@ -460,7 +488,7 @@ shared_examples_for :subscription_controller do
460
488
  end
461
489
 
462
490
  it "updates subscribing to true" do
463
- expect(@subscription.reload.subscribing).to be_truthy
491
+ expect(@subscription.reload.subscribing?).to be_truthy
464
492
  end
465
493
 
466
494
  it "renders the :open template as format js" do
@@ -473,7 +501,7 @@ shared_examples_for :subscription_controller do
473
501
  context "http direct POST request" do
474
502
  before do
475
503
  @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
476
- expect(@subscription.subscribing).to be_truthy
504
+ expect(@subscription.subscribing?).to be_truthy
477
505
  post_with_compatibility :unsubscribe, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
478
506
  end
479
507
 
@@ -482,7 +510,7 @@ shared_examples_for :subscription_controller do
482
510
  end
483
511
 
484
512
  it "updates subscribing to false" do
485
- expect(@subscription.reload.subscribing).to be_falsey
513
+ expect(@subscription.reload.subscribing?).to be_falsey
486
514
  end
487
515
 
488
516
  it "redirects to :index" do
@@ -493,7 +521,7 @@ shared_examples_for :subscription_controller do
493
521
  context "http POST request from root_path" do
494
522
  before do
495
523
  @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
496
- expect(@subscription.subscribing).to be_truthy
524
+ expect(@subscription.subscribing?).to be_truthy
497
525
  request.env["HTTP_REFERER"] = root_path
498
526
  post_with_compatibility :unsubscribe, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
499
527
  end
@@ -503,7 +531,7 @@ shared_examples_for :subscription_controller do
503
531
  end
504
532
 
505
533
  it "updates subscribing to false" do
506
- expect(@subscription.reload.subscribing).to be_falsey
534
+ expect(@subscription.reload.subscribing?).to be_falsey
507
535
  end
508
536
 
509
537
  it "redirects to root_path as request.referer" do
@@ -514,7 +542,7 @@ shared_examples_for :subscription_controller do
514
542
  context "Ajax POST request" do
515
543
  before do
516
544
  @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
517
- expect(@subscription.subscribing).to be_truthy
545
+ expect(@subscription.subscribing?).to be_truthy
518
546
  request.env["HTTP_REFERER"] = root_path
519
547
  xhr_with_compatibility :post, :unsubscribe, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
520
548
  end
@@ -528,7 +556,7 @@ shared_examples_for :subscription_controller do
528
556
  end
529
557
 
530
558
  it "updates subscribing to false" do
531
- expect(@subscription.reload.subscribing).to be_falsey
559
+ expect(@subscription.reload.subscribing?).to be_falsey
532
560
  end
533
561
 
534
562
  it "renders the :open template as format js" do
@@ -542,7 +570,7 @@ shared_examples_for :subscription_controller do
542
570
  before do
543
571
  @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
544
572
  @subscription.unsubscribe_to_email
545
- expect(@subscription.subscribing_to_email).to be_falsey
573
+ expect(@subscription.subscribing_to_email?).to be_falsey
546
574
  post_with_compatibility :subscribe_to_email, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
547
575
  end
548
576
 
@@ -551,7 +579,7 @@ shared_examples_for :subscription_controller do
551
579
  end
552
580
 
553
581
  it "updates subscribing_to_email to true" do
554
- expect(@subscription.reload.subscribing_to_email).to be_truthy
582
+ expect(@subscription.reload.subscribing_to_email?).to be_truthy
555
583
  end
556
584
 
557
585
  it "redirects to :index" do
@@ -563,7 +591,7 @@ shared_examples_for :subscription_controller do
563
591
  before do
564
592
  @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
565
593
  @subscription.unsubscribe_to_email
566
- expect(@subscription.subscribing_to_email).to be_falsey
594
+ expect(@subscription.subscribing_to_email?).to be_falsey
567
595
  request.env["HTTP_REFERER"] = root_path
568
596
  post_with_compatibility :subscribe_to_email, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
569
597
  end
@@ -573,7 +601,7 @@ shared_examples_for :subscription_controller do
573
601
  end
574
602
 
575
603
  it "updates subscribing_to_email to true" do
576
- expect(@subscription.reload.subscribing_to_email).to be_truthy
604
+ expect(@subscription.reload.subscribing_to_email?).to be_truthy
577
605
  end
578
606
 
579
607
  it "redirects to root_path as request.referer" do
@@ -585,7 +613,7 @@ shared_examples_for :subscription_controller do
585
613
  before do
586
614
  @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
587
615
  @subscription.unsubscribe_to_email
588
- expect(@subscription.subscribing_to_email).to be_falsey
616
+ expect(@subscription.subscribing_to_email?).to be_falsey
589
617
  request.env["HTTP_REFERER"] = root_path
590
618
  xhr_with_compatibility :post, :subscribe_to_email, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
591
619
  end
@@ -599,7 +627,7 @@ shared_examples_for :subscription_controller do
599
627
  end
600
628
 
601
629
  it "updates subscribing_to_email to true" do
602
- expect(@subscription.reload.subscribing_to_email).to be_truthy
630
+ expect(@subscription.reload.subscribing_to_email?).to be_truthy
603
631
  end
604
632
 
605
633
  it "renders the :open template as format js" do
@@ -611,8 +639,8 @@ shared_examples_for :subscription_controller do
611
639
  before do
612
640
  @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
613
641
  @subscription.unsubscribe
614
- expect(@subscription.subscribing).to be_falsey
615
- expect(@subscription.subscribing_to_email).to be_falsey
642
+ expect(@subscription.subscribing?).to be_falsey
643
+ expect(@subscription.subscribing_to_email?).to be_falsey
616
644
  post_with_compatibility :subscribe_to_email, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
617
645
  end
618
646
 
@@ -621,7 +649,7 @@ shared_examples_for :subscription_controller do
621
649
  end
622
650
 
623
651
  it "cannot update subscribing_to_email to true" do
624
- expect(@subscription.reload.subscribing_to_email).to be_falsey
652
+ expect(@subscription.reload.subscribing_to_email?).to be_falsey
625
653
  end
626
654
 
627
655
  it "redirects to :index" do
@@ -634,7 +662,7 @@ shared_examples_for :subscription_controller do
634
662
  context "http direct POST request" do
635
663
  before do
636
664
  @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
637
- expect(@subscription.subscribing_to_email).to be_truthy
665
+ expect(@subscription.subscribing_to_email?).to be_truthy
638
666
  post_with_compatibility :unsubscribe_to_email, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
639
667
  end
640
668
 
@@ -643,7 +671,7 @@ shared_examples_for :subscription_controller do
643
671
  end
644
672
 
645
673
  it "updates subscribing_to_email to false" do
646
- expect(@subscription.reload.subscribing_to_email).to be_falsey
674
+ expect(@subscription.reload.subscribing_to_email?).to be_falsey
647
675
  end
648
676
 
649
677
  it "redirects to :index" do
@@ -654,7 +682,7 @@ shared_examples_for :subscription_controller do
654
682
  context "http POST request from root_path" do
655
683
  before do
656
684
  @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
657
- expect(@subscription.subscribing_to_email).to be_truthy
685
+ expect(@subscription.subscribing_to_email?).to be_truthy
658
686
  request.env["HTTP_REFERER"] = root_path
659
687
  post_with_compatibility :unsubscribe_to_email, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
660
688
  end
@@ -664,7 +692,7 @@ shared_examples_for :subscription_controller do
664
692
  end
665
693
 
666
694
  it "updates subscribing_to_email to false" do
667
- expect(@subscription.reload.subscribing_to_email).to be_falsey
695
+ expect(@subscription.reload.subscribing_to_email?).to be_falsey
668
696
  end
669
697
 
670
698
  it "redirects to root_path as request.referer" do
@@ -675,7 +703,7 @@ shared_examples_for :subscription_controller do
675
703
  context "Ajax POST request" do
676
704
  before do
677
705
  @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
678
- expect(@subscription.subscribing_to_email).to be_truthy
706
+ expect(@subscription.subscribing_to_email?).to be_truthy
679
707
  request.env["HTTP_REFERER"] = root_path
680
708
  xhr_with_compatibility :post, :unsubscribe_to_email, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
681
709
  end
@@ -689,7 +717,7 @@ shared_examples_for :subscription_controller do
689
717
  end
690
718
 
691
719
  it "updates subscribing_to_email to false" do
692
- expect(@subscription.reload.subscribing_to_email).to be_falsey
720
+ expect(@subscription.reload.subscribing_to_email?).to be_falsey
693
721
  end
694
722
 
695
723
  it "renders the :open template as format js" do
@@ -698,6 +726,201 @@ shared_examples_for :subscription_controller do
698
726
  end
699
727
  end
700
728
 
729
+ describe "POST #subscribe_to_optional_target" do
730
+ context "without optional_target_name param" do
731
+ before do
732
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
733
+ @subscription.unsubscribe_to_optional_target(:base)
734
+ expect(@subscription.subscribing_to_optional_target?(:base)).to be_falsey
735
+ post_with_compatibility :subscribe_to_optional_target, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
736
+ end
737
+
738
+ it "returns 400 as http status code" do
739
+ expect(response.status).to eq(400)
740
+ end
741
+
742
+ it "does not update subscribing_to_optional_target?" do
743
+ expect(@subscription.subscribing_to_optional_target?(:base)).to be_falsey
744
+ end
745
+ end
746
+
747
+ context "http direct POST request" do
748
+ before do
749
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
750
+ @subscription.unsubscribe_to_optional_target(:base)
751
+ expect(@subscription.subscribing_to_optional_target?(:base)).to be_falsey
752
+ post_with_compatibility :subscribe_to_optional_target, target_params.merge({ id: @subscription, optional_target_name: 'base', typed_target_param => test_target }), valid_session
753
+ end
754
+
755
+ it "returns 302 as http status code" do
756
+ expect(response.status).to eq(302)
757
+ end
758
+
759
+ it "updates subscribing_to_optional_target to true" do
760
+ expect(@subscription.reload.subscribing_to_optional_target?(:base)).to be_truthy
761
+ end
762
+
763
+ it "redirects to :index" do
764
+ expect(response).to redirect_to action: :index
765
+ end
766
+ end
767
+
768
+ context "http POST request from root_path" do
769
+ before do
770
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
771
+ @subscription.unsubscribe_to_optional_target(:base)
772
+ expect(@subscription.subscribing_to_optional_target?(:base)).to be_falsey
773
+ request.env["HTTP_REFERER"] = root_path
774
+ post_with_compatibility :subscribe_to_optional_target, target_params.merge({ id: @subscription, optional_target_name: 'base', typed_target_param => test_target }), valid_session
775
+ end
776
+
777
+ it "returns 302 as http status code" do
778
+ expect(response.status).to eq(302)
779
+ end
780
+
781
+ it "updates subscribing_to_optional_target to true" do
782
+ expect(@subscription.reload.subscribing_to_optional_target?(:base)).to be_truthy
783
+ end
784
+
785
+ it "redirects to root_path as request.referer" do
786
+ expect(response).to redirect_to root_path
787
+ end
788
+ end
789
+
790
+ context "Ajax POST request" do
791
+ before do
792
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
793
+ @subscription.unsubscribe_to_optional_target(:base)
794
+ expect(@subscription.subscribing_to_optional_target?(:base)).to be_falsey
795
+ request.env["HTTP_REFERER"] = root_path
796
+ xhr_with_compatibility :post, :subscribe_to_optional_target, target_params.merge({ id: @subscription, optional_target_name: 'base', typed_target_param => test_target }), valid_session
797
+ end
798
+
799
+ it "returns 200 as http status code" do
800
+ expect(response.status).to eq(200)
801
+ end
802
+
803
+ it "assigns subscription index as @subscriptions" do
804
+ expect(assigns(:subscriptions)).to eq([@subscription])
805
+ end
806
+
807
+ it "updates subscribing_to_optional_target to true" do
808
+ expect(@subscription.reload.subscribing_to_optional_target?(:base)).to be_truthy
809
+ end
810
+
811
+ it "renders the :open template as format js" do
812
+ expect(response).to render_template :subscribe_to_optional_target, format: :js
813
+ end
814
+ end
815
+
816
+ context "with unsubscribed target" do
817
+ before do
818
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
819
+ @subscription.unsubscribe_to_optional_target(:base)
820
+ @subscription.unsubscribe
821
+ expect(@subscription.subscribing?).to be_falsey
822
+ expect(@subscription.subscribing_to_optional_target?(:base)).to be_falsey
823
+ post_with_compatibility :subscribe_to_optional_target, target_params.merge({ id: @subscription, optional_target_name: 'base', typed_target_param => test_target }), valid_session
824
+ end
825
+
826
+ it "returns 302 as http status code" do
827
+ expect(response.status).to eq(302)
828
+ end
829
+
830
+ it "cannot update subscribing_to_optional_target to true" do
831
+ expect(@subscription.reload.subscribing_to_optional_target?(:base)).to be_falsey
832
+ end
833
+
834
+ it "redirects to :index" do
835
+ expect(response).to redirect_to action: :index
836
+ end
837
+ end
838
+ end
839
+
840
+ describe "POST #unsubscribe_to_email" do
841
+ context "without optional_target_name param" do
842
+ before do
843
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
844
+ expect(@subscription.subscribing_to_optional_target?(:base)).to be_truthy
845
+ post_with_compatibility :unsubscribe_to_optional_target, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
846
+ end
847
+
848
+ it "returns 400 as http status code" do
849
+ expect(response.status).to eq(400)
850
+ end
851
+
852
+ it "does not update subscribing_to_optional_target?" do
853
+ expect(@subscription.subscribing_to_optional_target?(:base)).to be_truthy
854
+ end
855
+ end
856
+
857
+ context "http direct POST request" do
858
+ before do
859
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
860
+ expect(@subscription.subscribing_to_optional_target?(:base)).to be_truthy
861
+ post_with_compatibility :unsubscribe_to_optional_target, target_params.merge({ id: @subscription, optional_target_name: 'base', typed_target_param => test_target }), valid_session
862
+ end
863
+
864
+ it "returns 302 as http status code" do
865
+ expect(response.status).to eq(302)
866
+ end
867
+
868
+ it "updates subscribing_to_optional_target to false" do
869
+ expect(@subscription.reload.subscribing_to_optional_target?(:base)).to be_falsey
870
+ end
871
+
872
+ it "redirects to :index" do
873
+ expect(response).to redirect_to action: :index
874
+ end
875
+ end
876
+
877
+ context "http POST request from root_path" do
878
+ before do
879
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
880
+ expect(@subscription.subscribing_to_optional_target?(:base)).to be_truthy
881
+ request.env["HTTP_REFERER"] = root_path
882
+ post_with_compatibility :unsubscribe_to_optional_target, target_params.merge({ id: @subscription, optional_target_name: 'base', typed_target_param => test_target }), valid_session
883
+ end
884
+
885
+ it "returns 302 as http status code" do
886
+ expect(response.status).to eq(302)
887
+ end
888
+
889
+ it "updates subscribing_to_optional_target to false" do
890
+ expect(@subscription.reload.subscribing_to_optional_target?(:base)).to be_falsey
891
+ end
892
+
893
+ it "redirects to root_path as request.referer" do
894
+ expect(response).to redirect_to root_path
895
+ end
896
+ end
897
+
898
+ context "Ajax POST request" do
899
+ before do
900
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
901
+ expect(@subscription.subscribing_to_optional_target?(:base)).to be_truthy
902
+ request.env["HTTP_REFERER"] = root_path
903
+ xhr_with_compatibility :post, :unsubscribe_to_optional_target, target_params.merge({ id: @subscription, optional_target_name: 'base', typed_target_param => test_target }), valid_session
904
+ end
905
+
906
+ it "returns 200 as http status code" do
907
+ expect(response.status).to eq(200)
908
+ end
909
+
910
+ it "assigns subscription index as @subscriptions" do
911
+ expect(assigns(:subscriptions)).to eq([@subscription])
912
+ end
913
+
914
+ it "updates subscribing_to_optional_target to false" do
915
+ expect(@subscription.reload.subscribing_to_optional_target?(:base)).to be_falsey
916
+ end
917
+
918
+ it "renders the :open template as format js" do
919
+ expect(response).to render_template :unsubscribe_to_optional_target, format: :js
920
+ end
921
+ end
922
+ end
923
+
701
924
 
702
925
  private
703
926