Sha256: 553c296d2579d8ff47f9767da592803c5e8cefe6919ccd7e0423bc17252b3549

Contents?: true

Size: 1009 Bytes

Versions: 5

Compression:

Stored size: 1009 Bytes

Contents

# frozen_string_literal: true

class RailsTransactionalOutbox
  class OutboxEntryFactory
    attr_reader :config
    private     :config

    def initialize(config: RailsTransactionalOutbox.configuration)
      @config = config
    end

    def build(model, event_type)
      outbox_model.new(attributes_for_entry(model, event_type))
    end

    private

    delegate :outbox_model, :outbox_entry_causality_key_resolver, to: :config

    def attributes_for_entry(model, event_type)
      {
        resource_class: model.class.to_s,
        resource_id: model.id,
        changeset: model.saved_changes,
        event_name: "#{model.model_name.singular}_#{event_name_suffix(event_type)}",
        context: RailsTransactionalOutbox::RecordProcessors::ActiveRecordProcessor.context,
        causality_key: outbox_entry_causality_key_resolver.call(model)
      }.compact
    end

    def event_name_suffix(event_type)
      RailsTransactionalOutbox::EventType.new(event_type).event_name_suffix
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails-transactional-outbox-1.0.0 lib/rails_transactional_outbox/outbox_entry_factory.rb
rails-transactional-outbox-0.4.0 lib/rails_transactional_outbox/outbox_entry_factory.rb
rails-transactional-outbox-0.3.1 lib/rails_transactional_outbox/outbox_entry_factory.rb
rails-transactional-outbox-0.3.0 lib/rails_transactional_outbox/outbox_entry_factory.rb
rails-transactional-outbox-0.2.1 lib/rails_transactional_outbox/outbox_entry_factory.rb