Skip to content

Commit b4db278

Browse files
committed
Batch update at update_many
1 parent d0d3f4c commit b4db278

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

lib/zendesk_api/actions.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,19 @@ def update!(client, attributes = {}, &block)
276276
module UpdateMany
277277
# Updates multiple resources using the update_many endpoint.
278278
# @param [Client] client The {Client} object to be used
279-
# @param [Array] ids An array of ids to update
279+
# @param [Array] ids_or_attributes_array An array of ids or arributes including ids to update
280280
# @param [Hash] attributes The attributes to update resources with
281281
# @return [JobStatus] the {JobStatus} instance for this destroy job
282-
def update_many!(client, ids, attributes)
282+
def update_many!(client, ids_or_attributes_array, attributes = {})
283283
association = attributes.delete(:association) || Association.new(:class => self)
284284

285285
response = client.connection.put("#{association.generate_path}/update_many") do |req|
286-
req.params = { :ids => ids.join(',') }
287-
req.body = { singular_resource_name => attributes }
286+
if attributes == {}
287+
req.body = { resource_name => ids_or_attributes_array }
288+
else
289+
req.params = { :ids => ids_or_attributes_array.join(',') }
290+
req.body = { singular_resource_name => attributes }
291+
end
288292

289293
yield req if block_given?
290294
end

spec/core/bulk_actions_spec.rb

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,44 @@
4747
subject { ZendeskAPI::BulkTestResource }
4848

4949
context "update_many!" do
50-
let(:attributes) { { :name => 'A', :age => 25 } }
50+
context "arity: 3" do
51+
let(:attributes) { { :name => 'A', :age => 25 } }
5152

52-
before(:each) do
53-
stub_json_request(:put, %r{bulk_test_resources/update_many}, json(:job_status => { :id => 'ghi' }))
54-
@response = subject.update_many!(client, [1, 2, 3], attributes)
55-
end
53+
before(:each) do
54+
stub_json_request(:put, %r{bulk_test_resources/update_many}, json(:job_status => { :id => 'ghi' }))
55+
@response = subject.update_many!(client, [1, 2, 3], attributes)
56+
end
57+
58+
it 'calls the update_many endpoint' do
59+
assert_requested(:put, %r{bulk_test_resources/update_many\?ids=1,2,3$},
60+
:body => json(:bulk_test_resource => attributes)
61+
)
62+
end
5663

57-
it 'calls the update_many endpoint' do
58-
assert_requested(:put, %r{bulk_test_resources/update_many\?ids=1,2,3$},
59-
:body => json(:bulk_test_resource => attributes)
60-
)
64+
it 'returns a JobStatus' do
65+
expect(@response).to be_instance_of(ZendeskAPI::JobStatus)
66+
expect(@response.id).to eq('ghi')
67+
end
6168
end
6269

63-
it 'returns a JobStatus' do
64-
expect(@response).to be_instance_of(ZendeskAPI::JobStatus)
65-
expect(@response.id).to eq('ghi')
70+
context "arity: 2" do
71+
let(:attributes_array) { [{ :id => 1, :name => 'A' }, { :id => 2, :name => 'B' }] }
72+
73+
before(:each) do
74+
stub_json_request(:put, %r{bulk_test_resources/update_many}, json(:job_status => { :id => 'jkl' }))
75+
@response = subject.update_many!(client, attributes_array)
76+
end
77+
78+
it 'calls the update_many endpoint' do
79+
assert_requested(:put, %r{bulk_test_resources/update_many$},
80+
:body => json(:bulk_test_resources => attributes_array)
81+
)
82+
end
83+
84+
it 'returns a JobStatus' do
85+
expect(@response).to be_instance_of(ZendeskAPI::JobStatus)
86+
expect(@response.id).to eq('jkl')
87+
end
6688
end
6789
end
6890
end

0 commit comments

Comments
 (0)