Skip to content

Commit

Permalink
Extract DeliverBy to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Jan 15, 2024
1 parent 5ec9617 commit f8e0f8c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
40 changes: 0 additions & 40 deletions app/models/concerns/noticed/deliverable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,6 @@ module Noticed
module Deliverable
extend ActiveSupport::Concern

class DeliverBy
attr_reader :name, :config, :bulk

def initialize(name, config, bulk: false)
@name, @config, @bulk, = name, config, bulk
end

def constant
namespace = bulk ? "Noticed::BulkDeliveryMethods" : "Noticed::DeliveryMethods"
config.fetch(:class, [namespace, name.to_s.camelize].join("::")).constantize
end

def validate!
constant.required_option_names.each do |option|
raise ValidationError, "option `#{option}` must be set for `deliver_by :#{name}`" unless config[option].present?
end
end

def perform_later(event_or_notification, options = {})
options[:wait] = evaluate_option(:wait, event_or_notification) if config.has_key?(:wait)
options[:wait_until] = evaluate_option(:wait_until, event_or_notification) if config.has_key?(:wait_until)
options[:queue] = evaluate_option(:queue, event_or_notification) if config.has_key?(:queue)
options[:priority] = evaluate_option(:priority, event_or_notification) if config.has_key?(:priority)

constant.set(options).perform_later(name, event_or_notification)
end

def evaluate_option(name, context)
option = config[name]

if option&.respond_to?(:call)
context.instance_exec(&option)
elsif option.is_a?(Symbol) && context.respond_to?(option)
context.send(option)
else
option
end
end
end

included do
class_attribute :bulk_delivery_methods, instance_writer: false, default: {}
class_attribute :delivery_methods, instance_writer: false, default: {}
Expand Down
43 changes: 43 additions & 0 deletions app/models/noticed/deliverable/deliver_by.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Noticed
module Deliverable
class DeliverBy
attr_reader :name, :config, :bulk

def initialize(name, config, bulk: false)
@name, @config, @bulk, = name, config, bulk
end

def constant
namespace = bulk ? "Noticed::BulkDeliveryMethods" : "Noticed::DeliveryMethods"
config.fetch(:class, [namespace, name.to_s.camelize].join("::")).constantize
end

def validate!
constant.required_option_names.each do |option|
raise ValidationError, "option `#{option}` must be set for `deliver_by :#{name}`" unless config[option].present?
end
end

def perform_later(event_or_notification, options = {})
options[:wait] = evaluate_option(:wait, event_or_notification) if config.has_key?(:wait)
options[:wait_until] = evaluate_option(:wait_until, event_or_notification) if config.has_key?(:wait_until)
options[:queue] = evaluate_option(:queue, event_or_notification) if config.has_key?(:queue)
options[:priority] = evaluate_option(:priority, event_or_notification) if config.has_key?(:priority)

constant.set(options).perform_later(name, event_or_notification)
end

def evaluate_option(name, context)
option = config[name]

if option&.respond_to?(:call)
context.instance_exec(&option)
elsif option.is_a?(Symbol) && context.respond_to?(option)
context.send(option)
else
option
end
end
end
end
end

0 comments on commit f8e0f8c

Please sign in to comment.