Skip to content

Commit

Permalink
allow events to override timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
desheikh committed Nov 18, 2023
1 parent 75e485d commit 9cd2fe7
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## 1.2.1 - 2023-11-18
### Changed
- Allow timestamp data in events to override default timestamps update.
- This allows us to create temporal events for example in the case of snapshots.

## 1.2.0 - 2023-09-26
### Changed
- Fix issue where the reactor worker might not work correctly for models not inheriting from ApplicationRecord.
Expand Down
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
eventsimple (1.2.0)
eventsimple (1.2.1)
dry-struct (~> 1.6)
dry-types (~> 1.7)
pg (~> 1.4)
Expand Down Expand Up @@ -89,7 +89,7 @@ GEM
coderay (1.1.3)
concurrent-ruby (1.2.2)
crass (1.0.6)
date (3.3.3)
date (3.3.4)
diff-lcs (1.5.0)
dry-core (1.0.1)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -158,12 +158,12 @@ GEM
mini_mime (1.1.5)
minitest (5.20.0)
nenv (0.3.0)
net-imap (0.3.7)
net-imap (0.4.5)
date
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.2.1)
net-protocol (0.2.2)
timeout
net-smtp (0.4.0)
net-protocol
Expand Down Expand Up @@ -300,7 +300,7 @@ GEM
lint_roller (~> 1.0)
rubocop-rails (~> 2.20.2)
thor (1.2.2)
timeout (0.4.0)
timeout (0.4.1)
treetop (1.6.12)
polyglot (~> 0.3)
tzinfo (2.0.6)
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,6 @@ module UserComponent
def apply(user)
user.canonical_id = data.canonical_id
user.email = data.email
user
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/eventsimple/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def reproject(at: nil)
assign_attributes(self.class.column_defaults.except(*ignore_props))

event_history.each do |event|
event.apply(self)
event.apply_timestamps(self)
event.apply(self)
end

self
Expand Down
2 changes: 1 addition & 1 deletion lib/eventsimple/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def extend_validation

# Apply the transformation to the aggregate and save it.
def apply_and_persist
apply(aggregate)
apply_timestamps(aggregate)
apply(aggregate)

# Persist!
aggregate.save!
Expand Down
2 changes: 2 additions & 0 deletions lib/eventsimple/support/spec_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@
end
end
end

RSpec::Matchers.define_negated_matcher(:not_change, :change)
2 changes: 1 addition & 1 deletion lib/eventsimple/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Eventsimple
VERSION = '1.2.0'
VERSION = '1.2.1'
end
1 change: 0 additions & 1 deletion spec/dummy/app/components/user_component/events/created.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def apply(user)
user.canonical_id = data.canonical_id
user.username = data.username
user.email = data.email
user
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions spec/dummy/app/components/user_component/events/deleted.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ def apply(user)

user.created_at ||= created_at
user.updated_at = created_at

user
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module UserComponent
module Events
class OverrideTimestamp < UserEvent
attribute :data, Eventsimple::DataType.new(self)

class Message < Eventsimple::Message
attribute :canonical_id, DryTypes::Strict::String
attribute :created_at, DryTypes::JSON::Time
attribute :updated_at, DryTypes::JSON::Time
end

def apply(user)
user.canonical_id = data.canonical_id
user.created_at = data.created_at
user.updated_at = data.updated_at
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def apply(user)
user.canonical_id = data.canonical_id
user.username = 'test'
user.email = '[email protected]'
user
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def apply(user)
user.canonical_id = data.canonical_id
user.username = 'test'
user.email = '[email protected]'
user
end
end
end
Expand Down
1 change: 0 additions & 1 deletion spec/dummy/app/components/user_component/events/updated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def can_apply?(user)

def apply(user)
user.email = data.email
user
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/db/migrate/20220917150826_create_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def change
create_table :users do |t|
t.string :canonical_id, null: false

t.string :username, null: false
t.string :username
t.string :email

t.integer :lock_version
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

create_table "users", force: :cascade do |t|
t.string "canonical_id", null: false
t.string "username", null: false
t.string "username"
t.string "email"
t.integer "lock_version"
t.datetime "deleted_at"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

RSpec.describe UserComponent::Events::OverrideTimestamp do
describe '#create' do
subject(:create_event) { event.save }

let(:canonical_id) { SecureRandom.uuid }

let(:user) { User.new }
let(:event) do
described_class.new(
user: user,
data: {
canonical_id: canonical_id,
created_at: Time.new(2023, 1, 1),
updated_at: Time.new(2023, 1, 1),
},
)
end

it 'updates the user properties' do
create_event

expect(user.canonical_id).to eq(event.data.canonical_id)
expect(user.created_at).to eq(event.data.created_at)
expect(user.updated_at).to eq(event.data.updated_at)
end
end
end
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
ENV['RAILS_ROOT'] ||= "#{File.dirname(__FILE__)}../../../spec/dummy"

require 'rspec/rails'
RSpec::Matchers.define_negated_matcher(:not_change, :change)

ActiveRecord::Migration.maintain_test_schema!

Expand Down

0 comments on commit 9cd2fe7

Please sign in to comment.