Skip to content

Commit

Permalink
Merge pull request #44 from samvera-labs/support_new_requirement
Browse files Browse the repository at this point in the history
Support new Valkyrie save interface
  • Loading branch information
tpendragon committed Oct 19, 2021
2 parents 2e6932e + 366fee0 commit a7c9411
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
19 changes: 13 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,11 +121,18 @@ 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
return create(resource: resource, attributes: attributes) unless resource.persisted? && !exists?(id: attributes[:id])
update(resource: resource, attributes: attributes)
if exists?(id: attributes[:id])
update(resource: resource, attributes: attributes)
else
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

def create(resource:, attributes:)
Expand All @@ -146,7 +153,7 @@ def update(resource:, attributes:)
end

def exists?(id:)
resources.select(1).first(id: id).nil?
!resources.select(1).first(id: id).nil?
end
end
end
2 changes: 1 addition & 1 deletion lib/valkyrie/sequel/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true
module Valkyrie
module Sequel
VERSION = "2.2.1"
VERSION = "3.0.0.beta1"
end
end
2 changes: 1 addition & 1 deletion valkyrie-sequel.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "sequel", "~> 5.0"
spec.add_dependency "sequel_pg", "~> 1.0"
spec.add_dependency "valkyrie", ">= 2.1", "< 2.3"
spec.add_dependency "valkyrie", ">= 3.0.0.beta1"
spec.add_dependency "oj", "~> 3.0"
spec.add_development_dependency "bundler", "~> 2.0"
spec.add_development_dependency "rake"
Expand Down

0 comments on commit a7c9411

Please sign in to comment.