Skip to content

Commit 90ff178

Browse files
committed
Add save deny list to base REST resource
1 parent 4fdb0fc commit 90ff178

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Changelog
2+
3+
## Unreleased
4+
5+
- [#919](https://github.com/Shopify/shopify_api/pull/919) Allow REST resources to configure a deny list of attributes to be excluded when saving
6+
17
## Version 10.0.0
28

39
- Major update to the library to provide _all_ essential functions needed for a Shopify app, supporting embedded apps with session tokens. See the [full list of changes](https://github.com/Shopify/shopify_api#breaking-change-notice-for-version-1000) here

lib/shopify_api/rest/base.rb

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Base
1515
@has_many = T.let({}, T::Hash[Symbol, Class])
1616
@paths = T.let([], T::Array[T::Hash[Symbol, T.any(T::Array[Symbol], String, Symbol)]])
1717
@custom_prefix = T.let(nil, T.nilable(String))
18+
@attribute_save_deny_list = T.let([], T.nilable(T::Array[Symbol]))
1819

1920
sig { returns(T::Hash[Symbol, T.untyped]) }
2021
attr_accessor :original_state
@@ -118,6 +119,11 @@ def has_one?(attribute)
118119
@has_one.include?(attribute)
119120
end
120121

122+
sig { returns(T.nilable(T::Array[Symbol])) }
123+
def attribute_save_deny_list
124+
@attribute_save_deny_list&.map { |a| :"@#{a}" }
125+
end
126+
121127
sig do
122128
params(
123129
http_method: Symbol,
@@ -259,6 +265,7 @@ def to_hash
259265
hash = {}
260266
instance_variables.each do |var|
261267
next if [:"@original_state", :"@session", :"@client", :"@forced_nils", :"@errors"].include?(var)
268+
next if self.class.attribute_save_deny_list&.include?(var)
262269

263270
attribute = var.to_s.delete("@").to_sym
264271
if self.class.has_many?(attribute)

test/clients/base_rest_resource_test.rb

+17
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,23 @@ def test_saves_with_children
146146
assert_requested(stubbed_request)
147147
end
148148

149+
def test_save_ignores_unsaveable_attributes
150+
request_body = { fake_resource: { attribute: "attribute" } }.to_json
151+
response_body = { fake_resource: { id: 1, attribute: "attribute" } }.to_json
152+
153+
stubbed_request = stub_request(:post, "#{@prefix}/fake_resources.json")
154+
.with(body: request_body)
155+
.to_return(status: 201, body: response_body)
156+
157+
resource = TestHelpers::FakeResource.new(session: @session)
158+
resource.attribute = "attribute"
159+
resource.unsaveable_attribute = "this is an attribute"
160+
161+
resource.save
162+
assert_requested(stubbed_request)
163+
assert_nil(resource.id)
164+
end
165+
149166
def test_deletes_existing_resource_and_fails_on_deleting_nonexistent_resource
150167
resource = TestHelpers::FakeResource.new(session: @session)
151168
resource.id = 1

test/test_helpers/fake_resource.rb

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class FakeResource < ShopifyAPI::Rest::Base
1616
@prev_page_info = T.let(Concurrent::ThreadLocalVar.new { nil }, Concurrent::ThreadLocalVar)
1717
@next_page_info = T.let(Concurrent::ThreadLocalVar.new { nil }, Concurrent::ThreadLocalVar)
1818

19+
@attribute_save_deny_list = T.let([:unsaveable_attribute], T::Array[Symbol])
20+
1921
@paths = T.let([
2022
{ http_method: :get, operation: :get, ids: [], path: "fake_resources.json" },
2123
{ http_method: :post, operation: :post, ids: [], path: "fake_resources.json" },
@@ -36,6 +38,7 @@ def initialize(session: nil)
3638
@has_one_attribute = T.let(nil, T.nilable(FakeResource))
3739
@has_many_attribute = T.let(nil, T.nilable(T::Array[FakeResource]))
3840
@other_resource_id = T.let(nil, T.nilable(Integer))
41+
@unsaveable_attribute = T.let(nil, T.nilable(String))
3942
end
4043

4144
sig { returns(T.nilable(Integer)) }
@@ -53,6 +56,9 @@ def initialize(session: nil)
5356
sig { returns(T.nilable(Integer)) }
5457
attr_reader :other_resource_id
5558

59+
sig { returns(T.nilable(String)) }
60+
attr_reader :unsaveable_attribute
61+
5662
class << self
5763
sig do
5864
params(id: T.any(Integer, String), session: ShopifyAPI::Auth::Session, param: T.untyped,

0 commit comments

Comments
 (0)