Skip to content

Commit

Permalink
Skip publish on destroy for non persisted object in AR (#62)
Browse files Browse the repository at this point in the history
* Skip publish when object is new and event destroy

* Replace return to next

* Fix rubocop

* Add spec

* Fix docs

* Fix version

* Fix docs

* bundle

* Fix docs and specs

* fix spec
  • Loading branch information
AS-AlStar authored Dec 2, 2021
1 parent 6edb1b0 commit 77eb529
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## [6.0.2] - 2021-12-01
### Fixed
- Fixed bug: skip publish when object is new and event is destroy for ActiveRecord

## [6.0.1] - 2021-11-30
### Fixed
- fixed docs
Expand Down
16 changes: 9 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
table_sync (6.0.1)
table_sync (6.0.2)
memery
rabbit_messaging
rails
Expand Down Expand Up @@ -91,7 +91,7 @@ GEM
activesupport (>= 5.0)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
lamian (1.2.0)
lamian (1.4.0)
rails (>= 4.2)
loofah (2.12.0)
crass (~> 1.0.2)
Expand All @@ -103,9 +103,11 @@ GEM
ruby2_keywords (~> 0.0.2)
method_source (1.0.0)
mini_mime (1.1.2)
mini_portile2 (2.6.1)
minitest (5.14.4)
nio4r (2.5.8)
nokogiri (1.12.5-x86_64-linux)
nokogiri (1.12.5)
mini_portile2 (~> 2.6.1)
racc (~> 1.4)
parallel (1.20.1)
parser (3.0.0.0)
Expand Down Expand Up @@ -207,7 +209,7 @@ GEM
sequel (5.43.0)
serverengine (2.0.7)
sigdump (~> 0.2.2)
set (1.0.1)
set (1.0.2)
sigdump (0.2.4)
simplecov (0.21.2)
docile (~> 1.1)
Expand All @@ -228,9 +230,9 @@ GEM
sprockets (4.0.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.2)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets-rails (3.4.1)
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
tainbox (2.1.2)
activesupport
Expand Down
19 changes: 18 additions & 1 deletion docs/publishing.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ class SomeOtherModel < Sequel::Model
end
```

### Non persisted record destruction

Sometimes destroy event can happen for non persisted record. In this case we can expect the following:

For Sequel: 'Sequel::NoExistingObject' is raised. (This is default Sequel behaviour)
For Active Record: Publishing is skipped.

Example:

```ruby
# ActiveRecord
user = User.new.destroy! # Publishing is skipped.

# Sequel
user = User.new.destroy! # raise Sequel::NoExistingObject
```

## Manual

Directly call one of the publishers. It's the best if you need to sync a lot of data.
Expand All @@ -60,4 +77,4 @@ Example:

- [Publishers](publishing/publishers.md)
- [Configuration](publishing/configuration.md)
- [Manual Sync (examples)](publishing/manual.md)
- [Manual Sync (examples)](publishing/manual.md)
2 changes: 2 additions & 0 deletions lib/table_sync/setup/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def define_after_commit(event)
options = options_exposed_for_block

object_class.after_commit(on: event) do
next if new_record? && destroyed?

if instance_eval(&options[:if]) && !instance_eval(&options[:unless])
TableSync::Publishing::Single.new(
object_class: self.class.name,
Expand Down
2 changes: 1 addition & 1 deletion lib/table_sync/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module TableSync
VERSION = "6.0.1"
VERSION = "6.0.2"
end
11 changes: 11 additions & 0 deletions spec/setup/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ def setup_sync(options = {})
setup_sync
end
end

context "when event destroy" do
before { setup_sync }

context "when user is new record" do
specify do
expect(job).not_to receive(:perform_at)
test_class.new.destroy
end
end
end
end
11 changes: 11 additions & 0 deletions spec/setup/sequel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ def setup_sync(options = {})
setup_sync
end
end

context "when event destroy" do
before { setup_sync }

context "when user is new record" do
specify do
expect(job).not_to receive(:perform_at)
expect { test_class.new.destroy }.to raise_error(Sequel::NoExistingObject)
end
end
end
end
11 changes: 11 additions & 0 deletions spec/support/shared/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,15 @@
include_examples "doesn't enqueue job"
end
end

context "when event destroy" do
before { setup_sync }

context "when user is persisted" do
specify do
expect(job).to receive(:perform_at)
test_class.first.destroy
end
end
end
end

0 comments on commit 77eb529

Please sign in to comment.