Skip to content

Instantly share code, notes, and snippets.

@iberianpig
Last active April 13, 2017 02:51
Show Gist options
  • Save iberianpig/cd12d66759b23dd8d4ac53b05ba7decc to your computer and use it in GitHub Desktop.
Save iberianpig/cd12d66759b23dd8d4ac53b05ba7decc to your computer and use it in GitHub Desktop.
パンくずリストを構造化付きデータごとレンダリングさせるカスタムビルダ breadcrumbs_on_railsがベース(https://github.com/weppos/breadcrumbs_on_rails)
module BreadcrumbHelper
def render_breadcrumbs
super(builder: CustomBreadcrumbsBuilder)
end
end
class CustomBreadcrumbsBuilder < BreadcrumbsOnRails::Breadcrumbs::SimpleBuilder
def initialize(context, elements, options = {})
super
@options[:separator] = ' > '
end
def render
if @elements.present?
@context.content_tag(:ol, class: 'breadcrumb', itemscope: true, itemtype: "http://schema.org/BreadcrumbList") do
@elements.map.with_index do |element, index|
separator = @options[:separator]
separator = "" if @elements.size == index + 1
render_element(element, index) + separator
end.join.html_safe
end
else
''
end
end
def render_element(element, index)
# truncate page title
# if element.name !~ /^</ && element.name.to_s.length > 20
# trunc = element.name[0..19].to_s
# element.name = trunc + '...'
# end
# with microdata
# NOTE: https://productforums.google.com/d/msg/webmaster-ja/61INP019tKA/1hm-Tg4XhVkJ
content = @context.content_tag(:span, compute_name(element), itemprop: "name")
unless element.path.nil?
content = @context.link_to_unless_current(
content,
compute_path(element), itemprop: "item")
end
position = index + 1
content << @context.content_tag(:meta, nil, itemprop: "position", content: position)
@context.content_tag(:li, content.html_safe, itemprop: "itemListElement",
itemscope: true, itemtype: "http://schema.org/ListItem")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment