Skip to content

Commit

Permalink
Lift the restriction of Vorpal not allowing anyone else to set ids
Browse files Browse the repository at this point in the history
  • Loading branch information
sskirby committed Nov 26, 2020
1 parent c60e01b commit d229165
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ Vorpal.define do
end
```

CAVEAT: Vorpal currently does NOT SUPPORT anyone but Vorpal setting the id of an entity!

## API Documentation

http://rubydoc.info/github/nulogy/vorpal/main/frames
Expand Down
4 changes: 2 additions & 2 deletions lib/vorpal/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ def serialize(owned_objects, mapping, loaded_db_objects)
def serialize_object(object, config, loaded_db_objects)
if config.serialization_required?
attributes = config.serialize(object)
if object.id.nil?
db_object = loaded_db_objects.find_by_id(config, object.id)
if object.id.nil? || db_object.nil? # object doesn't exist in the DB
config.build_db_object(attributes)
else
db_object = loaded_db_objects.find_by_id(config, object.id)
config.set_db_object_attributes(db_object, attributes)
db_object
end
Expand Down
28 changes: 22 additions & 6 deletions spec/acceptance/vorpal/aggregate_mapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ def initialize(id: nil, name: "", trunk: nil, environment: nil, fissures: [], br
expect(tree_db.id).to eq tree.id
end

it 'allows the id to be set by clients' do
test_mapper = configure

tree = Tree.new(id: 999999)
test_mapper.persist(tree)

expect(tree.id).to be 999999

tree_db = db_class_for(Tree, test_mapper).first
expect(tree_db.id).to eq 999999
end

it 'saves AR::Base objects' do
test_mapper = configure

Expand Down Expand Up @@ -756,12 +768,7 @@ def initialize(id: nil, name: "")
end

it 'generates a UUID as a PK on create' do
engine = Vorpal.define do
map TreeUUID, id: :uuid do
attributes :name
end
end
mapper = engine.mapper_for(TreeUUID)
mapper = configure_uuid_id

tree = TreeUUID.new(name: 'new tree')
mapper.persist([tree])
Expand All @@ -772,6 +779,15 @@ def initialize(id: nil, name: "")

private

def configure_uuid_id
engine = Vorpal.define do
map TreeUUID, id: :uuid do
attributes :name
end
end
engine.mapper_for(TreeUUID)
end

def db_class_for(clazz, mapper)
mapper.engine.mapper_for(clazz).db_class
end
Expand Down

0 comments on commit d229165

Please sign in to comment.