Class: Breadcrumby::Viewer

Inherits:
Object
  • Object
show all
Defined in:
lib/breadcrumby/models/viewer.rb

Instance Method Summary collapse

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, options = {}, view = ActionController::Base.helpers)
  @object  = object
  @options = options
  @view    = view
end

Instance Method Details



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/breadcrumby/models/viewer.rb', line 11

def breadcrumb
  return '' unless @object.respond_to?(:breadcrumby)

  list = breadcrumbs(current_object).map.with_index do |object, index|
    item link(object) + meta(index + 1)
  end

  list += object_extra(list.size)

  @view. :ol, list.join('').html_safe, list_options
end


23
24
25
26
27
28
# File 'lib/breadcrumby/models/viewer.rb', line 23

def breadcrumbs(object)
  items = object.breadcrumby
  items << Breadcrumby::Home.new(@view)

  items.reverse
end

#current_objectObject



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.breadcrumby_options[:method_name]) || '--')
end

#item(content, options = item_options) ⇒ Object



56
57
58
# File 'lib/breadcrumby/models/viewer.rb', line 56

def item(content, options = item_options)
  @view. :li, content, options
end

#item_optionsObject



60
61
62
63
64
65
66
# File 'lib/breadcrumby/models/viewer.rb', line 60

def item_options
  {
    itemprop:  :itemListElement,
    itemscope: true,
    itemtype:  'http://schema.org/ListItem'
  }
end


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),
    link_options(object, action)
  )
end


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


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/breadcrumby/models/viewer.rb', line 83

def link_options(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


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. :span, name, link_tag_name_options
end


103
104
105
# File 'lib/breadcrumby/models/viewer.rb', line 103

def link_tag_name_options
  { itemprop: :name }
end

#list_optionsObject



107
108
109
110
111
112
113
# File 'lib/breadcrumby/models/viewer.rb', line 107

def list_options
  {
    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 meta(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 = @options[:action]
  result = []

  return result if action.blank?

  if @object != current_object
    index += 1

    result << item(link(@object, action: :index) + meta(index))
  end

  result << item(link(current_object, action: action) + meta(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.breadcrumby_options[:i18n_key] if include_model

  result
end