Class: Breadcrumby::Viewer
- Inherits:
-
Object
- Object
- Breadcrumby::Viewer
- Defined in:
- lib/breadcrumby/models/viewer.rb
Instance Method Summary collapse
- #breadcrumb ⇒ Object
- #breadcrumbs(object) ⇒ Object
- #current_object ⇒ Object
- #i18n_action_name(object, action) ⇒ Object
- #i18n_name(object) ⇒ Object
-
#initialize(object, options = {}, view = ActionController::Base.helpers) ⇒ Viewer
constructor
A new instance of Viewer.
- #item(content, options = item_options) ⇒ Object
- #item_options ⇒ Object
- #link(object, action: nil) ⇒ Object
- #link_action(object, action) ⇒ Object
- #link_options(object, action) ⇒ Object
- #link_tag_name(object, action) ⇒ Object
- #link_tag_name_options ⇒ Object
- #list_options ⇒ Object
- #meta(index) ⇒ Object
- #object_extra(index) ⇒ Object
- #scope(object, include_model: true) ⇒ Object
Constructor Details
#initialize(object, options = {}, view = ActionController::Base.helpers) ⇒ Viewer
Returns a new instance of Viewer.
5 6 7 8 9 |
# File 'lib/breadcrumby/models/viewer.rb', line 5 def initialize(object, = {}, view = ActionController::Base.helpers) @object = object = @view = view end |
Instance Method Details
#breadcrumb ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/breadcrumby/models/viewer.rb', line 11 def return '' unless @object.respond_to?(:breadcrumby) list = (current_object).map.with_index do |object, index| item link(object) + (index + 1) end list += object_extra(list.size) @view.content_tag :ol, list.join('').html_safe, end |
#breadcrumbs(object) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/breadcrumby/models/viewer.rb', line 23 def (object) items = object. items << Breadcrumby::Home.new(@view) items.reverse end |
#current_object ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/breadcrumby/models/viewer.rb', line 30 def current_object @current_object ||= begin return @object if object_action.blank? if object_action.respond_to?(:call) object_action.call @view else object_action end end end |
#i18n_action_name(object, action) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/breadcrumby/models/viewer.rb', line 42 def i18n_action_name(object, action) label = "actions.#{action}.name" I18n.t(label, scope: scope(object), default: I18n.t(label, scope: scope(object, include_model: false), default: I18n.t(action))) end |
#i18n_name(object) ⇒ Object
50 51 52 53 54 |
# File 'lib/breadcrumby/models/viewer.rb', line 50 def i18n_name(object) I18n.t(:name, scope: scope(object), default: object.send(object.[:method_name]) || '--') end |
#item(content, options = item_options) ⇒ Object
56 57 58 |
# File 'lib/breadcrumby/models/viewer.rb', line 56 def item(content, = ) @view.content_tag :li, content, end |
#item_options ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/breadcrumby/models/viewer.rb', line 60 def { itemprop: :itemListElement, itemscope: true, itemtype: 'http://schema.org/ListItem' } end |
#link(object, action: nil) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/breadcrumby/models/viewer.rb', line 68 def link(object, action: nil) @view.link_to( link_tag_name(object, action), link_action(object, action), (object, action) ) end |
#link_action(object, action) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/breadcrumby/models/viewer.rb', line 76 def link_action(object, action) return object.index_path if action == :index # TODO: get current path to behaves like a refresh on the same page. @view.request.url? action ? 'javascript:void(0);' : object.show_path end |
#link_options(object, action) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/breadcrumby/models/viewer.rb', line 83 def (object, action) name = i18n_name(object) title_path = action ? "actions.#{action}.title" : :title title = I18n.t(title_path, scope: scope(object), name: name, default: I18n.t(title_path, scope: scope(object, include_model: false))) { itemprop: :item, itemscope: true, itemtype: 'http://schema.org/Thing', title: title } end |
#link_tag_name(object, action) ⇒ Object
97 98 99 100 101 |
# File 'lib/breadcrumby/models/viewer.rb', line 97 def link_tag_name(object, action) name = action ? i18n_action_name(object, action) : i18n_name(object) @view.content_tag :span, name, end |
#link_tag_name_options ⇒ Object
103 104 105 |
# File 'lib/breadcrumby/models/viewer.rb', line 103 def { itemprop: :name } end |
#list_options ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/breadcrumby/models/viewer.rb', line 107 def { class: :breadcrumby, itemscope: true, itemtype: 'http://schema.org/BreadcrumbList' } end |
#meta(index) ⇒ Object
115 116 117 |
# File 'lib/breadcrumby/models/viewer.rb', line 115 def (index) @view.tag :meta, content: index, itemprop: :position end |
#object_extra(index) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/breadcrumby/models/viewer.rb', line 119 def object_extra(index) action = [:action] result = [] return result if action.blank? if @object != current_object index += 1 result << item(link(@object, action: :index) + (index)) end result << item(link(current_object, action: action) + (index + 1)) result end |
#scope(object, include_model: true) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/breadcrumby/models/viewer.rb', line 136 def scope(object, include_model: true) result = [:breadcrumby] result << object.[:i18n_key] if include_model result end |