Skip to content

Commit

Permalink
Add support for delivery_by config blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Oct 19, 2023
1 parent 55aa7bf commit 2a6fdc0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
4 changes: 3 additions & 1 deletion lib/noticed/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class Base

class << self
def deliver_by(name, options = {})
delivery_methods.push(name: name, options: options)
config = ActiveSupport::OrderedOptions.new.merge(options)
yield config if block_given?
delivery_methods.push(name: name, options: config)
define_model_callbacks(name)
end

Expand Down
6 changes: 5 additions & 1 deletion lib/noticed/delivery_methods/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ def association_name

def attributes
if (method = options[:format])
notification.send(method)
if method.respond_to? :call
notification.instance_eval(&method)
else
notification.send(method)
end
else
{
type: notification.class.name,
Expand Down
23 changes: 14 additions & 9 deletions test/dummy/app/notifications/comment_notification.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
class CommentNotification < Noticed::Base
deliver_by :database, format: :attributes_for_database
deliver_by :database do |config|
config.format = proc do
{
account_id: 1,
type: self.class.name,
params: params
}
end
end

deliver_by :action_cable
deliver_by :email, mailer: "UserMailer"
deliver_by :discord, class: "DiscordNotification"

def attributes_for_database
{
account_id: 1,
type: self.class.name,
params: params
}
deliver_by :email do |config|
config.mailer = "UserMailer"
end

deliver_by :discord, class: "DiscordNotification"

def url
root_url
end
Expand Down

0 comments on commit 2a6fdc0

Please sign in to comment.