From 6f7ed7eadf2f62addadfad929d0a337129a91abe Mon Sep 17 00:00:00 2001 From: Lee Richmond Date: Mon, 24 Aug 2020 14:10:27 -0400 Subject: [PATCH] Ensure scope override is honored on update/destroy If you were passing a scope override for an update or destroy operation, that scope wouldn't be properly honored. This PR ensures an error will be correctly thrown if no record is found. The downside is we look up the record twice, now, but I think we should get a fix in ASAP. --- lib/graphiti/resource_proxy.rb | 2 ++ lib/graphiti/version.rb | 2 +- spec/persistence_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/graphiti/resource_proxy.rb b/lib/graphiti/resource_proxy.rb index ba69f4c0..0fc6e45a 100644 --- a/lib/graphiti/resource_proxy.rb +++ b/lib/graphiti/resource_proxy.rb @@ -118,6 +118,7 @@ def save(action: :create) end def destroy + data transaction_response = @resource.transaction do metadata = {method: :destroy} model = @resource.destroy(@query.filters[:id], metadata) @@ -135,6 +136,7 @@ def destroy end def update_attributes + data save(action: :update) end diff --git a/lib/graphiti/version.rb b/lib/graphiti/version.rb index 6ee5b3ac..69667898 100644 --- a/lib/graphiti/version.rb +++ b/lib/graphiti/version.rb @@ -1,3 +1,3 @@ module Graphiti - VERSION = "1.2.21" + VERSION = "1.2.22" end diff --git a/spec/persistence_spec.rb b/spec/persistence_spec.rb index 45005865..32723e46 100644 --- a/spec/persistence_spec.rb +++ b/spec/persistence_spec.rb @@ -32,6 +32,40 @@ def self.name expect(employee.data.first_name).to eq("Jane") end + describe 'updating' do + let!(:employee) { PORO::Employee.create(first_name: 'asdf') } + + before do + payload[:data][:id] = employee.id.to_s + end + + describe 'with scope override' do + it 'is honored' do + employee = klass.find(payload, { type: 'foo' }) + expect { + employee.update_attributes + }.to raise_error(Graphiti::Errors::RecordNotFound) + end + end + end + + describe 'destroying' do + let!(:employee) { PORO::Employee.create(first_name: 'asdf') } + + before do + payload[:data][:id] = employee.id.to_s + end + + describe 'with scope override' do + it 'is honored' do + employee = klass.find(payload, { type: 'foo' }) + expect { + employee.destroy + }.to raise_error(Graphiti::Errors::RecordNotFound) + end + end + end + describe "overrides" do before do klass.class_eval do