Commit bdbf69e0e8ef1d4bf592181b54de7ed6ba8abf3e

Authored by Victor Costa
1 parent e8398c7d
Exists in staging

breadcrumbs: return links in api_content

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 &lt; 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 &lt; 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'
... ...