Skip to content

Commit

Permalink
entities can only be written to by events by default
Browse files Browse the repository at this point in the history
  • Loading branch information
desheikh committed Jan 23, 2024
1 parent c573847 commit 69c7d29
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 197 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/doc/
/log/*.log
/pkg/
/spec/examples.txt
/spec/reports/
/tmp/
.idea
Expand Down
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.3.0 - 2024-01-14
### Changed
- Prevent manual write access to the entity table by default. This is to prevent
accidental writes to the entity table. Use entity.enable_writes! to enable writes.

## 1.2.3 - 2024-01-11
### Changed
- Fix rendering of deleted events
Expand Down
8 changes: 7 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
eventsimple (1.2.3)
eventsimple (1.3.0)
dry-struct (~> 1.6)
dry-types (~> 1.7)
pg (~> 1.4)
Expand Down Expand Up @@ -125,6 +125,11 @@ GEM
dry-logic (~> 1.4)
zeitwerk (~> 2.6)
erubi (1.12.0)
factory_bot (6.4.5)
activesupport (>= 5.0.0)
factory_bot_rails (6.4.3)
factory_bot (~> 6.4)
railties (>= 5.0.0)
ffi (1.16.3)
formatador (1.1.0)
fuubar (2.5.1)
Expand Down Expand Up @@ -358,6 +363,7 @@ PLATFORMS
DEPENDENCIES
bundle-audit
eventsimple!
factory_bot_rails
fuubar
git
guard-rspec
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,24 @@ Create a rake task to run the consumer
## Helper methods
Some convenience methods are provided to help with common use cases.

**`#enable_writes!`**
Write access on entities is disabled by default outside of writes via events. Use this method to enable writes on an entity.

```ruby
user = User.find_by(canonical_id: 'user-123')
user.enable_writes! do
user.reproject
user.save!
end
```

If you are using FactoryBot, you can add the following in your rails_helper.rb to enable writes on the entity:
```ruby
FactoryBot.define do
after(:build) { |model| model.enable_writes! if model.class.ancestors.include?(Eventsimple::Entity::InstanceMethods) }
end
``

**`#reproject(at: nil)`**

Reproject an entity from events (rebuilds in memory but does not persist the entity).
Expand Down
1 change: 1 addition & 0 deletions eventsimple.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'retriable', '~> 3.1'

spec.add_development_dependency 'bundle-audit'
spec.add_development_dependency 'factory_bot_rails'
spec.add_development_dependency 'fuubar'
spec.add_development_dependency 'git'
spec.add_development_dependency 'guard-rspec'
Expand Down
11 changes: 11 additions & 0 deletions lib/eventsimple/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def event_driven_by(event_klass, aggregate_id:, filter_attributes: [])
autosave: false,
validate: false

after_initialize :readonly!

class_attribute :ignored_for_projection, default: []

class_attribute :_filter_attributes
Expand All @@ -32,6 +34,15 @@ def projection_matches_events?
attributes == reprojected.attributes
end

def enable_writes!(&block)
@readonly = false

if block
yield
@readonly = true
end
end

def reproject(at: nil)
event_history = at ? events.where('created_at <= ?', at).load : events.load
ignore_props = (DEFAULT_IGNORE_PROPS + ignored_for_projection).map(&:to_s)
Expand Down
2 changes: 2 additions & 0 deletions lib/eventsimple/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def apply_and_persist
apply(aggregate)

# Persist!
aggregate.enable_writes!
aggregate.save!
aggregate.readonly!

self.aggregate = aggregate
end
Expand Down
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.3'
VERSION = '1.3.0'
end
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,7 @@
end

context 'when can_apply? check fails' do
let(:user) {
User.create(
canonical_id: canonical_id,
username: 'test-user',
email: '[email protected]',
created_at: Time.current,
updated_at: Time.current,
)
}
let(:user) { create(:user, canonical_id: canonical_id) }

it_behaves_like 'an event in invalid state'
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@
end

context 'when can_apply? check fails' do
let(:user) {
User.create(
canonical_id: canonical_id,
username: 'test-user',
email: '[email protected]',
created_at: Time.current,
updated_at: Time.current,
)
}
let(:user) { create(:user, canonical_id: canonical_id) }

it_behaves_like 'an event in invalid state that is rescued'
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,7 @@
end

context 'when can_apply? check fails' do
let(:user) {
User.create(
canonical_id: canonical_id,
username: 'test-user',
email: '[email protected]',
created_at: Time.current,
updated_at: Time.current,
)
}
let(:user) { create(:user, canonical_id: canonical_id) }

it_behaves_like 'an event in invalid state'
end
Expand Down
11 changes: 11 additions & 0 deletions spec/dummy/spec/factories/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

FactoryBot.define do
factory :user do
canonical_id { SecureRandom.uuid }
username { 'test-user' }
email { '[email protected]' }
created_at { Time.current }
updated_at { Time.current }
end
end
64 changes: 0 additions & 64 deletions spec/dummy/spec/rails_helper.rb

This file was deleted.

93 changes: 0 additions & 93 deletions spec/dummy/spec/spec_helper.rb

This file was deleted.

Loading

0 comments on commit 69c7d29

Please sign in to comment.