Skip to content

Commit

Permalink
Allow configuring delivery methods with a config block
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Oct 19, 2023
1 parent 7319306 commit a3ff8db
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
6 changes: 4 additions & 2 deletions 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 Expand Up @@ -129,7 +131,7 @@ def run_delivery_method(delivery_method, recipient:, enqueue:, record:)
end

def delivery_method_for(name, options)
if options[:class]
if options.has_key? :class
options[:class].constantize
else
"Noticed::DeliveryMethods::#{name.to_s.camelize}".constantize
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
25 changes: 16 additions & 9 deletions test/dummy/app/notifications/comment_notification.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
class CommentNotification < Noticed::Base
deliver_by :database, format: :attributes_for_database
deliver_by :database do |config|
config.format = Proc.new 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 do |config|
config.class = "DiscordNotification"
end

def url
Expand Down

0 comments on commit a3ff8db

Please sign in to comment.