File tree Expand file tree Collapse file tree 5 files changed +56
-4
lines changed
spec/userlist/push/operations Expand file tree Collapse file tree 5 files changed +56
-4
lines changed Original file line number Diff line number Diff line change @@ -5,8 +5,9 @@ module Create
55 module ClassMethods
66 def create ( payload = { } , config = self . config )
77 return false unless resource = from_payload ( payload , config )
8+ return false unless resource . create?
89
9- strategy . call ( :post , endpoint , resource ) if resource . create?
10+ strategy . call ( :post , endpoint , resource . for_context ( : create) )
1011 end
1112
1213 alias push create
Original file line number Diff line number Diff line change @@ -5,8 +5,9 @@ module Delete
55 module ClassMethods
66 def delete ( payload = { } , config = self . config )
77 return false unless resource = from_payload ( payload , config )
8+ return false unless resource . delete?
89
9- strategy . call ( :delete , endpoint , resource ) if resource . delete?
10+ strategy . call ( :delete , endpoint , resource . for_context ( : delete) )
1011 end
1112 end
1213
Original file line number Diff line number Diff line change @@ -58,13 +58,14 @@ def generated_methods
5858 end
5959 end
6060
61- attr_reader :payload , :config
61+ attr_reader :payload , :config , :context
6262
6363 def initialize ( payload = { } , config = Userlist . config )
6464 raise Userlist ::ArgumentError , 'Missing required payload' unless payload
6565
6666 @payload = payload
6767 @config = config
68+ @context = :push
6869 end
6970
7071 def respond_to_missing? ( method , include_private = false )
@@ -73,7 +74,7 @@ def respond_to_missing?(method, include_private = false)
7374 end
7475
7576 def to_hash
76- Serializer . serialize ( self , context : :push )
77+ Serializer . serialize ( self , context : context )
7778 end
7879 alias to_h to_hash
7980
@@ -106,6 +107,12 @@ def push?
106107 true
107108 end
108109
110+ def for_context ( context )
111+ dup . tap do |instance |
112+ instance . instance_variable_set ( :@context , context )
113+ end
114+ end
115+
109116 private
110117
111118 def method_missing ( method , *args , &block )
Original file line number Diff line number Diff line change 3636 expect ( strategy ) . to receive ( :call ) . with ( :post , '/users' , resource )
3737 relation . create ( payload )
3838 end
39+
40+ it 'should set the context to :create' do
41+ expect ( strategy ) . to receive ( :call ) . with ( :post , '/users' , satisfy { |r | r . context == :create } )
42+ relation . create ( payload )
43+ end
44+ end
45+
46+ context 'when given a resource instance' do
47+ let ( :payload ) { resource_type . from_payload ( { identifier : 'identifier' } ) }
48+
49+ it 'should send the request to the endpoint' do
50+ expect ( strategy ) . to receive ( :call ) . with ( :post , '/users' , resource )
51+ relation . create ( payload )
52+ end
53+
54+ it 'should set the context to :create' do
55+ expect ( strategy ) . to receive ( :call ) . with ( :post , '/users' , satisfy { |r | r . context == :create } )
56+ relation . create ( payload )
57+ end
3958 end
4059
4160 context 'when given an identifier' do
Original file line number Diff line number Diff line change 1717 expect ( strategy ) . to receive ( :call ) . with ( :delete , '/users' , resource )
1818 relation . delete ( payload )
1919 end
20+
21+ it 'should set the context to :create' do
22+ expect ( strategy ) . to receive ( :call ) . with ( :delete , '/users' , satisfy { |r | r . context == :delete } )
23+ relation . delete ( payload )
24+ end
2025 end
2126
2227 context 'when given a payload hash' do
2833 expect ( strategy ) . to receive ( :call ) . with ( :delete , '/users' , resource )
2934 relation . delete ( payload )
3035 end
36+
37+ it 'should set the context to :create' do
38+ expect ( strategy ) . to receive ( :call ) . with ( :delete , '/users' , satisfy { |r | r . context == :delete } )
39+ relation . delete ( payload )
40+ end
41+ end
42+
43+ context 'when given a resource instance' do
44+ let ( :payload ) { resource_type . from_payload ( { identifier : identifier } ) }
45+
46+ it 'should send the request to the endpoint' do
47+ expect ( strategy ) . to receive ( :call ) . with ( :delete , '/users' , resource )
48+ relation . delete ( payload )
49+ end
50+
51+ it 'should set the context to :create' do
52+ expect ( strategy ) . to receive ( :call ) . with ( :delete , '/users' , satisfy { |r | r . context == :delete } )
53+ relation . delete ( payload )
54+ end
3155 end
3256
3357 context 'when given nil' do
You can’t perform that action at this time.
0 commit comments