diff --git a/Gemfile b/Gemfile index 71e4bf3..50911f3 100644 --- a/Gemfile +++ b/Gemfile @@ -5,3 +5,4 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } # Specify your gem's dependencies in valkyrie-sequel.gemspec gemspec +gem "valkyrie", branch: "837-persisters-should-error", github: "samvera/valkyrie" diff --git a/lib/valkyrie/sequel/persister.rb b/lib/valkyrie/sequel/persister.rb index 5149f96..6b8ab50 100644 --- a/lib/valkyrie/sequel/persister.rb +++ b/lib/valkyrie/sequel/persister.rb @@ -124,8 +124,12 @@ def wipe! def create_or_update(resource:, attributes:) 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 resource.persisted? + raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time." unless exists?(id: attributes[:id]) + update(resource: resource, attributes: attributes) + else + create(resource: resource, attributes: attributes) unless resource.persisted? && !exists?(id: attributes[:id]) + end end def create(resource:, attributes:) @@ -146,7 +150,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