Commit 81b7d6ad authored by Mike Kozono's avatar Mike Kozono

Automatically publish created events

For Models with Replicators that define the
"handle_after_create_commit" hook method.
parent 678846d1
......@@ -11,8 +11,7 @@ module Geo
class_methods do
end
# Called by Packages::PackageFile on create
def publish_created_event
def handle_after_create_commit
publish(:created, **created_params)
end
......
......@@ -35,7 +35,6 @@ class Packages::PackageFile < ApplicationRecord
with_replicator Geo::PackageFileReplicator
after_save :update_file_metadata, if: :saved_change_to_file?
after_create_commit -> { replicator.publish_created_event }
update_project_statistics project_statistics_name: :packages_size
......
......@@ -3,11 +3,14 @@
module Gitlab
module Geo
module ReplicableModel
def self.included(klass)
klass.extend(ClassMethods)
extend ActiveSupport::Concern
included do
# If this hook turns out not to apply to all Models, perhaps we should extract a `ReplicableBlobModel`
after_create_commit -> { replicator.handle_after_create_commit if replicator.respond_to?(:handle_after_create_commit) }
end
module ClassMethods
class_methods do
def with_replicator(klass)
raise ArgumentError, 'Must be a class inheriting from Gitlab::Geo::Replicator' unless klass < ::Gitlab::Geo::Replicator
......
......@@ -38,6 +38,10 @@ describe Gitlab::Geo::Replicator do
context 'model DSL' do
class DummyModel
include ActiveModel::Model
def self.after_create_commit(*args)
end
include Gitlab::Geo::ReplicableModel
with_replicator DummyReplicator
......
......@@ -18,10 +18,10 @@ RSpec.shared_examples 'a blob replicator' do
stub_current_geo_node(primary)
end
describe '#publish_created_event' do
describe '#handle_after_create_commit' do
it 'creates a Geo::Event' do
expect do
replicator.publish_created_event
replicator.handle_after_create_commit
end.to change { ::Geo::Event.count }.by(1)
expect(::Geo::Event.last.attributes).to include(
......@@ -60,5 +60,18 @@ RSpec.shared_examples 'a blob replicator' do
it 'is a Class' do
expect(invoke_model).to be_a(Class)
end
# For convenience (and reliability), instead of asking developers to include shared examples on each model spec as well
context 'replicable model' do
it 'defines #replicator' do
expect(model_record).to respond_to(:replicator)
end
it 'invokes replicator.handle_after_create_commit on create' do
expect(replicator).to receive(:handle_after_create_commit)
model_record.save!
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment