Skip to content

Commit

Permalink
(code-updates) Changes from lassebunk#73 & lassebunk#78 pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrShemek committed Dec 8, 2016
1 parent 8038a7c commit 7d4e599
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Option | Description
:semantic | Whether it should generate [semantic breadcrumbs](http://support.google.com/webmasters/bin/answer.py?hl=en&answer=185417). | False
:id | ID for the breadcrumbs container. | None
:class | CSS class for the breadcrumbs container. Can be set to `nil` for no class. | `"breadcrumbs"`
:fragment_class | CSS class for the fragment link or span. Can be set to `nil` for no class. | None
:current_class | CSS class for the current link or span. Can be set to `nil` for no class. | `"current"`
:pretext_class | CSS class for the pretext, if given. Can be set to `nil` for no class. | `"pretext"`
:posttext_class | CSS class for the posttext, if given. Can be set to `nil` for no class. | `"posttext"`
Expand All @@ -107,6 +108,7 @@ Style | Description
`:ol` | Renders the links in `<li>` elements contained in an outer `<ol>`.
`:ul` | Renders the links in `<li>` elements contained in an outer `<ul>`.
`:bootstrap` | Renders the links for use in [Twitter Bootstrap](http://getbootstrap.com/).
`:bootstrap4` | Renders the links for use in [Bootstrap v4](http://v4-alpha.getbootstrap.com/).
`:foundation5` | Renders the links for use in [Foundation 5](http://foundation.zurb.com/).

Or you can build the breadcrumbs manually for full customization; see below.
Expand Down
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
23 changes: 15 additions & 8 deletions lib/gretel/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Renderer
ol: { container_tag: :ol, fragment_tag: :li },
ul: { container_tag: :ul, fragment_tag: :li },
bootstrap: { container_tag: :ol, fragment_tag: :li, class: "breadcrumb", current_class: "active" },
bootstrap4: { container_tag: :ol, fragment_tag: :li, class: "breadcrumb", fragment_class: "breadcrumb-item", current_class: "active" },
foundation5: { container_tag: :ul, fragment_tag: :li, class: "breadcrumbs", current_class: "current" }
}

Expand Down Expand Up @@ -171,12 +172,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, options[:semantic], fragment_class: options[:fragment_class])
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), options[:semantic], fragment_class: options[:fragment_class], class: options[:current_class], current_link: current_link.url)

# Build the final HTML
html_fragments = []
Expand Down Expand Up @@ -208,6 +209,9 @@ def render_fragment(fragment_tag, text, url, semantic, options = {})

# Renders semantic fragment HTML.
def render_semantic_fragment(fragment_tag, text, url, options = {})
fragment_class = [options[:fragment_class], options[:class]].join(' ').strip
fragment_class = nil if fragment_class.blank?

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

Expand All @@ -218,23 +222,26 @@ def render_semantic_fragment(fragment_tag, text, url, options = {})
text = text + tag(:meta, itemprop: "url", 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: fragment_class, itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb")
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: "title"), url, class: fragment_class, itemprop: "url"), itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb")
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: fragment_class, itemprop: "title"), itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb")
end
end

# Renders regular, non-semantic fragment HTML.
def render_nonsemantic_fragment(fragment_tag, text, url, options = {})
fragment_class = [options[:fragment_class], options[:class]].join(' ').strip
fragment_class = nil if fragment_class.blank?

if fragment_tag
text = breadcrumb_link_to(text, url) if url.present?
content_tag(fragment_tag, text, class: options[:class])
content_tag(fragment_tag, text, class: fragment_class)
elsif url.present?
breadcrumb_link_to(text, url, class: options[:class])
breadcrumb_link_to(text, url, class: fragment_class)
elsif options[:class].present?
content_tag(:span, text, class: options[:class])
content_tag(:span, text, class: fragment_class)
else
text
end
Expand Down
12 changes: 12 additions & 0 deletions test/helper_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ def itemscope_value
breadcrumbs(class: "custom_class").to_s
end

test "custom fragment class" do
breadcrumb :basic
assert_dom_equal %{<div class="breadcrumbs"><a class="custom_fragment_class" href="/">Home</a> &rsaquo; <span class="custom_fragment_class current">About</span></div>},
breadcrumbs(fragment_class: "custom_fragment_class").to_s
end

test "custom current class" do
breadcrumb :basic
assert_dom_equal %{<div class="breadcrumbs"><a href="/">Home</a> &rsaquo; <span class="custom_current_class">About</span></div>},
Expand Down Expand Up @@ -358,6 +364,12 @@ def itemscope_value
breadcrumbs(style: :bootstrap).to_s
end

test "bootstrap4 style" do
breadcrumb :basic
assert_dom_equal %{<ol class="breadcrumb"><li class="breadcrumb-item"><a href="/">Home</a></li><li class="breadcrumb-item active">About</li></ol>},
breadcrumbs(style: :bootstrap4).to_s
end

test "foundation5 style" do
breadcrumb :basic
assert_dom_equal %{<ul class="breadcrumbs"><li><a href="/">Home</a></li><li class="current">About</li></ul>},
Expand Down

0 comments on commit 7d4e599

Please sign in to comment.