Noticed v2 is a great update but there are a few things that work differently to make note of:
notification_methods
blockA quick note before jumping in. When you make a subclass of Noticed::Event
, an additional nested Notification
class is created.
notification_methods
BlockBecause a nested Notification
class is programatically created, you can’t easily access it. That is where the magic of the notification_methods
block comes in. This block is run in the context of the class allowing you to easily customize and extend the class you didn’t directly create. You can see how this is done in the Noticed::NotificaationMethods
module.
The recommended method for configuring deliver methods in Noticed v2 is using a config block. This looks something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# app/notifiers/new_comment_notifier.rb
class NewCommendNotifier < Noticed::Event
deliver_by :my_method do |config|
config.value_option = 123
config.symbol_option = :my_symbol
config.lambda_option = -> { notification_method }
end
def my_symbol(notification)
# called by symbol and passed notification
# …
end
notification_methods do
def notification_method
# called from lambda
# …
end
end
end
The keys and values used are dependent of the delivery method you are working with but the way they are executed is the same. Depending on the type of value, configuration options are interpreted in different ways:
config.value_option
): Values are passed through directly. Easy peasy for this one.config.symbol_option
): Symbols are called on the Notice::Event
class and are passed the individual Notification
as a parameterconfig.lambda_option
): Lambdas are run in the context of the Notification
nested class. This means you have access to the normal notification methods along with everything in the notification_methods
block.Hope this helps when you are adding notifications to your project. If you’re running into problems, send me an email and I’ll see if I can help you out.