From c42fa3a2c785a4ca6c410f3b928822c14849b453 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Fri, 19 Jan 2024 20:34:58 -0600 Subject: [PATCH] Pass notification to event methods. #368 --- lib/noticed/delivery_method.rb | 2 +- test/delivery_method_test.rb | 33 ++++++++++---- test/dummy/app/notifiers/comment_notifier.rb | 45 +------------------- 3 files changed, 27 insertions(+), 53 deletions(-) diff --git a/lib/noticed/delivery_method.rb b/lib/noticed/delivery_method.rb index 90f1fb07..f84dc63e 100644 --- a/lib/noticed/delivery_method.rb +++ b/lib/noticed/delivery_method.rb @@ -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 diff --git a/test/delivery_method_test.rb b/test/delivery_method_test.rb index 0807f72c..725096bf 100644 --- a/test/delivery_method_test.rb +++ b/test/delivery_method_test.rb @@ -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 diff --git a/test/dummy/app/notifiers/comment_notifier.rb b/test/dummy/app/notifiers/comment_notifier.rb index 7e80c635..c27d94c9 100644 --- a/test/dummy/app/notifiers/comment_notifier.rb +++ b/test/dummy/app/notifiers/comment_notifier.rb @@ -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