Module: Skyline::Rendering::Helpers::BreadCrumbHelper

Included in:
Renderer::Helpers
Defined in:
lib/skyline/rendering/helpers/bread_crumb_helper.rb

Instance Method Summary collapse

Instance Method Details

Helper to easily create breadcrumbs that are cut off in the middle.

Parameters:

  • bc (Array<Array>)

    An array of arrays with two elements: [[title,url],…]

  • options (Hash) (defaults to: {})

    Options

Options Hash (options):

  • :max_length (Integer)

    () Limit the max length in chars. Returns an array with nil element where something has been cut away.

Returns:

  • (Array<Array>)

    Array of arrays with the first element the title and as second element the url.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/skyline/rendering/helpers/bread_crumb_helper.rb', line 12

def bread_crumb(bc,options={})
  if bc.kind_of? Skyline::ArticleVersion
    page = bc
    bc = bc.page.nesting.map{|p| [p.published_publication_data.andand.navigation_title,p.url]}
    bc[-1][0] = page.data.navigation_title
  end
  
  if options[:max_length] && bc.size > 1
    # always display the last page
    b = [bc.pop]
    length = b[-1][0].size
          
    # then try to 'add' the first page if it fits
    first = nil
    if length + bc[0][0].to_s.size <= options[:max_length]
      first = bc.shift
      length += first[0].to_s.size
    end
    
    # try to add pages in between starting from the 2nd to last page and going back as long as it fits
    bc.reverse.each do |p|
      if length + p[0].to_s.size <= options[:max_length]
        b.unshift(p)
        length += p[0].to_s.size
      else
        b.unshift(nil)
        break
      end
    end
    
    b.unshift(first) if first
    b
  else
    bc
  end    
end