Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace data-vocabulary with schema.org #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions gemfiles/Gemfile-rails.4.0.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ gem "sqlite3"
# jquery-rails is used by the dummy application
gem "jquery-rails"

if RUBY_VERSION > '1.9.3'
gem 'mime-types'
else
gem 'mime-types', '2.99'
end

# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
Expand Down
6 changes: 6 additions & 0 deletions gemfiles/Gemfile-rails.4.1.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ gem "sqlite3"
# jquery-rails is used by the dummy application
gem "jquery-rails"

if RUBY_VERSION > '1.9.3'
gem 'mime-types'
else
gem 'mime-types', '2.99'
end

# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
Expand Down
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