wisper-activerecord-publisher 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f481f3a7e215a299ad8852ed13ca58015e567a26
4
- data.tar.gz: d9103ee22d6a60efc68694c7ebceac390516acff
2
+ SHA256:
3
+ metadata.gz: 164a0ca8671589cf020e6de23cc9fb349c0ef9be1ea90ebe0fbe6e8f3d4fc4c6
4
+ data.tar.gz: a0224dc694ee505e116f0a8b3d5debec80b4ca5abcb73c5aba66f7493f7a701f
5
5
  SHA512:
6
- metadata.gz: d91617efd5bec3e16f0533d28fa1e37b27f0d50e872bfededbe884b1c9745bcc5c5f6d8ac5b9e47771c92270ce7f998051d898588f0ca9a1c9f55afe7a74f408
7
- data.tar.gz: 61ba29218c5f18db8a79e9c3c1f592a430d56148589774f8fd827339e2ba998c6fac893e91e762d31ae2695c793f59f9c35f7777f735f4c76f17042e74f42f01
6
+ metadata.gz: f0225ec11f2609585aef571208e0ac74fe2370b1b01db4c400b39e7107bd904ce439f88398d45575c3877d7e4a5fe4157be1a7180b314d565ad1affe9cb6d25e
7
+ data.tar.gz: 27378a8026b2ca278433477c225d567be2569f76c30995a38d13642f20a828ba54c01cf2b2a75669975b819f0f8503353166520b39aee7620b71eec156e0426d
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.3.1
1
+ 3.1.3
data/README.md CHANGED
@@ -26,7 +26,7 @@ This gem *does* support background event processing since the events are
26
26
  published after commit and each event includes the necessary payload (ie:
27
27
  changes).
28
28
 
29
- ## Example
29
+ ### Example
30
30
 
31
31
  ```ruby
32
32
  class Foo < ActiveRecord::Base
@@ -47,6 +47,45 @@ class MyListener
47
47
  end
48
48
  ```
49
49
 
50
+ ### Limiting events per model
51
+
52
+ Rather than have a model broadcast all events, you can limit them using the `broadcast_on` method.
53
+
54
+ ```ruby
55
+ class Foo < ActiveRecord::Base
56
+ broadcast_on :create
57
+ end
58
+
59
+ class MyListener
60
+ def foo_created(model)
61
+ # do something here with the model
62
+ end
63
+
64
+ def foo_updated(model, changes)
65
+ # Will not be called
66
+ end
67
+
68
+ def foo_destroyed(model_attributes)
69
+ # Will not be called
70
+ end
71
+ end
72
+ ```
73
+
74
+ ### Changing default event broadcasts
75
+
76
+ You can turn off or limit broadcasts for all models in your application via an initializer.
77
+
78
+ ```ruby
79
+ # config/initializers/wisper_activerecord_publisher.rb
80
+
81
+ Wisper::ActiveRecord::Publisher.configure do |config|
82
+ config.default_broadcast_events = [:create, :update] # Only broadcast create and update events
83
+ end
84
+ ```
85
+
86
+ Additionally, you can disable all broadcasts in a model by calling `disable_all_lifecycle_broadcasts!` on the model
87
+ class.
88
+
50
89
  ## Development
51
90
 
52
91
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,27 @@
1
+ module Wisper
2
+ module ActiveRecord
3
+ module Publisher
4
+ def self.configuration
5
+ @configuration ||= Configuration.new
6
+ end
7
+
8
+ def self.configure
9
+ yield configuration
10
+ end
11
+
12
+ class Configuration
13
+ attr_reader :default_broadcast_events
14
+
15
+ def initialize
16
+ @default_broadcast_events = VALID_BROADCAST_EVENTS
17
+ end
18
+
19
+ def default_broadcast_events=(events)
20
+ raise ArgumentError, "default_broadcast_events must be an array" unless events.is_a?(Array)
21
+
22
+ @default_broadcast_events = events
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,7 +1,7 @@
1
1
  module Wisper
2
2
  module Activerecord
3
3
  module Publisher
4
- VERSION = '0.1.2'.freeze
4
+ VERSION = '0.2.0'.freeze
5
5
  end
6
6
  end
7
7
  end
@@ -1,4 +1,5 @@
1
1
  require 'wisper/activerecord/publisher/version'
2
+ require 'wisper/activerecord/publisher/configuration'
2
3
  require 'active_record'
3
4
  require 'wisper'
4
5
 
@@ -10,14 +11,30 @@ module Wisper
10
11
  module Publisher
11
12
  extend ActiveSupport::Concern
12
13
 
14
+ VALID_BROADCAST_EVENTS = %i[create update destroy].freeze
15
+
13
16
  included do
14
17
  include Wisper::Publisher
15
18
 
16
19
  # NOTE: do not need to silence deprecations on Rails 5+
17
20
  ActiveSupport::Deprecation.silence do
18
- after_commit :broadcast_create, on: :create
19
- after_commit :broadcast_update, on: :update
20
- after_commit :broadcast_destroy, on: :destroy
21
+ VALID_BROADCAST_EVENTS.each do |event_name|
22
+ after_commit "broadcast_#{event_name}".to_sym, on: event_name, if: -> { should_broadcast?(event_name) }
23
+ end
24
+ end
25
+
26
+ class_attribute :wisper_activerecord_publisher_broadcast_events
27
+
28
+ def self.broadcast_on(*events)
29
+ events.each do |event|
30
+ raise ArgumentError, "Unknown broadcast event '#{event}'" unless VALID_BROADCAST_EVENTS.include?(event)
31
+ end
32
+
33
+ self.wisper_activerecord_publisher_broadcast_events = events
34
+ end
35
+
36
+ def self.disable_all_lifecycle_broadcasts!
37
+ self.wisper_activerecord_publisher_broadcast_events = []
21
38
  end
22
39
  end
23
40
 
@@ -45,9 +62,17 @@ module Wisper
45
62
  def broadcast_event_name(lifecycle)
46
63
  "#{self.class.model_name.param_key}_#{lifecycle}"
47
64
  end
65
+
66
+ def should_broadcast?(event)
67
+ (
68
+ wisper_activerecord_publisher_broadcast_events ||
69
+ Wisper::ActiveRecord::Publisher.configuration.default_broadcast_events
70
+ ).include?(event)
71
+ end
48
72
  end
49
73
  end
50
74
  end
75
+
51
76
  ActiveSupport.on_load(:active_record) do
52
77
  include Wisper::ActiveRecord::Publisher
53
78
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wisper-activerecord-publisher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - BetterUp Developers
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-08 00:00:00.000000000 Z
11
+ date: 2023-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: wisper
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- description:
83
+ description:
84
84
  email:
85
85
  - developers@betterup.co
86
86
  executables: []
@@ -100,13 +100,14 @@ files:
100
100
  - bin/setup
101
101
  - circle.yml
102
102
  - lib/wisper/activerecord/publisher.rb
103
+ - lib/wisper/activerecord/publisher/configuration.rb
103
104
  - lib/wisper/activerecord/publisher/version.rb
104
105
  - wisper-activerecord-publisher.gemspec
105
106
  homepage: https://github.com/betterup/wisper-activerecord-publisher
106
107
  licenses:
107
108
  - MIT
108
109
  metadata: {}
109
- post_install_message:
110
+ post_install_message:
110
111
  rdoc_options: []
111
112
  require_paths:
112
113
  - lib
@@ -121,9 +122,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
122
  - !ruby/object:Gem::Version
122
123
  version: '0'
123
124
  requirements: []
124
- rubyforge_project:
125
- rubygems_version: 2.6.6
126
- signing_key:
125
+ rubygems_version: 3.3.26
126
+ signing_key:
127
127
  specification_version: 4
128
128
  summary: Publish wisper events for activerecord model lifecycle
129
129
  test_files: []