Skip to content

Commit

Permalink
Allow use of evaluated recipients in ephemeral notifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiculescu committed Sep 23, 2024
1 parent 217975a commit c748ba6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
6 changes: 5 additions & 1 deletion app/models/noticed/ephemeral.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ def self.notification_methods(&block)
const_get(:Notification).class_eval(&block)
end

def deliver(recipients)
def deliver(recipients = nil)
recipients ||= evaluate_recipients
recipients = Array.wrap(recipients)

bulk_delivery_methods.each do |_, deliver_by|
deliver_by.ephemeral_perform_later(self.class.name, recipients, params)
end
Expand All @@ -48,6 +50,8 @@ def deliver(recipients)
deliver_by.ephemeral_perform_later(self.class.name, recipient, params)
end
end

self
end

def record
Expand Down
32 changes: 29 additions & 3 deletions test/notifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,28 @@ class RecipientsBlock < Noticed::Event
recipients do
params.fetch(:recipients)
end
deliver_by :test
end

class RecipientsLambda < Noticed::Event
recipients -> { params.fetch(:recipients) }
deliver_by :test
end

class RecipientsMethod < Noticed::Event
deliver_by :test
recipients :recipients

def recipients
params.fetch(:recipients)
end
end

class RecipientsLambdaEphemeral < Noticed::Ephemeral
recipients -> { params.fetch(:recipients) }
deliver_by :test
end

test "includes Rails urls" do
assert_equal "http://localhost:3000/", SimpleNotifier.new.url
end
Expand Down Expand Up @@ -59,15 +67,33 @@ def recipients
end

test "recipients block" do
assert_equal [:foo, :bar], RecipientsBlock.with(recipients: [:foo, :bar]).evaluate_recipients
event = RecipientsBlock.with(recipients: [User.create!(email: "foo"), User.create!(email: "bar")]).deliver
assert_equal 2, event.notifications.count
assert_equal User.find_by(email: "foo"), event.notifications.first.recipient
end

test "recipients lambda" do
assert_equal [:foo, :bar], RecipientsLambda.with(recipients: [:foo, :bar]).evaluate_recipients
event = RecipientsLambda.with(recipients: [User.create!(email: "foo"), User.create!(email: "bar")]).deliver
assert_equal 2, event.notifications.count
assert_equal User.find_by(email: "foo"), event.notifications.first.recipient
end

test "recipients" do
assert_equal [:foo, :bar], RecipientsMethod.with(recipients: [:foo, :bar]).evaluate_recipients
event = RecipientsMethod.with(recipients: [User.create!(email: "foo"), User.create!(email: "bar")]).deliver
assert_equal 2, event.notifications.count
assert_equal User.find_by(email: "foo"), event.notifications.first.recipient

assert_enqueued_with(job: Noticed::DeliveryMethods::Test, args: [:test, event.notifications.last]) do
perform_enqueued_jobs
end
end

test "recipients ephemeral" do
users = [ User.create!(email: "foo"), User.create!(email: "bar") ]

assert_enqueued_with(job: Noticed::DeliveryMethods::Test, args: [:test, "NotifierTest::RecipientsLambdaEphemeral::Notification", { recipient: User.find_by(email: "foo"), params: { recipients: users } }]) do
RecipientsLambdaEphemeral.with(recipients: users).deliver
end
end

test "deliver without recipients" do
Expand Down

0 comments on commit c748ba6

Please sign in to comment.