Skip to content

Commit

Permalink
Support callbacks in bulk delivery methods. (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiculescu authored Sep 10, 2024
1 parent 217975a commit 4a10493
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
### Unreleased

* Support callbacks in bulk delivery methods.

### 2.4.3

* Add translation and URL helpers to Ephemeral notifiers & notifications #485
Expand Down
7 changes: 6 additions & 1 deletion lib/noticed/bulk_delivery_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ class BulkDeliveryMethod < ApplicationJob
include ApiClient
include RequiredOptions

extend ActiveModel::Callbacks
define_model_callbacks :deliver

class_attribute :logger, default: Rails.logger

attr_reader :config, :event
Expand All @@ -20,7 +23,9 @@ def perform(delivery_method_name, event, recipients: nil, params: {}, overrides:
return false if config.has_key?(:if) && !evaluate_option(:if)
return false if config.has_key?(:unless) && evaluate_option(:unless)

deliver
run_callbacks :deliver do
deliver
end
end

def deliver
Expand Down
23 changes: 23 additions & 0 deletions test/delivery_method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,26 @@ def deliver
end
end

class CallbackBulkDeliveryMethod < Noticed::BulkDeliveryMethod
before_deliver :set_message
attr_reader :message

def set_message
@message = "new message"
end

def deliver
end
end

class CallbackNotifier < Noticed::Event
deliver_by :test
end

class CallbackBulkNotifier < Noticed::Event
bulk_deliver_by :test
end

test "calls callbacks" do
event = CallbackNotifier.with(message: "test")
notification = Noticed::Notification.create(recipient: User.first, event: event)
Expand All @@ -81,6 +97,13 @@ class CallbackNotifier < Noticed::Event
assert_equal delivery_method.message, "new message"
end

test "calls callbacks for bulk delivery" do
event = CallbackBulkNotifier.with(message: "test")
delivery_method = CallbackBulkDeliveryMethod.new
delivery_method.perform(:test, event)
assert_equal delivery_method.message, "new message"
end

private

def set_config(config)
Expand Down

0 comments on commit 4a10493

Please sign in to comment.