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 | get ':id' do |
7 | 7 | block = Block.find(params["id"]) |
8 | 8 | return forbidden! unless block.visible_to_user?(current_person) || block.allow_edit?(current_person) |
9 | + block.api_content_params = params.except("id") | |
9 | 10 | present block, :with => Entities::Block, display_api_content: true, current_person: current_person |
10 | 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 | 26 | false |
27 | 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 | 56 | end | ... | ... |
plugins/breadcrumbs/test/unit/content_breadcrumbs_block_test.rb
... | ... | @@ -5,9 +5,13 @@ class ContentBreadcrumbsBlockTest < ActiveSupport::TestCase |
5 | 5 | include NoosferoTestHelper |
6 | 6 | |
7 | 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 | 11 | end |
10 | 12 | |
13 | + attr_accessor :block, :profile | |
14 | + | |
11 | 15 | should 'has a description' do |
12 | 16 | assert_not_equal Block.description, BreadcrumbsPlugin::ContentBreadcrumbsBlock.description |
13 | 17 | end |
... | ... | @@ -16,11 +20,18 @@ class ContentBreadcrumbsBlockTest < ActiveSupport::TestCase |
16 | 20 | assert @block.help |
17 | 21 | end |
18 | 22 | |
19 | - | |
20 | 23 | should 'not be cacheable' do |
21 | 24 | refute @block.cacheable? |
22 | 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 | 35 | end |
25 | 36 | |
26 | 37 | require 'boxes_helper' | ... | ... |
test/api/blocks_test.rb
... | ... | @@ -141,4 +141,18 @@ class BlocksTest < ActiveSupport::TestCase |
141 | 141 | json = JSON.parse(last_response.body) |
142 | 142 | assert_includes json["block"]["permissions"], 'allow_edit' |
143 | 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 | 158 | end | ... | ... |