Module: Gretel::ViewHelpers
- Defined in:
- lib/gretel/view_helpers.rb
Instance Method Summary collapse
-
#breadcrumb(key = nil, *args) ⇒ Object
Sets the current breadcrumb to be rendered elsewhere.
-
#breadcrumbs(options = {}, &block) ⇒ Object
Renders the breadcrumbs HTML, for example in your layout.
-
#parent_breadcrumb(options = {}, &block) ⇒ Object
Returns or yields parent breadcrumb (second-to-last in the trail) if it is present.
-
#with_breadcrumb(key, *args, &block) ⇒ Object
Yields a block where inside the block you have a different breadcrumb than outside.
Instance Method Details
#breadcrumb(key = nil, *args) ⇒ Object
Sets the current breadcrumb to be rendered elsewhere. Put it somewhere in the view, preferably in the top, before you render any breadcrumbs HTML:
<% breadcrumb :category, @category %>
If you pass an instance of an object that responds to model_name (like an ActiveRecord model instance), the breadcrumb can be automatically inferred, so a shortcut for the above would be:
<% breadcrumb @category %>
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/gretel/view_helpers.rb', line 10 def (key = nil, *args) if key.nil? || key.is_a?(Hash) raise ArgumentError, "The `breadcrumb` method was called with #{key.inspect} as the key. This method is used to set the breadcrumb. Maybe you meant to call the `breadcrumbs` method (with an 's' in the end) which is used to render the breadcrumbs?" elsif key.class.respond_to?(:model_name) # Enables calling `breadcrumb @product` instead of `breadcrumb :product, @product` args.unshift key key = key.class.model_name.to_s.underscore.to_sym end @_gretel_renderer = Gretel::Renderer.new(self, key, *args) end |
#breadcrumbs(options = {}, &block) ⇒ Object
Renders the breadcrumbs HTML, for example in your layout. See the readme for default options.
<%= breadcrumbs pretext: "You are here: " %>
If you supply a block, it will yield an array with the breadcrumb links so you can build the breadcrumbs HTML manually:
<% breadcrumbs do |links| %>
<% if links.any? %>
You are here:
<% links.each do |link| %>
<%= link_to link.text, link.url %> (<%= link.key %>)
<% end %>
<% end %>
<% end %>
51 52 53 54 55 56 57 |
# File 'lib/gretel/view_helpers.rb', line 51 def ( = {}, &block) if block_given? gretel_renderer.yield_links(, &block) else gretel_renderer.render() end end |
#parent_breadcrumb(options = {}, &block) ⇒ Object
Returns or yields parent breadcrumb (second-to-last in the trail) if it is present.
<% parent_breadcrumb do |link| %>
<%= link_to link.text, link.url %> (<%= link.key %>)
<% end %>
64 65 66 67 68 69 70 |
# File 'lib/gretel/view_helpers.rb', line 64 def ( = {}, &block) if block_given? gretel_renderer.(, &block) else gretel_renderer.() end end |
#with_breadcrumb(key, *args, &block) ⇒ Object
Yields a block where inside the block you have a different breadcrumb than outside.
<% breadcrumb :about %>
<%= breadcrumbs # shows the :about breadcrumb %>
<% with_breadcrumb :product, Product.first do %>
<%= # shows the :product breadcrumb %>
<% end %>
<%= breadcrumbs # shows the :about breadcrumb %>
32 33 34 35 36 37 |
# File 'lib/gretel/view_helpers.rb', line 32 def (key, *args, &block) original_renderer = @_gretel_renderer @_gretel_renderer = Gretel::Renderer.new(self, key, *args) yield @_gretel_renderer = original_renderer end |