Commit 24b3013c55d813711287b55a931be84eba1fb419
Exists in
staging
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
Showing
5 changed files
with
57 additions
and
2 deletions
Show diff stats
app/api/v1/blocks.rb
@@ -6,6 +6,7 @@ module Api | @@ -6,6 +6,7 @@ module Api | ||
6 | get ':id' do | 6 | get ':id' do |
7 | block = Block.find(params["id"]) | 7 | block = Block.find(params["id"]) |
8 | return forbidden! unless block.visible_to_user?(current_person) || block.allow_edit?(current_person) | 8 | return forbidden! unless block.visible_to_user?(current_person) || block.allow_edit?(current_person) |
9 | + block.api_content_params = params.except("id") | ||
9 | present block, :with => Entities::Block, display_api_content: true, current_person: current_person | 10 | present block, :with => Entities::Block, display_api_content: true, current_person: current_person |
10 | end | 11 | end |
11 | 12 |
app/models/block.rb
plugins/breadcrumbs/lib/breadcrumbs_plugin/content_breadcrumbs_block.rb
@@ -26,4 +26,31 @@ class BreadcrumbsPlugin::ContentBreadcrumbsBlock < Block | @@ -26,4 +26,31 @@ class BreadcrumbsPlugin::ContentBreadcrumbsBlock < Block | ||
26 | false | 26 | false |
27 | end | 27 | end |
28 | 28 | ||
29 | + def api_content | ||
30 | + links = [] | ||
31 | + links << profile_link | ||
32 | + links << page_links | ||
33 | + { links: links.compact.flatten } | ||
34 | + end | ||
35 | + | ||
36 | + private | ||
37 | + | ||
38 | + def profile_link | ||
39 | + return nil if (api_content_params || {})[:profile].blank? | ||
40 | + profile = environment.profiles.find_by(identifier: api_content_params[:profile]) | ||
41 | + return nil if profile.blank? | ||
42 | + { :name => profile.name, :url => "/#{profile.identifier}" } | ||
43 | + end | ||
44 | + | ||
45 | + def page_links | ||
46 | + return nil if (api_content_params || {})[:page].blank? | ||
47 | + page = owner.articles.find_by(path: api_content_params[:page]) | ||
48 | + return nil if page.blank? | ||
49 | + page_trail(page) | ||
50 | + end | ||
51 | + | ||
52 | + def page_trail(page) | ||
53 | + links = page.ancestors.reverse.map { |p| { :name => p.title, :url => p.full_path } } || [] | ||
54 | + links << { :name => page.title, :url => page.full_path } | ||
55 | + end | ||
29 | end | 56 | end |
plugins/breadcrumbs/test/unit/content_breadcrumbs_block_test.rb
@@ -5,9 +5,13 @@ class ContentBreadcrumbsBlockTest < ActiveSupport::TestCase | @@ -5,9 +5,13 @@ class ContentBreadcrumbsBlockTest < ActiveSupport::TestCase | ||
5 | include NoosferoTestHelper | 5 | include NoosferoTestHelper |
6 | 6 | ||
7 | def setup | 7 | def setup |
8 | - @block = BreadcrumbsPlugin::ContentBreadcrumbsBlock.new | 8 | + @profile = fast_create(Profile) |
9 | + box = Box.create!(owner: profile) | ||
10 | + @block = fast_create(BreadcrumbsPlugin::ContentBreadcrumbsBlock, box_id: box.id) | ||
9 | end | 11 | end |
10 | 12 | ||
13 | + attr_accessor :block, :profile | ||
14 | + | ||
11 | should 'has a description' do | 15 | should 'has a description' do |
12 | assert_not_equal Block.description, BreadcrumbsPlugin::ContentBreadcrumbsBlock.description | 16 | assert_not_equal Block.description, BreadcrumbsPlugin::ContentBreadcrumbsBlock.description |
13 | end | 17 | end |
@@ -16,11 +20,18 @@ class ContentBreadcrumbsBlockTest < ActiveSupport::TestCase | @@ -16,11 +20,18 @@ class ContentBreadcrumbsBlockTest < ActiveSupport::TestCase | ||
16 | assert @block.help | 20 | assert @block.help |
17 | end | 21 | end |
18 | 22 | ||
19 | - | ||
20 | should 'not be cacheable' do | 23 | should 'not be cacheable' do |
21 | refute @block.cacheable? | 24 | refute @block.cacheable? |
22 | end | 25 | end |
23 | 26 | ||
27 | + should 'return page links in api_content' do | ||
28 | + folder = fast_create(Folder, profile_id: profile.id, name: 'folder') | ||
29 | + article = Article.create!(profile: profile, parent: folder, name: 'child') | ||
30 | + block.api_content_params = { page: article.path, profile: profile.identifier } | ||
31 | + links = block.api_content[:links] | ||
32 | + assert_equal [profile.name, 'folder', 'child'], links.map {|l| l[:name]} | ||
33 | + assert_equal article.full_path, links.last[:url] | ||
34 | + end | ||
24 | end | 35 | end |
25 | 36 | ||
26 | require 'boxes_helper' | 37 | require 'boxes_helper' |
test/api/blocks_test.rb
@@ -141,4 +141,18 @@ class BlocksTest < ActiveSupport::TestCase | @@ -141,4 +141,18 @@ class BlocksTest < ActiveSupport::TestCase | ||
141 | json = JSON.parse(last_response.body) | 141 | json = JSON.parse(last_response.body) |
142 | assert_includes json["block"]["permissions"], 'allow_edit' | 142 | assert_includes json["block"]["permissions"], 'allow_edit' |
143 | end | 143 | end |
144 | + | ||
145 | + should 'get a block with api content params' do | ||
146 | + class MyTestBlock < Block | ||
147 | + def api_content | ||
148 | + api_content_params | ||
149 | + end | ||
150 | + end | ||
151 | + box = fast_create(Box, :owner_id => environment.id, :owner_type => Environment.name) | ||
152 | + block = fast_create(MyTestBlock, box_id: box.id) | ||
153 | + params["custom_param"] = "custom_value" | ||
154 | + get "/api/v1/blocks/#{block.id}?#{params.to_query}" | ||
155 | + json = JSON.parse(last_response.body) | ||
156 | + assert_equal "custom_value", json["block"]["api_content"]["custom_param"] | ||
157 | + end | ||
144 | end | 158 | end |