[Git][noosfero/noosfero][master] 2 commits: api: accept type when creating a new block

Leandro Nunes gitlab at mg.gitlab.com
Tue Apr 25 15:18:14 BRT 2017


Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
1ae02b89 by Victor Costa at 2017-04-25T13:34:55-03:00
api: accept type when creating a new block

- - - - -
c5e601e1 by Leandro Nunes at 2017-04-25T18:17:58+00:00
Merge branch 'api-block-type' into 'master'

api: accept type when creating a new block

See merge request !1185
- - - - -


3 changed files:

- app/models/box.rb
- test/api/environment_test.rb
- test/api/profiles_test.rb


Changes:

=====================================
app/models/box.rb
=====================================
--- a/app/models/box.rb
+++ b/app/models/box.rb
@@ -74,10 +74,17 @@ class Box < ApplicationRecord
     ]
   end
 
+  def blocks_attributes=(attributes)
+    attributes.select { |b| b[:id].nil? }.each do |b|
+      block = b.delete(:type).constantize.new(b)
+      self.blocks << block
+    end
+    assign_nested_attributes_for_collection_association(:blocks, attributes.reject { |b| b[:id].nil? }.map { |b| b.except(:type) })
+  end
+
   private
 
   def to_css_selector(blocks_classes)
     blocks_classes.map{ |block_class| ".#{block_class.name.to_css_class}" }.join(',')
   end
-
 end


=====================================
test/api/environment_test.rb
=====================================
--- a/test/api/environment_test.rb
+++ b/test/api/environment_test.rb
@@ -198,10 +198,11 @@ class EnvironmentTest < ActiveSupport::TestCase
     environment.add_admin(person)
     environment.boxes << Box.new
 
-    block = { title: 'test' }
+    block = { title: 'test', type: RawHTMLBlock }
     params[:environment] = { boxes_attributes: [{id: environment.boxes.first.id, blocks_attributes: [block] }] }
     post "/api/v1/environments/#{environment.id}?#{params.to_query}"
     assert_equal ['test'], environment.reload.blocks.map(&:title)
+    assert_equal ['RawHTMLBlock'], environment.reload.blocks.map(&:type)
   end
 
   should 'remove blocks from environment' do


=====================================
test/api/profiles_test.rb
=====================================
--- a/test/api/profiles_test.rb
+++ b/test/api/profiles_test.rb
@@ -352,10 +352,11 @@ class ProfilesTest < ActiveSupport::TestCase
     community.add_member(person)
     community.boxes << Box.new
 
-    block = { title: 'test' }
+    block = { title: 'test', type: RawHTMLBlock }
     params.merge!({profile: {boxes_attributes: [{id: community.boxes.first.id, blocks_attributes: [block] }] } })
     post "/api/v1/profiles/#{community.id}?#{params.to_query}"
     assert_equal ['test'], community.reload.blocks.map(&:title)
+    assert_equal ['RawHTMLBlock'], community.reload.blocks.map(&:type)
   end
 
   should 'remove blocks in a profile' do



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/9fcfdaded3db6fc501a61e69f7b1c099b7705ea5...c5e601e18b62a664ac3ece6b2257355d2d9bc662

---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/9fcfdaded3db6fc501a61e69f7b1c099b7705ea5...c5e601e18b62a664ac3ece6b2257355d2d9bc662
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20170425/639d9b3e/attachment-0001.html>


More information about the Noosfero-dev mailing list