Class: Breadcrumbs
- Inherits:
-
Object
- Object
- Breadcrumbs
- Defined in:
- lib/breadcrumbs.rb,
lib/breadcrumbs/render.rb,
lib/breadcrumbs/version.rb,
lib/breadcrumbs/render/base.rb,
lib/breadcrumbs/render/list.rb,
lib/breadcrumbs/render/inline.rb,
lib/breadcrumbs/render/ordered_list.rb,
lib/breadcrumbs/action_controller_ext.rb
Defined Under Namespace
Modules: ActionController, Render, Version
Instance Attribute Summary collapse
-
#items ⇒ Object
Returns the value of attribute items.
Instance Method Summary collapse
-
#add(text, url = nil, options = {}) ⇒ Object
(also: #<<)
Add a new breadcrumbs.
-
#initialize ⇒ Breadcrumbs
constructor
:nodoc:.
-
#render(options = {}) ⇒ Object
Render breadcrumbs using the specified format.
-
#translate(scope) ⇒ Object
:nodoc:.
Constructor Details
#initialize ⇒ Breadcrumbs
:nodoc:
13 14 15 |
# File 'lib/breadcrumbs.rb', line 13 def initialize # :nodoc: self.items = [] end |
Instance Attribute Details
#items ⇒ Object
Returns the value of attribute items.
11 12 13 |
# File 'lib/breadcrumbs.rb', line 11 def items @items end |
Instance Method Details
#add(text, url = nil, options = {}) ⇒ Object Also known as: <<
Add a new breadcrumbs.
.add 'Home'
.add 'Home', '/'
.add 'Home', '/', class: 'home'
If you provide a symbol as text, it will try to find it as I18n scope.
26 27 28 29 30 |
# File 'lib/breadcrumbs.rb', line 26 def add(text, url = nil, = {}) = {i18n: true}.merge() text = translate(text) if .delete(:i18n) items << [text.to_s, url, ] end |
#render(options = {}) ⇒ Object
Render breadcrumbs using the specified format. Use HTML lists by default, but can be plain links.
.render
.render(format: "inline")
.render(format: "inline", separator: "|")
.render(format: "list")
.render(format: "ordered_list")
.render(id: 'breadcrumbs')
.render(class: 'breadcrumbs')
You can also define your own formatter. Just create a class that implements a render
instance method and you’re good to go.
class Breadcrumbs::Render::Dl
def render
# return breadcrumbs wrapped in a <DL> tag
end
end
To use your new format, just provide the :format
option.
.render(format: 'dl')
59 60 61 62 63 64 65 |
# File 'lib/breadcrumbs.rb', line 59 def render( = {}) [:format] ||= :list klass_name = .delete(:format).to_s.classify klass = Breadcrumbs::Render.const_get(klass_name) klass.new(self, ).render end |
#translate(scope) ⇒ Object
:nodoc:
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/breadcrumbs.rb', line 67 def translate(scope) # :nodoc: return scope if scope.match?(/\A[\s.]+\z/) text = begin I18n.t(scope, scope: "breadcrumbs", raise: true) rescue StandardError nil end text ||= begin I18n.t(scope, default: scope.to_s) rescue StandardError scope end text end |