Sha256: 501de341569ff7d4f4daf196ad1e0140b5d105427c5aa3a8850a41d45f821e82

Contents?: true

Size: 1.47 KB

Versions: 7

Compression:

Stored size: 1.47 KB

Contents

module Journaled::Event
  extend ActiveSupport::Concern

  def journal!
    Journaled::Writer.new(journaled_event: self).journal!
  end

  # Base attributes

  def id
    @id ||= SecureRandom.uuid
  end

  def event_type
    @event_type ||= self.class.event_type
  end

  def created_at
    @created_at ||= Time.zone.now
  end

  # Event metadata and configuration (not serialized)

  def journaled_schema_name
    self.class.to_s.underscore
  end

  def journaled_attributes
    self.class.public_send(:journaled_attributes).each_with_object({}) do |attribute, memo|
      memo[attribute] = public_send(attribute)
    end
  end

  def journaled_partition_key
    event_type
  end

  def journaled_stream_name
    Journaled.default_stream_name
  end

  def tagged?
    false
  end

  private

  class_methods do
    def journal_attributes(*args, enqueue_with: {}, tagged: false)
      journaled_attributes.concat(args)
      journaled_enqueue_opts.merge!(enqueue_with)

      include Tagged if tagged
    end

    def journaled_attributes
      @journaled_attributes ||= []
    end

    def event_type
      name.underscore.parameterize(separator: '_')
    end
  end

  included do
    class_attribute :journaled_enqueue_opts, default: {}

    journal_attributes :id, :event_type, :created_at
  end

  module Tagged
    extend ActiveSupport::Concern

    included do
      journaled_attributes << :tags
    end

    def tags
      Journaled::Current.tags
    end

    def tagged?
      true
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
journaled-5.3.1 app/models/journaled/event.rb
journaled-5.3.0 app/models/journaled/event.rb
journaled-5.2.0 app/models/journaled/event.rb
journaled-5.1.1 app/models/journaled/event.rb
journaled-5.1.0 app/models/journaled/event.rb
journaled-5.0.0 app/models/journaled/event.rb
journaled-4.3.0 app/models/journaled/event.rb