[Git][noosfero/noosfero][master] 3 commits: api: change blocks when edit a profile
Leandro Nunes
gitlab at mg.gitlab.com
Wed Apr 12 12:37:27 BRT 2017
Leandro Nunes pushed to branch master at Noosfero / noosfero
Commits:
0141317b by Victor Costa at 2017-04-10T09:02:29-03:00
api: change blocks when edit a profile
- - - - -
380dd137 by Victor Costa at 2017-04-10T10:21:25-03:00
api: change blocks when edit an environment
- - - - -
94d3e541 by Leandro Nunes at 2017-04-12T15:37:03+00:00
Merge branch 'api-edit-blocks-profile' into 'master'
api: change blocks when edit a profile
See merge request !1168
- - - - -
6 changed files:
- app/models/box.rb
- app/models/concerns/acts_as_having_boxes.rb
- app/models/environment.rb
- app/models/profile.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
@@ -4,8 +4,9 @@ class Box < ApplicationRecord
belongs_to :owner, :polymorphic => true
has_many :blocks, -> { order 'position' }, dependent: :destroy
+ accepts_nested_attributes_for :blocks, allow_destroy: true
- attr_accessible :owner
+ attr_accessible :owner, :blocks_attributes
include Noosfero::Plugin::HotSpot
=====================================
app/models/concerns/acts_as_having_boxes.rb
=====================================
--- a/app/models/concerns/acts_as_having_boxes.rb
+++ b/app/models/concerns/acts_as_having_boxes.rb
@@ -3,6 +3,7 @@ module ActsAsHavingBoxes
module ClassMethods
def acts_as_having_boxes
has_many :boxes, -> { order :position }, as: :owner, dependent: :destroy
+ accepts_nested_attributes_for :boxes
self.send(:include, ActsAsHavingBoxes)
end
end
@@ -34,4 +35,3 @@ module ActsAsHavingBoxes
end
end
-
=====================================
app/models/environment.rb
=====================================
--- a/app/models/environment.rb
+++ b/app/models/environment.rb
@@ -15,7 +15,7 @@ class Environment < ApplicationRecord
:members_whitelist, :highlighted_news_amount,
:portal_news_amount, :date_format, :signup_intro,
:enable_feed_proxy, :http_feed_proxy, :https_feed_proxy,
- :disable_feed_ssl, :layout_template
+ :disable_feed_ssl, :layout_template, :boxes_attributes
has_many :users
=====================================
app/models/profile.rb
=====================================
--- a/app/models/profile.rb
+++ b/app/models/profile.rb
@@ -12,7 +12,7 @@ class Profile < ApplicationRecord
:custom_url_redirection, :layout_template, :email_suggestions,
:allow_members_to_invite, :invite_friends_only, :secret,
:profile_admin_mail_notification, :allow_followers, :wall_access,
- :profile_kinds, :tag_list
+ :profile_kinds, :tag_list, :boxes_attributes
attr_accessor :old_region_id
=====================================
test/api/environment_test.rb
=====================================
--- a/test/api/environment_test.rb
+++ b/test/api/environment_test.rb
@@ -192,5 +192,40 @@ class EnvironmentTest < ActiveSupport::TestCase
assert_equal Api::Status::DEPRECATED, last_response.status
end
+ should 'add block in environment' do
+ login_api
+ environment = Environment.default
+ environment.add_admin(person)
+ environment.boxes << Box.new
+
+ block = { title: 'test' }
+ 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)
+ end
+
+ should 'remove blocks from environment' do
+ login_api
+ environment = Environment.default
+ environment.add_admin(person)
+ environment.boxes << Box.new
+ environment.boxes.first.blocks << Block.new(title: 'test')
+ block = { id: environment.boxes.first.blocks.first.id, _destroy: true }
+ params[:environment] = { boxes_attributes: [{id: environment.boxes.first.id, blocks_attributes: [block] }] }
+ post "/api/v1/environments/#{environment.id}?#{params.to_query}"
+ assert environment.reload.blocks.empty?
+ end
+ should 'edit block from environment' do
+ login_api
+ environment = Environment.default
+ environment.add_admin(person)
+ environment.boxes << Box.new
+ environment.boxes.first.blocks << Block.new(title: 'test')
+
+ block = { id: environment.boxes.first.blocks.first.id, title: 'test 2' }
+ params[:environment] = { boxes_attributes: [{id: environment.boxes.first.id, blocks_attributes: [block] }] }
+ post "/api/v1/environments/#{environment.id}?#{params.to_query}"
+ assert_equal ['test 2'], environment.reload.blocks.map(&:title)
+ end
end
=====================================
test/api/profiles_test.rb
=====================================
--- a/test/api/profiles_test.rb
+++ b/test/api/profiles_test.rb
@@ -345,4 +345,41 @@ class ProfilesTest < ActiveSupport::TestCase
assert_equal "blank", json['message']['name'].first['error']
assert_equal "not_available", json['message']['identifier'].first['error']
end
+
+ should 'add block in a profile' do
+ login_api
+ community = fast_create(Community)
+ community.add_member(person)
+ community.boxes << Box.new
+
+ block = { title: 'test' }
+ 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)
+ end
+
+ should 'remove blocks in a profile' do
+ login_api
+ community = fast_create(Community)
+ community.add_member(person)
+ community.boxes << Box.new
+ community.boxes.first.blocks << Block.new(title: 'test')
+ block = { id: community.boxes.first.blocks.first.id, _destroy: true }
+ params.merge!({profile: {boxes_attributes: [{id: community.boxes.first.id, blocks_attributes: [block] }] } })
+ post "/api/v1/profiles/#{community.id}?#{params.to_query}"
+ assert community.reload.blocks.empty?
+ end
+
+ should 'edit block in a profile' do
+ login_api
+ community = fast_create(Community)
+ community.add_member(person)
+ community.boxes << Box.new
+ community.boxes.first.blocks << Block.new(title: 'test')
+
+ block = { id: community.boxes.first.blocks.first.id, title: 'test 2' }
+ 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 2'], community.reload.blocks.map(&:title)
+ end
end
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/e3cef2d42e7661ca8d306f7574972427eef5364e...94d3e541c3842bbf2f8e182f6a27ebb7f48a95e9
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20170412/a0673926/attachment-0001.html>
More information about the Noosfero-dev
mailing list