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:
9 10 11 |
# File 'lib/breadcrumbs.rb', line 9 def initialize # :nodoc: self.items = [] end |
Instance Attribute Details
#items ⇒ Object
Returns the value of attribute items.
7 8 9 |
# File 'lib/breadcrumbs.rb', line 7 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.
22 23 24 25 26 |
# File 'lib/breadcrumbs.rb', line 22 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')
54 55 56 57 58 59 60 61 62 |
# File 'lib/breadcrumbs.rb', line 54 def render( = {}) [:format] ||= :list klass_name = .delete(:format).to_s.classify klass = Breadcrumbs::Render.const_get(klass_name) html = klass.new(self, ).render html.respond_to?(:html_safe) ? html.html_safe : html end |
#translate(scope) ⇒ Object
:nodoc:
64 65 66 67 68 |
# File 'lib/breadcrumbs.rb', line 64 def translate(scope) # :nodoc: text = I18n.t(scope, scope: 'breadcrumbs', raise: true) rescue nil text ||= I18n.t(scope, default: scope.to_s) rescue scope text end |