Skip to content

Commit

Permalink
Re-Implement Callbacks (#380)
Browse files Browse the repository at this point in the history
* Adding the ability to use callbacks for events and delivery methods

* Fixing standardrb issues and one failing test due to a copy/paste

* Moving the callbacks to outside the transaction

* Removing event callbacks in favor of only delivery method callbacks

---------

Co-authored-by: Chris Oliver <[email protected]>
  • Loading branch information
avanrielly and excid3 authored Jan 24, 2024
1 parent 42b7fff commit f529df0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ CarSaleNotifier.with(record: Car.last, branch: Branch.last).deliver(Branch.hq)
#=> OK
```


#### Helper Methods

Notifiers can implement various helper methods, within a `notification_methods` block, that make it easier to render the resulting notification directly. These helpers can be helpful depending on where and how you choose to render notifications. A common use is rendering a user’s notifications in your web UI as standard ERB. These notification helper methods make that rendering much simpler:
Expand All @@ -225,7 +226,7 @@ Notifiers can implement various helper methods, within a `notification_methods`

On the other hand, if you’re using email delivery, ActionMailer has its own full stack for setting up objects and rendering. Your notification helper methods will always be available from the notification object, but using ActionMailer’s own paradigms may fit better for that particular delivery method. YMMV.

##### URL Helpers
#### URL Helpers

Rails url helpers are included in Notifiers by default so you have full access to them in your notification helper methods, just like you would in your controllers and views.

Expand All @@ -235,7 +236,7 @@ _But don't forget_, you'll need to configure `default_url_options` in order for
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
```

##### Translations
#### Translations

We've also included Rails’ `translate` and `t` helpers for you to use in your notification helper methods. This also provides an easy way of scoping translations. If the key starts with a period, it will automatically scope the key under `notifiers`, the underscored name of the notifier class, and `notification`. For example:

Expand Down Expand Up @@ -503,6 +504,18 @@ class DeliveryMethods::WhatsApp < Noticed::DeliveryMethod
end
```

#### Callbacks

Callbacks for delivery methods wrap the _actual_ delivery of the notification. You can use `before_deliver`, `around_deliver` and `after_deliver` in your custom delivery methods.

```ruby
class DeliveryMethods::Discord < Noticed::DeliveryMethod
after_deliver do
# Do whatever you want
end
end
```

### 📦 Database Model

The Noticed database models include several helpful features to make working with notifications easier.
Expand Down
7 changes: 6 additions & 1 deletion lib/noticed/delivery_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ class DeliveryMethod < Noticed.parent_class.constantize
include ApiClient
include RequiredOptions

extend ActiveModel::Callbacks
define_model_callbacks :deliver

class_attribute :logger, default: Rails.logger

attr_reader :config, :event, :notification
Expand All @@ -19,7 +22,9 @@ def perform(delivery_method_name, notification, 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
24 changes: 24 additions & 0 deletions test/delivery_method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ def event.example_method(notification)
assert_equal notification, event.instance_variable_get(:@example)
end

class CallbackDeliveryMethod < Noticed::DeliveryMethod
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

test "calls callbacks" do
event = CallbackNotifier.with(message: "test")
notification = Noticed::Notification.create(recipient: User.first, event: event)
delivery_method = CallbackDeliveryMethod.new
delivery_method.perform(:test, notification)
assert_equal delivery_method.message, "new message"
end

private

def set_config(config)
Expand Down

0 comments on commit f529df0

Please sign in to comment.