Skip to content

Commit

Permalink
Set recipient instance variable in Noticed::Base (#180)
Browse files Browse the repository at this point in the history
* Revert instance @recipient removal from #126

* Add tests/lint

Signed-off-by: John Cline <[email protected]>

* Prefer self for consistency
  • Loading branch information
clinejj authored Nov 19, 2021
1 parent 80522f7 commit 7940adc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/noticed/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def params
def run_delivery(recipient, enqueue: true)
delivery_methods = self.class.delivery_methods.dup

self.recipient = recipient

# Run database delivery inline first if it exists so other methods have access to the record
if (index = delivery_methods.find_index { |m| m[:name] == :database })
delivery_method = delivery_methods.delete_at(index)
Expand Down
22 changes: 18 additions & 4 deletions test/noticed_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,26 @@ class CallbackExample < Noticed::Base

deliver_by :database

after_deliver do
self.class.callbacks << :everything
before_database do
raise ArgumentError unless recipient

self.class.callbacks << :before_database
end

around_database do
raise ArgumentError unless recipient

self.class.callbacks << :around_database
end

after_database do
self.class.callbacks << :database
raise ArgumentError unless recipient

self.class.callbacks << :after_database
end

after_deliver do
self.class.callbacks << :after_everything
end
end

Expand Down Expand Up @@ -141,7 +155,7 @@ class Noticed::Test < ActiveSupport::TestCase

test "runs callbacks on notifications" do
CallbackExample.deliver(user)
assert_equal [:database, :everything], CallbackExample.callbacks
assert_equal [:before_database, :around_database, :after_database, :after_everything], CallbackExample.callbacks
end

test "runs callbacks on delivery methods" do
Expand Down

0 comments on commit 7940adc

Please sign in to comment.