[Git][noosfero/noosfero][master] 3 commits: api: accept parameters to build api content for blocks

Leandro Nunes gitlab at mg.gitlab.com
Fri Sep 30 17:26:49 BRT 2016


Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
e8398c7d by Victor Costa at 2016-09-29T16:25:01-03:00
api: accept parameters to build api content for blocks

- - - - -
bdbf69e0 by Victor Costa at 2016-09-29T16:28:40-03:00
breadcrumbs: return links in api_content

- - - - -
24b3013c by Leandro Nunes at 2016-09-30T20:26:30+00:00
Merge branch 'block_api_content_params' into 'master'

api: accept parameters to build api content for blocks

Also, return links in api_content for breadcrumb block.

See merge request !1022
- - - - -


5 changed files:

- app/api/v1/blocks.rb
- app/models/block.rb
- plugins/breadcrumbs/lib/breadcrumbs_plugin/content_breadcrumbs_block.rb
- plugins/breadcrumbs/test/unit/content_breadcrumbs_block_test.rb
- test/api/blocks_test.rb


Changes:

=====================================
app/api/v1/blocks.rb
=====================================
--- a/app/api/v1/blocks.rb
+++ b/app/api/v1/blocks.rb
@@ -6,6 +6,7 @@ module Api
         get ':id' do
           block = Block.find(params["id"])
           return forbidden! unless block.visible_to_user?(current_person) || block.allow_edit?(current_person)
+          block.api_content_params = params.except("id")
           present block, :with => Entities::Block, display_api_content: true, current_person: current_person
         end
 


=====================================
app/models/block.rb
=====================================
--- a/app/models/block.rb
+++ b/app/models/block.rb
@@ -320,6 +320,8 @@ class Block < ApplicationRecord
     false
   end
 
+  attr_accessor :api_content_params
+
   private
 
   def home_page_path


=====================================
plugins/breadcrumbs/lib/breadcrumbs_plugin/content_breadcrumbs_block.rb
=====================================
--- a/plugins/breadcrumbs/lib/breadcrumbs_plugin/content_breadcrumbs_block.rb
+++ b/plugins/breadcrumbs/lib/breadcrumbs_plugin/content_breadcrumbs_block.rb
@@ -26,4 +26,31 @@ class BreadcrumbsPlugin::ContentBreadcrumbsBlock < Block
     false
   end
 
+  def api_content
+    links = []
+    links << profile_link
+    links << page_links
+    { links: links.compact.flatten }
+  end
+
+  private
+
+  def profile_link
+    return nil if (api_content_params || {})[:profile].blank?
+    profile = environment.profiles.find_by(identifier: api_content_params[:profile])
+    return nil if profile.blank?
+    { :name => profile.name, :url => "/#{profile.identifier}" }
+  end
+
+  def page_links
+    return nil if (api_content_params || {})[:page].blank?
+    page = owner.articles.find_by(path: api_content_params[:page])
+    return nil if page.blank?
+    page_trail(page)
+  end
+
+  def page_trail(page)
+    links = page.ancestors.reverse.map { |p| { :name => p.title, :url => p.full_path } } || []
+    links << { :name => page.title, :url => page.full_path }
+  end
 end


=====================================
plugins/breadcrumbs/test/unit/content_breadcrumbs_block_test.rb
=====================================
--- a/plugins/breadcrumbs/test/unit/content_breadcrumbs_block_test.rb
+++ b/plugins/breadcrumbs/test/unit/content_breadcrumbs_block_test.rb
@@ -5,9 +5,13 @@ class ContentBreadcrumbsBlockTest < ActiveSupport::TestCase
   include NoosferoTestHelper
 
   def setup
-    @block = BreadcrumbsPlugin::ContentBreadcrumbsBlock.new
+    @profile = fast_create(Profile)
+    box = Box.create!(owner: profile)
+    @block = fast_create(BreadcrumbsPlugin::ContentBreadcrumbsBlock, box_id: box.id)
   end
 
+  attr_accessor :block, :profile
+
   should 'has a description' do
     assert_not_equal Block.description, BreadcrumbsPlugin::ContentBreadcrumbsBlock.description
   end
@@ -16,11 +20,18 @@ class ContentBreadcrumbsBlockTest < ActiveSupport::TestCase
     assert @block.help
   end
 
-
   should 'not be cacheable' do
     refute @block.cacheable?
   end
 
+  should 'return page links in api_content' do
+    folder = fast_create(Folder, profile_id: profile.id, name: 'folder')
+    article = Article.create!(profile: profile, parent: folder, name: 'child')
+    block.api_content_params = { page: article.path, profile: profile.identifier }
+    links = block.api_content[:links]
+    assert_equal [profile.name, 'folder', 'child'], links.map {|l| l[:name]}
+    assert_equal article.full_path, links.last[:url]
+  end
 end
 
 require 'boxes_helper'


=====================================
test/api/blocks_test.rb
=====================================
--- a/test/api/blocks_test.rb
+++ b/test/api/blocks_test.rb
@@ -141,4 +141,18 @@ class BlocksTest < ActiveSupport::TestCase
     json = JSON.parse(last_response.body)
     assert_includes json["block"]["permissions"], 'allow_edit'
   end
+
+  should 'get a block with api content params' do
+    class MyTestBlock < Block
+      def api_content
+        api_content_params
+      end
+    end
+    box = fast_create(Box, :owner_id => environment.id, :owner_type => Environment.name)
+    block = fast_create(MyTestBlock, box_id: box.id)
+    params["custom_param"] = "custom_value"
+    get "/api/v1/blocks/#{block.id}?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_equal "custom_value", json["block"]["api_content"]["custom_param"]
+  end
 end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/1445c0a767a994e4f680170e647314b301516bec...24b3013c55d813711287b55a931be84eba1fb419
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20160930/efd1ce13/attachment-0001.html>


More information about the Noosfero-dev mailing list