active_job_channel 0.0.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '058d0b861b22420aa23b79986d3d73bb01cbe27d7279ef706382edb7a4d47ba5'
4
- data.tar.gz: 79aaf5fbd4416e22a93c9a14353ef41c7d6965d58670d4815e3af6f86b88f726
3
+ metadata.gz: e15e3cd0deda9ddda3a0d594f125554938b14233444b6f7f6f53ae24cbb3bbac
4
+ data.tar.gz: 448bfe9ff104ebc3f489de2fe8f95591fcca443efef647d63c264717187542ba
5
5
  SHA512:
6
- metadata.gz: 5f12183f81a90854fb76a584d93b96ef6e656f41b4ff0926ef6a242b275b14fe3e6172abd830264ee425732016fafc4b448476b3b32ba15d76f4f7c900b8ea96
7
- data.tar.gz: b5e178a2ed8ac074fd4350d9f884373e142aa0ea73712570e3e10d86efc196f70eb4b083d604e78d8a4aec277b844d91bdb8aa2217634ed8274ff49813416bd0
6
+ metadata.gz: 52748705d9f2f217353138fc768910c5e2ef5daa4df099fc9f9e2a6a863c82df66dfadadc1d8a8307a8bc719a0fbf2bb1d5a6d336e7f20458787810e1a18762c
7
+ data.tar.gz: 56dad2e05f5e500f228c0b0c1ee89d53ad3064d54946ab599a0613706f940ce60b34d91ae70b4674871b85fd804788ff398776d50b23bd2c118615fc26f295d9
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Gem Version](https://badge.fury.io/rb/active_job_channel.svg)](https://badge.fury.io/rb/active_job_channel)
2
+
1
3
  # ActiveJobChannel
2
4
  Use ActionCable to alert front-end users of finished ActiveJobs
3
5
 
data/Rakefile CHANGED
@@ -11,10 +11,19 @@ Bundler::GemHelper.install_tasks
11
11
 
12
12
  Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each { |f| load f }
13
13
 
14
+ # bundler-audit task
15
+ require 'bundler/audit/task'
16
+ Bundler::Audit::Task.new
17
+
18
+ # RuboCop task
19
+ require 'rubocop/rake_task'
20
+ desc 'Run all specs in spec directory (excluding plugin specs)'
21
+ RuboCop::RakeTask.new
22
+
23
+ # RSpec Task
14
24
  require 'rspec/core'
15
25
  require 'rspec/core/rake_task'
16
-
17
26
  desc 'Run all specs in spec directory (excluding plugin specs)'
18
- RSpec::Core::RakeTask.new(:spec)
27
+ RSpec::Core::RakeTask.new
19
28
 
20
- task default: :spec
29
+ task default: %i[bundle:audit rubocop spec]
@@ -20,7 +20,7 @@ module ActiveJobChannel
20
20
  def active_job_channel(options = {})
21
21
  class_attribute :ajc_config
22
22
  self.ajc_config = { global_broadcast: false }
23
- self.ajc_config.merge!(options)
23
+ ajc_config.merge!(options)
24
24
 
25
25
  after_perform :broadcast_success
26
26
  rescue_from '::StandardError' do |exception|
@@ -1,3 +1,3 @@
1
1
  module ActiveJobChannel
2
- VERSION = '0.0.3'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
@@ -0,0 +1,145 @@
1
+ require 'active_job_channel/broadcaster'
2
+ require 'spec_helper'
3
+
4
+ module ActiveJobChannel
5
+ module Broadcaster
6
+ RSpec.describe ClassMethods do
7
+ before(:each) do
8
+ class DummyJob < ActiveJob::Base
9
+ active_job_channel global_broadcast: true
10
+ def perform
11
+ fake
12
+ end
13
+
14
+ private
15
+
16
+ def fake; end
17
+ end
18
+ DummyJob.extend(described_class)
19
+ end
20
+
21
+ describe '.active_job_channel' do
22
+ it 'sets ajc_config with options hash' do
23
+ DummyJob.active_job_channel
24
+ expect(DummyJob.ajc_config).to eq(global_broadcast: false)
25
+
26
+ DummyJob.active_job_channel global_broadcast: true
27
+ expect(DummyJob.ajc_config).to eq(global_broadcast: true)
28
+ end
29
+ end
30
+
31
+ describe '#broadcast_failure' do
32
+ it 'handles an error' do
33
+ allow_any_instance_of(DummyJob).
34
+ to receive(:fake).
35
+ and_raise(StandardError)
36
+ expect_any_instance_of(DummyJob).
37
+ to receive(:broadcast_failure).
38
+ with(StandardError).
39
+ and_call_original
40
+
41
+ action_cable_server = double 'action_cable_server'
42
+ allow(ActionCable).to receive(:server).and_return(action_cable_server)
43
+ expect(action_cable_server).
44
+ to receive(:broadcast).
45
+ with(
46
+ ::ActiveJobChannel::Channel::CHANNEL_NAME,
47
+ status: 'failure',
48
+ job_name: DummyJob.to_s,
49
+ error: StandardError.new.inspect
50
+ )
51
+
52
+ expect { DummyJob.perform_now }.to raise_error(StandardError)
53
+ end
54
+
55
+ context 'with ajc_identifier' do
56
+ it 'broadcast failure to the identifier' do
57
+ DummyJob.active_job_channel global_broadcast: false
58
+ ajc_identifier = 'ajc_identifier'
59
+ allow_any_instance_of(DummyJob).
60
+ to receive(:fake).
61
+ and_raise(StandardError)
62
+ allow_any_instance_of(DummyJob).
63
+ to receive(:ajc_identifier).
64
+ and_return(ajc_identifier)
65
+ expect_any_instance_of(DummyJob).
66
+ to receive(:broadcast_failure).
67
+ with(StandardError).
68
+ and_call_original
69
+
70
+ action_cable_server = double 'action_cable_server'
71
+ allow(ActionCable).
72
+ to receive(:server).
73
+ and_return(action_cable_server)
74
+ expect(action_cable_server).
75
+ to receive(:broadcast).
76
+ with(
77
+ "#{::ActiveJobChannel::Channel::CHANNEL_NAME}#" \
78
+ "#{ajc_identifier}",
79
+ status: 'failure',
80
+ job_name: DummyJob.to_s,
81
+ error: StandardError.new.inspect
82
+ )
83
+
84
+ expect { DummyJob.perform_now }.to raise_error(StandardError)
85
+ end
86
+ end
87
+ end
88
+
89
+ describe '#broadcast_success' do
90
+ it 'broadcasts' do
91
+ expect_any_instance_of(DummyJob).
92
+ to receive(:broadcast_success).
93
+ and_call_original
94
+
95
+ action_cable_server = double 'action_cable_server'
96
+ allow(ActionCable).to receive(:server).and_return(action_cable_server)
97
+ expect(action_cable_server).
98
+ to receive(:broadcast).
99
+ with(
100
+ ::ActiveJobChannel::Channel::CHANNEL_NAME,
101
+ status: 'success',
102
+ job_name: DummyJob.to_s
103
+ )
104
+
105
+ DummyJob.perform_now
106
+ end
107
+
108
+ context 'with ajc_identifier' do
109
+ it 'broadcast success to the identifier' do
110
+ DummyJob.active_job_channel global_broadcast: false
111
+ ajc_identifier = 'ajc_identifier'
112
+ allow_any_instance_of(DummyJob).
113
+ to receive(:ajc_identifier).
114
+ and_return(ajc_identifier)
115
+ expect_any_instance_of(DummyJob).
116
+ to receive(:broadcast_success).
117
+ and_call_original
118
+
119
+ action_cable_server = double 'action_cable_server'
120
+ allow(ActionCable).
121
+ to receive(:server).
122
+ and_return(action_cable_server)
123
+ expect(action_cable_server).
124
+ to receive(:broadcast).
125
+ with(
126
+ "#{::ActiveJobChannel::Channel::CHANNEL_NAME}#" \
127
+ "#{ajc_identifier}",
128
+ status: 'success',
129
+ job_name: DummyJob.to_s
130
+ )
131
+
132
+ DummyJob.perform_now
133
+ end
134
+ end
135
+ end
136
+
137
+ describe 'NoIdentifierError' do
138
+ it 'is raised when ajc_identifier is missing for private broadcast' do
139
+ DummyJob.active_job_channel global_broadcast: false
140
+ expect { DummyJob.perform_now }.to raise_error(NoIdentifierError)
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -1,7 +1,7 @@
1
1
  class ApplicationJob < ActiveJob::Base
2
2
  active_job_channel global_broadcast: true
3
3
 
4
- def perform(identifier)
4
+ def perform(identifier = nil)
5
5
  @ajc_identifier = identifier
6
6
  fake
7
7
  end