Skip to content

Commit

Permalink
Replace data-vocabulary with schema.org
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceda committed Jan 2, 2017
1 parent 8038a7c commit 7d5fa8b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ before_script:
- "cd test/dummy; rake db:migrate; rake db:test:prepare; cd ../.."
notifications:
email: false

before_deploy:
- gem install mime-types -v 2.6.2

matrix:
exclude:
- rvm: 1.9.3
gemfile: gemfiles/Gemfile-rails.4.0.x
- rvm: 1.9.3
gemfile: gemfiles/Gemfile-rails.4.1.x
30 changes: 18 additions & 12 deletions lib/gretel/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ def render

# Loop through all but the last (current) link and build HTML of the fragments
fragments = links[0..-2].map do |link|
render_fragment(options[:fragment_tag], link.text, link.url, options[:semantic])
render_fragment(options[:fragment_tag], link.text, link.url, links.index(link) + 1, options[:semantic])
end

# The current link is handled a little differently, and is only linked if specified in the options
current_link = links.last
fragments << render_fragment(options[:fragment_tag], current_link.text, (options[:link_current] ? current_link.url : nil), options[:semantic], class: options[:current_class], current_link: current_link.url)
fragments << render_fragment(options[:fragment_tag], current_link.text, (options[:link_current] ? current_link.url : nil), links.index(current_link) + 1, options[:semantic], class: options[:current_class], current_link: current_link.url)

# Build the final HTML
html_fragments = []
Expand All @@ -192,37 +192,43 @@ def render
end

html = html_fragments.join(" ").html_safe
content_tag(options[:container_tag], html, id: options[:id], class: options[:class])
if options[:semantic]
content_tag(options[:container_tag], html, id: options[:id], class: options[:class], itemscope: "", itemtype: "http://schema.org/BreadcrumbList")
else
content_tag(options[:container_tag], html, id: options[:id], class: options[:class])
end
end

alias :to_s :render

# Renders HTML for a breadcrumb fragment, i.e. a breadcrumb link.
def render_fragment(fragment_tag, text, url, semantic, options = {})
def render_fragment(fragment_tag, text, url, position, semantic, options = {})
if semantic
render_semantic_fragment(fragment_tag, text, url, options)
render_semantic_fragment(fragment_tag, text, url, position, options)
else
render_nonsemantic_fragment(fragment_tag, text, url, options)
end
end

# Renders semantic fragment HTML.
def render_semantic_fragment(fragment_tag, text, url, options = {})
def render_semantic_fragment(fragment_tag, text, url, position, options = {})
tag_position = tag(:meta, itemprop: "position", content: position)

if fragment_tag
text = content_tag(:span, text, itemprop: "title")
text = content_tag(:span, text, itemprop: "name")

if url.present?
text = breadcrumb_link_to(text, url, itemprop: "url")
text = breadcrumb_link_to(text, url, itemprop: "item")
elsif options[:current_link].present?
current_url = "#{root_url}#{options[:current_link].gsub(/^\//, '')}"
text = text + tag(:meta, itemprop: "url", content: current_url)
text = text + tag(:meta, itemprop: "item", content: current_url)
end

content_tag(fragment_tag, text, class: options[:class], itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb")
content_tag(fragment_tag, text, class: options[:class], itemscope: "", itemtype: "http://schema.org/ListItem")
elsif url.present?
content_tag(:span, breadcrumb_link_to(content_tag(:span, text, itemprop: "title"), url, class: options[:class], itemprop: "url"), itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb")
content_tag(:span, breadcrumb_link_to(content_tag(:span, text, itemprop: "name"), url, class: options[:class], itemprop: "item") + tag_position, itemscope: "", itemtype: "http://schema.org/ListItem", itemprop: "itemListElement")
else
content_tag(:span, content_tag(:span, text, class: options[:class], itemprop: "title"), itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb")
content_tag(:span, content_tag(:span, text, class: options[:class], itemprop: "name") + tag_position, itemscope: "", itemtype: "http://schema.org/ListItem", itemprop: "itemListElement")
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/helper_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def itemscope_value

test "semantic breadcrumb" do
breadcrumb :with_root
assert_dom_equal %Q{<div class="breadcrumbs"><span itemscope="#{itemscope_value}" itemtype="http://data-vocabulary.org/Breadcrumb"><a href="/" itemprop="url"><span itemprop="title">Home</span></a></span> &rsaquo; <span itemscope="#{itemscope_value}" itemtype="http://data-vocabulary.org/Breadcrumb"><span class="current" itemprop="title">About</span></span></div>},
assert_dom_equal %Q{<div class="breadcrumbs" itemscope="#{itemscope_value}" itemtype="http://schema.org/BreadcrumbList"><span itemscope="#{itemscope_value}" itemtype="http://schema.org/ListItem" itemprop="itemListElement"><a itemprop="item" href="/"><span itemprop="name">Home</span></a><meta itemprop="position" content="1" /></span> &rsaquo; <span itemscope="#{itemscope_value}" itemtype="http://schema.org/ListItem" itemprop="itemListElement"><span class="current" itemprop="name">About</span><meta itemprop="position" content="2" /></span></div>},
breadcrumbs(semantic: true).to_s
end

Expand Down Expand Up @@ -372,7 +372,7 @@ def itemscope_value

test "custom semantic container and fragment tags" do
breadcrumb :basic
assert_dom_equal %Q{<c class="breadcrumbs"><f itemscope="#{itemscope_value}" itemtype="http://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="/"><span itemprop="title">Home</span></a></f> &rsaquo; <f class="current" itemscope="#{itemscope_value}" itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">About</span><meta itemprop="url" content="http://test.host/about" /></f></c>},
assert_dom_equal %Q{<c class="breadcrumbs" itemscope="#{itemscope_value}" itemtype="http://schema.org/BreadcrumbList"><f itemscope="#{itemscope_value}" itemtype="http://schema.org/ListItem"><a itemprop="item" href="/"><span itemprop="name">Home</span></a></f> &rsaquo; <f class="current" itemscope="#{itemscope_value}" itemtype="http://schema.org/ListItem"><span itemprop="name">About</span><meta itemprop="item" content="http://test.host/about" /></f></c>},
breadcrumbs(container_tag: :c, fragment_tag: :f, semantic: true).to_s
end

Expand Down

0 comments on commit 7d5fa8b

Please sign in to comment.