mirror of
https://github.com/ditkrg/outboxable.git
synced 2026-01-22 22:06:47 +00:00
Adds the outboxable main module
This commit is contained in:
parent
3410cd3d30
commit
a72109bbb6
@ -2,7 +2,55 @@
|
||||
|
||||
require_relative "outboxable/version"
|
||||
|
||||
require 'outboxable/worker'
|
||||
require 'outboxable/publishing_manager'
|
||||
require 'outboxable/polling_publisher_worker'
|
||||
require 'outboxable/connection'
|
||||
require 'outboxable/configuration'
|
||||
require 'outboxable/rabbitmq/publisher'
|
||||
|
||||
|
||||
module Outboxable
|
||||
class Error < StandardError; end
|
||||
# Your code goes here...
|
||||
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
after_create :instantiate_outbox_for_create, if: proc { |object| object.check_outbox_condition(object:, operation: :create) }
|
||||
after_update :instantiate_outbox_for_update, if: proc { |object| object.check_outbox_condition(object:, operation: :update) }
|
||||
|
||||
has_many :outboxes, as: :outboxable, autosave: false
|
||||
|
||||
def instantiate_outbox(routing_key: )
|
||||
outboxes.new(
|
||||
routing_key:,
|
||||
exchange: Outboxable.configuration.rabbitmq_event_bus_exchange,
|
||||
payload: as_json
|
||||
)
|
||||
end
|
||||
|
||||
def instantiate_outbox_for_create
|
||||
routing_key = outbox_configurations[:run_on][:create]&.[](:routing_key) || "#{outbox_configurations[:base][:routing_key]}.created"
|
||||
instantiate_outbox(routing_key:).save!
|
||||
end
|
||||
|
||||
def instantiate_outbox_for_update
|
||||
routing_key = outbox_configurations[:run_on][:update]&.[](:routing_key) || "#{outbox_configurations[:base][:routing_key]}.updated"
|
||||
instantiate_outbox(routing_key:).save!
|
||||
end
|
||||
|
||||
def check_outbox_condition(object:, operation:)
|
||||
# Check if called on create
|
||||
operation_is_included = object.outbox_configurations[:run_on].keys.include?(operation)
|
||||
|
||||
# Check if there is a supplied condition
|
||||
supplied_condition_as_proc = object.outbox_configurations[:run_on][operation][:condition]
|
||||
|
||||
# Return the result of operation is included if supplied condition is blank, which mean that there is no condition to be met
|
||||
return operation_is_included if supplied_condition_as_proc.blank?
|
||||
|
||||
# Else check the condition as well as the inclusion of the operation
|
||||
operation_is_included && supplied_condition_as_proc.call(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user