Skip to content

Commit

Permalink
Fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpendragon committed Jul 28, 2021
1 parent 219b958 commit e94c1b8
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/valkyrie/sequel/persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ def initialize(adapter:)
@adapter = adapter
end

def save(resource:)
def save(resource:, external_resource: false)
object_attributes = resource_factory.from_resource(resource: resource)
output = create_or_update(resource: resource, attributes: object_attributes)
output = create_or_update(resource: resource, attributes: object_attributes, external_resource: external_resource)
resource_factory.to_resource(object: output)
end

Expand Down Expand Up @@ -121,14 +121,17 @@ def wipe!

private

def create_or_update(resource:, attributes:)
def create_or_update(resource:, attributes:, external_resource:)
attributes[:updated_at] = Time.now.utc
attributes[:created_at] ||= Time.now.utc
if resource.persisted?
raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time." unless exists?(id: attributes[:id])
if exists?(id: attributes[:id])
update(resource: resource, attributes: attributes)
else
create(resource: resource, attributes: attributes) unless resource.persisted? && !exists?(id: attributes[:id])
if !external_resource && resource.persisted?
# This resource has been deleted in the meantime, error.
raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time." unless exists?(id: attributes[:id])
end
create(resource: resource, attributes: attributes)
end
end

Expand Down

0 comments on commit e94c1b8

Please sign in to comment.