[Git][noosfero/noosfero][master] 2 commits: api: add method to update multiple blocks

Leandro Nunes gitlab at mg.gitlab.com
Mon Dec 12 16:09:50 BRST 2016


Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
befe5293 by Victor Costa at 2016-12-12T09:11:49-03:00
api: add method to update multiple blocks

- - - - -
3132e248 by Leandro Nunes at 2016-12-12T18:09:45+00:00
Merge branch 'api-update-blocks' into 'master'

api: add method to update multiple blocks

See merge request !1068
- - - - -


2 changed files:

- app/api/v1/blocks.rb
- test/api/blocks_test.rb


Changes:

=====================================
app/api/v1/blocks.rb
=====================================
--- a/app/api/v1/blocks.rb
+++ b/app/api/v1/blocks.rb
@@ -16,6 +16,28 @@ module Api
           block.update_attributes!(asset_with_images(params[:block]))
           present block, :with => Entities::Block, display_api_content: true, current_person: current_person
         end
+
+        patch do
+          error = nil
+          blocks = Block.transaction do
+            params["blocks"].map do |block_params|
+              block = Block.find(block_params["id"])
+              return forbidden! unless block.allow_edit?(current_person)
+              begin
+                block.update_attributes!(asset_with_images(block_params))
+              rescue => e
+                error = e
+                raise ActiveRecord::Rollback
+              end
+              block
+            end
+          end
+          if error.nil?
+            present blocks, :with => Entities::Block, current_person: current_person
+          else
+            error! error.message, 500
+          end
+        end
       end
     end
 


=====================================
test/api/blocks_test.rb
=====================================
--- a/test/api/blocks_test.rb
+++ b/test/api/blocks_test.rb
@@ -182,4 +182,38 @@ class BlocksTest < ActiveSupport::TestCase
     assert_equal 0, block.images.size
   end
 
+  should 'save multiple blocks' do
+    box = fast_create(Box, :owner_id => profile.id, :owner_type => Profile.name)
+    block = fast_create(Block, box_id: box.id)
+    block2 = fast_create(Block, box_id: box.id)
+    Environment.default.add_admin(person)
+    params[:blocks] = [{id: block.id, title: 'block1 title'}, {id: block2.id, title: 'block2 title'}]
+    patch "/api/v1/blocks?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_equal 200, last_response.status
+    assert_equal ['block1 title', 'block2 title'], json['blocks'].map {|b| b['title']}
+  end
+
+  should 'return forbidden when at least one block cannot be saved' do
+    box = fast_create(Box, :owner_id => person.id, :owner_type => Profile.name)
+    box2 = fast_create(Box, :owner_id => fast_create(Profile).id, :owner_type => Profile.name)
+    block = fast_create(Block, box_id: box.id)
+    block2 = fast_create(Block, box_id: box2.id)
+    params[:blocks] = [{id: block.id, title: 'block1 title'}, {id: block2.id, title: 'block2 title'}]
+    patch "/api/v1/blocks?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_equal 403, last_response.status
+  end
+
+  should 'not save any block modifications when an error was found' do
+    box = fast_create(Box, :owner_id => profile.id, :owner_type => Profile.name)
+    block = fast_create(Block, box_id: box.id, title: 'block1 title')
+    block2 = fast_create(Block, box_id: box.id, title: 'block2 title')
+    Environment.default.add_admin(person)
+    params[:blocks] = [{id: block.id, title: 'block1 title modified'}, {id: block2.id, title: 'block2 title modified', other_attribute: 'some value'}]
+    patch "/api/v1/blocks?#{params.to_query}"
+    assert_equal 500, last_response.status
+    assert_equal 'block1 title', block.reload.title
+    assert_equal 'block2 title', block2.reload.title
+  end
 end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/0d673be8451a9c81f2a0b4e7a4684fbcb8e0d561...3132e2487828da988d97241c778d65543bbdf20f
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20161212/a2873be4/attachment-0001.html>


More information about the Noosfero-dev mailing list