Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support callbacks in bulk delivery methods #487

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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