Skip to content

Commit

Permalink
rename polymorphic to messageable again
Browse files Browse the repository at this point in the history
  • Loading branch information
bedrock-adam committed Feb 24, 2024
1 parent c9b243d commit cee6200
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 81 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ bin/rails g outboxer:schema
bin/rake db:migrate
```

### include outboxable into your models
### include messageable into your models

```ruby
class Event < ActiveRecord::Base
include Outboxer::Outboxable
include Outboxer::Messageable

# ...
end
Expand All @@ -55,9 +55,9 @@ bin/rails g outboxer:sidekiq_publisher

```ruby
Outboxer::Publisher.publish! do |outboxer_message|
case outboxer_message.outboxable_type
case outboxer_message.messageable_type
when 'Event'
EventCreatedJob.perform_async({ 'id' => outboxer_message.outboxable_id })
EventCreatedJob.perform_async({ 'id' => outboxer_message.messageable_id })
end
end
```
Expand Down
4 changes: 2 additions & 2 deletions bin/sidekiq_publisher
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ Outboxer::Database.connect!(config: config.merge('pool' => concurrency + 1))
Outboxer::Publisher.publish!(
thread_count: concurrency, queue_size: concurrency * 2, poll_interval: 1, logger: logger
) do |outboxer_message|
case outboxer_message.outboxable_type
case outboxer_message.messageable_type
when 'Event'
EventCreatedJob.perform_async({ 'id' => outboxer_message.outboxable_id })
EventCreatedJob.perform_async({ 'id' => outboxer_message.messageable_id })
end
end

Expand Down
4 changes: 3 additions & 1 deletion db/migrate/create_outboxer_exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class CreateOutboxerExceptions < ActiveRecord::Migration[6.1]
def up
ActiveRecord::Base.transaction do
create_table :outboxer_exceptions, id: :uuid do |t|
create_table :outboxer_exceptions, id: false do |t|
t.uuid :id, primary_key: true

t.references :message, null: false,
type: :uuid, foreign_key: { to_table: :outboxer_messages }

Expand Down
4 changes: 3 additions & 1 deletion db/migrate/create_outboxer_frames.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class CreateOutboxerFrames < ActiveRecord::Migration[6.1]
def up
create_table :outboxer_frames, id: :uuid do |t|
create_table :outboxer_frames, id: false do |t|
t.uuid :id, primary_key: true

t.references :exception,
null: false, type: :uuid, foreign_key: { to_table: :outboxer_exceptions }

Expand Down
7 changes: 4 additions & 3 deletions db/migrate/create_outboxer_messages.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class CreateOutboxerMessages < ActiveRecord::Migration[6.1]
def up
create_table :outboxer_messages, id: :uuid do |t|
create_table :outboxer_messages, id: false do |t|
t.uuid :id, primary_key: true
t.string :status, null: false, limit: 255

t.text :outboxable_id, null: false
t.text :outboxable_type, null: false
t.text :messageable_id, null: false
t.text :messageable_type, null: false

t.timestamps
end
Expand Down
70 changes: 40 additions & 30 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,44 @@
# require_relative '../app/models/outboxer_exception'

100.times do |i|
outboxable_type = 'Event'
outboxable_id = 1

Outboxer::Models::Message.create!(
outboxable_type: outboxable_type,
outboxable_id: outboxable_id,
status: Outboxer::Models::Message::Status::UNPUBLISHED)

Outboxer::Models::Message.create!(
outboxable_type: outboxable_type,
outboxable_id: outboxable_id,
status: Outboxer::Models::Message::Status::PUBLISHING)

failed_message = Outboxer::Models::Message.create!(
outboxable_type: outboxable_type,
outboxable_id: outboxable_id,
status: Outboxer::Models::Message::Status::FAILED)

failed_message_exception = failed_message.exceptions.create!(
class_name: 'ActiveRecord::RecordInvalid',
message_text: 'Validation failed: Exceptions is invalid')

failed_message_exception.frames.create!(
index: 0, text: "/lib/active_record/validations.rb:84:in `raise_validation_error'")

failed_message_exception.frames.create!(
index: 1, text: "/lib/active_record/validations.rb:55:in `save!'")

failed_message_exception.frames.create!(
index: 2, text: "lib/active_record/transactions.rb:313:in `block in save!'")
case i % 3
when 0
Outboxer::Models::Message.create!(
id: SecureRandom.uuid,
messageable_type: 'Event',
messageable_id: i,
status: Outboxer::Models::Message::Status::UNPUBLISHED)
when 1
Outboxer::Models::Message.create!(
id: SecureRandom.uuid,
messageable_type: 'Event',
messageable_id: i,
status: Outboxer::Models::Message::Status::PUBLISHING)
else
failed_message = Outboxer::Models::Message.create!(
id: SecureRandom.uuid,
messageable_type: 'Event',
messageable_id: i,
status: Outboxer::Models::Message::Status::FAILED)

failed_message_exception = failed_message.exceptions.create!(
id: SecureRandom.uuid,
class_name: 'ActiveRecord::RecordInvalid',
message_text: 'Validation failed: Exceptions is invalid')

failed_message_exception.frames.create!(
id: SecureRandom.uuid,
index: 0,
text: "/lib/active_record/validations.rb:84:in `raise_validation_error'")

failed_message_exception.frames.create!(
id: SecureRandom.uuid,
index: 1,
text: "/lib/active_record/validations.rb:55:in `save!'")

failed_message_exception.frames.create!(
id: SecureRandom.uuid,
index: 2,
text: "lib/active_record/transactions.rb:313:in `block in save!'")
end
end
2 changes: 1 addition & 1 deletion lib/outboxer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
require_relative "outboxer/models"

require_relative "outboxer/message"
require_relative "outboxer/outboxable"
require_relative "outboxer/messageable"

require_relative "outboxer/database"
require_relative "outboxer/publisher"
Expand Down
4 changes: 2 additions & 2 deletions lib/outboxer/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ def failed!(id:, exception:)
outboxer_message.update!(status: Models::Message::Status::FAILED)

outboxer_exception = outboxer_message.exceptions.create!(
class_name: exception.class.name, message_text: exception.message)
id: SecureRandom.uuid, class_name: exception.class.name, message_text: exception.message)

exception.backtrace.each_with_index do |frame, index|
outboxer_exception.frames.create!(index: index, text: frame)
outboxer_exception.frames.create!(id: SecureRandom.uuid, index: index, text: frame)
end

outboxer_message
Expand Down
6 changes: 3 additions & 3 deletions lib/outboxer/outboxable.rb → lib/outboxer/messageable.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Outboxer
module Outboxable
# The Outboxable module, when included in a class, provides the ability to associate
module Messageable
# The Messageable module, when included in a class, provides the ability to associate
# that class with an Outboxer message. This association is set up through a series
# of class methods defined in the included ClassMethods module.

Expand All @@ -14,7 +14,7 @@ module ClassMethods
def has_outboxer_message(
dependent: nil,
class_name: 'Outboxer::Models::Message',
as: :outboxable
as: :messageable
)
has_one :outboxer_message,
dependent: dependent,
Expand Down
6 changes: 3 additions & 3 deletions lib/outboxer/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module Models
#
# @!attribute [r] id
# @return [Integer] The unique identifier for the message.
# @!attribute [r] outboxable_id
# @!attribute [r] messageable_id
# @return [Integer] The ID of the associated polymorphic message.
# @!attribute [r] outboxable_type
# @!attribute [r] messageable_type
# @return [String] The type of the associated polymorphic message.
# @!attribute status
# @return [String] The status of the message (see {STATUS}).
Expand All @@ -35,7 +35,7 @@ module Status
attribute :status, default: -> { Status::UNPUBLISHED }
validates :status, inclusion: { in: STATUSES }, length: { maximum: 255 }

belongs_to :outboxable, polymorphic: true
belongs_to :messageable, polymorphic: true

has_many :exceptions,
-> { order(created_at: :asc) },
Expand Down
10 changes: 6 additions & 4 deletions spec/lib/outboxer/message/failed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ def raise_exception
context 'when publishing message' do
let!(:publishing_message) do
Models::Message.create!(
outboxable_type: 'DummyType',
outboxable_id: 1,
id: SecureRandom.uuid,
messageable_type: 'DummyType',
messageable_id: 1,
status: Models::Message::Status::PUBLISHING,
created_at: DateTime.parse('2024-01-14T00:00:00Z'))
end
Expand Down Expand Up @@ -59,8 +60,9 @@ def raise_exception
context 'when unpublished messaged' do
let(:unpublished_message) do
Models::Message.create!(
outboxable_type: 'DummyType',
outboxable_id: 1,
id: SecureRandom.uuid,
messageable_type: 'DummyType',
messageable_id: 1,
status: Models::Message::Status::UNPUBLISHED,
created_at: DateTime.parse('2024-01-14T00:00:00Z'))
end
Expand Down
10 changes: 6 additions & 4 deletions spec/lib/outboxer/message/published_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ module Outboxer
context 'when publishing message' do
let!(:publishing_message) do
Models::Message.create!(
outboxable_type: 'DummyType',
outboxable_id: 1,
id: SecureRandom.uuid,
messageable_type: 'DummyType',
messageable_id: 1,
status: Models::Message::Status::PUBLISHING,
created_at: DateTime.parse('2024-01-14T00:00:00Z'))
end
Expand All @@ -26,8 +27,9 @@ module Outboxer
context 'when unpublished messaged' do
let(:unpublished_message) do
Models::Message.create!(
outboxable_type: 'DummyType',
outboxable_id: 1,
id: SecureRandom.uuid,
messageable_type: 'DummyType',
messageable_id: 1,
status: Models::Message::Status::UNPUBLISHED,
created_at: DateTime.parse('2024-01-14T00:00:00Z'))
end
Expand Down
10 changes: 6 additions & 4 deletions spec/lib/outboxer/message/republish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ module Outboxer
context 'when failed message' do
let!(:failed_message) do
Models::Message.create!(
outboxable_type: 'DummyType',
outboxable_id: 1,
id: SecureRandom.uuid,
messageable_type: 'DummyType',
messageable_id: 1,
status: Models::Message::Status::FAILED,
created_at: DateTime.parse('2024-01-14T00:00:00Z'))
end
Expand All @@ -29,8 +30,9 @@ module Outboxer
context 'when unpublished messaged' do
let(:unpublished_message) do
Models::Message.create!(
outboxable_type: 'DummyType',
outboxable_id: 1,
id: SecureRandom.uuid,
messageable_type: 'DummyType',
messageable_id: 1,
status: Models::Message::Status::UNPUBLISHED,
created_at: DateTime.parse('2024-01-14T00:00:00Z'))
end
Expand Down
14 changes: 8 additions & 6 deletions spec/lib/outboxer/message/unpublished_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ module Outboxer
let!(:unpublished_messages) do
[
Models::Message.create!(
outboxable_type: 'DummyType',
outboxable_id: 1,
id: SecureRandom.uuid,
messageable_type: 'DummyType',
messageable_id: 1,
status: Models::Message::Status::UNPUBLISHED,
created_at: DateTime.parse('2024-01-14T00:00:00Z')),
Models::Message.create!(
outboxable_type: 'DummyType',
outboxable_id: 2,
id: SecureRandom.uuid,
messageable_type: 'DummyType',
messageable_id: 2,
status: Models::Message::Status::UNPUBLISHED,
created_at: DateTime.parse('2024-01-14T00:00:01Z'))
]
Expand All @@ -27,8 +29,8 @@ module Outboxer
expect(publishing_messages.count).to eq(1)

publishing_message = publishing_messages.first
expect(publishing_message.outboxable_type).to eq('DummyType')
expect(publishing_message.outboxable_id).to eq('1')
expect(publishing_message.messageable_type).to eq('DummyType')
expect(publishing_message.messageable_id).to eq('1')
expect(publishing_message.status).to eq(Models::Message::Status::PUBLISHING)
end

Expand Down
12 changes: 7 additions & 5 deletions spec/lib/outboxer/publisher/pop_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ module Outboxer

let(:message) do
Models::Message.create!(
outboxable_type: 'DummyType',
outboxable_id: 1,
id: SecureRandom.uuid,
messageable_type: 'DummyType',
messageable_id: 1,
status: Models::Message::Status::PUBLISHING)
end

Expand All @@ -20,7 +21,7 @@ module Outboxer

it 'processes the message' do
Publisher.pop_message!(queue: queue, logger: logger) do |msg|
expect(msg.outboxable_type).to eq('DummyType')
expect(msg.messageable_type).to eq('DummyType')
expect(msg.id).to eq(message.id)
end
end
Expand All @@ -38,8 +39,9 @@ module Outboxer

let(:message) do
Models::Message.create!(
outboxable_type: 'DummyType',
outboxable_id: 1,
id: SecureRandom.uuid,
messageable_type: 'DummyType',
messageable_id: 1,
status: Models::Message::Status::PUBLISHING)
end

Expand Down
9 changes: 5 additions & 4 deletions spec/lib/outboxer/publisher/publish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ module Outboxer

let!(:message) do
Models::Message.create!(
outboxable_type: 'DummyType',
outboxable_id: 1,
id: SecureRandom.uuid,
messageable_type: 'DummyType',
messageable_id: 1,
status: Models::Message::Status::UNPUBLISHED)
end

Expand All @@ -29,8 +30,8 @@ module Outboxer
logger: logger,
kernel: kernel
) do |publishing_message|
expect(publishing_message.outboxable_type).to eq('DummyType')
expect(publishing_message.outboxable_id).to eq('1')
expect(publishing_message.messageable_type).to eq('DummyType')
expect(publishing_message.messageable_id).to eq('1')
expect(publishing_message.status).to eq(Models::Message::Status::PUBLISHING)

Publisher.stop!
Expand Down
10 changes: 6 additions & 4 deletions spec/lib/outboxer/publisher/push_messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ module Outboxer

let!(:message) do
Models::Message.create!(
outboxable_type: 'DummyType',
outboxable_id: 2,
id: SecureRandom.uuid,
messageable_type: 'DummyType',
messageable_id: 2,
status: Models::Message::Status::UNPUBLISHED)
end

Expand Down Expand Up @@ -72,8 +73,9 @@ module Outboxer

let(:message) do
Models::Message.create!(
outboxable_type: 'DummyType',
outboxable_id: 1,
id: SecureRandom.uuid,
messageable_type: 'DummyType',
messageable_id: 1,
status: Models::Message::Status::PUBLISHING)
end

Expand Down

0 comments on commit cee6200

Please sign in to comment.