Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/multiple_man/mixins/publisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ def Publisher.included(base)
base.extend(ClassMethods)
if base.respond_to?(:after_commit)
base.after_commit(on: :create) { |r| r.multiple_man_publish(:create) }
base.after_commit(on: :update) do |r|
if !r.respond_to?(:previous_changes) || r.previous_changes.any?
r.multiple_man_publish(:update)
end
end
base.after_commit(on: :update) { |r| r.multiple_man_publish(:update) }
base.after_commit(on: :destroy) { |r| r.multiple_man_publish(:destroy) }
end

Expand Down
14 changes: 11 additions & 3 deletions lib/multiple_man/model_publisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@

module MultipleMan
class ModelPublisher
DEFAULT_OPTIONS = {
update_unless: ->(r) {
r.respond_to?(:previous_changes) && r.previous_changes.blank?
}
}.freeze

def initialize(options = {})
self.options = options.with_indifferent_access
@options = DEFAULT_OPTIONS.merge(options).with_indifferent_access
@update_unless = @options[:update_unless]
end

def publish(records, operation=:create)
def publish(records, operation = :create)
return unless MultipleMan.configuration.enabled

Connection.connect do |connection|
ActiveSupport::Notifications.instrument('multiple_man.publish_messages') do
all_records(records) do |record|
next if :update == operation && update_unless.call(record)

ActiveSupport::Notifications.instrument('multiple_man.publish_message') do
push_record(connection, record, operation)
end
Expand All @@ -25,7 +33,7 @@ def publish(records, operation=:create)

private

attr_accessor :options
attr_reader :options, :update_unless

def push_record(connection, record, operation)
data = PayloadGenerator.new(record, operation, options)
Expand Down
19 changes: 17 additions & 2 deletions spec/model_publisher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe MultipleMan::ModelPublisher do
describe MultipleMan::ModelPublisher do
let(:channel_stub) { double(Bunny::Channel, topic: topic_stub)}
let(:topic_stub) { double(Bunny::Exchange, publish: nil) }

Expand Down Expand Up @@ -34,6 +34,21 @@ def model_name
described_class.new(fields: [:foo]).publish(MockObject.new)
end

it 'skips updates for models with no changes by default' do
expect(topic_stub).to_not receive(:publish)

model = OpenStruct.new(previous_changes: [])
described_class.new(fields: [:foo]).publish(model, :update)
end

it 'allows skipping updates' do
expect(topic_stub).to_not receive(:publish)

model = OpenStruct.new(previous_changes: [])
described_class.new(fields: [:foo], update_unless: ->(r) { true })
.publish(model, :update)
end

it "should call the error handler on error" do
ex = Exception.new("Bad stuff happened")
topic_stub.stub(:publish) { raise ex }
Expand All @@ -60,4 +75,4 @@ def as_json
end
end

end
end