Skip to content

Commit

Permalink
Pass notification to event methods. #368
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Jan 20, 2024
1 parent 0bd255d commit c42fa3a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 53 deletions.
2 changes: 1 addition & 1 deletion lib/noticed/delivery_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def evaluate_option(name)

# Call method if symbol and matching method on Notifier
elsif option.is_a?(Symbol) && event.respond_to?(option)
event.send(option, self)
event.send(option, notification)

# Return the value
else
Expand Down
33 changes: 25 additions & 8 deletions test/delivery_method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,38 @@ class InheritedDeliveryMethod < Noticed::DeliveryMethods::ActionCable
test "if config" do
event = TestNotifier.deliver(User.first)
notification = event.notifications.first
@delivery_method = Noticed::DeliveryMethods::Test.new
delivery_method = Noticed::DeliveryMethods::Test.new

assert @delivery_method.perform(:test, notification, overrides: {if: true})
assert @delivery_method.perform(:test, notification, overrides: {if: -> { unread? }})
refute @delivery_method.perform(:test, notification, overrides: {if: false})
assert delivery_method.perform(:test, notification, overrides: {if: true})
assert delivery_method.perform(:test, notification, overrides: {if: -> { unread? }})
refute delivery_method.perform(:test, notification, overrides: {if: false})
end

test "unless overrides" do
event = TestNotifier.deliver(User.first)
notification = event.notifications.first
@delivery_method = Noticed::DeliveryMethods::Test.new
delivery_method = Noticed::DeliveryMethods::Test.new

refute @delivery_method.perform(:test, notification, overrides: {unless: true})
assert @delivery_method.perform(:test, notification, overrides: {unless: false})
assert @delivery_method.perform(:test, notification, overrides: {unless: -> { read? }})
refute delivery_method.perform(:test, notification, overrides: {unless: true})
assert delivery_method.perform(:test, notification, overrides: {unless: false})
assert delivery_method.perform(:test, notification, overrides: {unless: -> { read? }})
end

test "passes notification when calling methods on Event" do
notification = noticed_notifications(:one)
event = notification.event

def event.example_method(notification)
@example = notification
end

delivery_method = Noticed::DeliveryMethods::Test.new
delivery_method.instance_variable_set :@notification, notification
delivery_method.instance_variable_set :@event, event
delivery_method.instance_variable_set :@config, {if: :example_method}
delivery_method.evaluate_option(:if)

assert_equal notification, event.instance_variable_get(:@example)
end

private
Expand Down
45 changes: 1 addition & 44 deletions test/dummy/app/notifiers/comment_notifier.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,15 @@
class CommentNotifier < Noticed::Event
required_params :message

deliver_by :test

# delivery_by :email, mailer: "UserMailer", method: "new_comment"
deliver_by :email do |config|
config.mailer = "UserMailer"
config.method = :new_comment
# config.params = -> { params }
# config.args = -> { recipient }
end

deliver_by :action_cable do |config|
config.channel = "NotificationChannel"
config.channel = "Noticed::NotificationChannel"
config.stream = -> { recipient }
config.message = -> { params }
end

deliver_by :twilio_messaging do |config|
config.phone_number = "+1234567890"
config.account_sid = "abcd1234"
config.auth_token = "secret"
end

deliver_by :microsoft_teams do |config|
config.url = "https://example.org"
config.json = -> { params }
end

deliver_by :slack do |config|
config.headers = {"Authorization" => "Bearer xoxb-xxxxxxxxx-xxxxxxxxxx"}
config.json = -> { params }
end

deliver_by :fcm do |config|
config.credentials = Rails.root.join("config/certs/fcm.json")
# Or store them in the Rails credentials
# config.credentials = Rails.application.credentials.fcm
config.device_tokens = -> { recipient.device_tokens }

config.json = ->(device_token) {
{
token: device_token,
notification: {
title: "Test Title",
body: "Test body"
}
}
}

# Clean up invalid tokens that are no longer usable
config.invalid_token = ->(token:, platform:) { recipient.device_tokens.where(id: token.id).destroy_all }
end

deliver_by :ios do |config|
end
end

0 comments on commit c42fa3a

Please sign in to comment.