From 444dc49aec188a3b34fab3cc5d7c0ce2df26fabf Mon Sep 17 00:00:00 2001 From: Craig Savolainen Date: Wed, 10 Aug 2016 18:41:04 -0400 Subject: [PATCH] Allow update suppression to be configurable Currently updates will be suppressed if the model responds to #previous_changes and return an #empty? object. This allows each publisher to specify an :update_unless proc that will accept the record being processed. --- lib/multiple_man/mixins/publisher.rb | 6 +----- lib/multiple_man/model_publisher.rb | 14 +++++++++++--- spec/model_publisher_spec.rb | 19 +++++++++++++++++-- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/multiple_man/mixins/publisher.rb b/lib/multiple_man/mixins/publisher.rb index 3a0751b..d8ea7dd 100644 --- a/lib/multiple_man/mixins/publisher.rb +++ b/lib/multiple_man/mixins/publisher.rb @@ -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 diff --git a/lib/multiple_man/model_publisher.rb b/lib/multiple_man/model_publisher.rb index 921ba98..b6cd4c7 100644 --- a/lib/multiple_man/model_publisher.rb +++ b/lib/multiple_man/model_publisher.rb @@ -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 @@ -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) diff --git a/spec/model_publisher_spec.rb b/spec/model_publisher_spec.rb index 52fd763..0607029 100644 --- a/spec/model_publisher_spec.rb +++ b/spec/model_publisher_spec.rb @@ -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) } @@ -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 } @@ -60,4 +75,4 @@ def as_json end end -end \ No newline at end of file +end