Module: Workbench::NavigationHelper

Defined in:
app/helpers/workbench/navigation_helper.rb

Overview

Methods for 1) generating paths; or 2) generating links For helper methods that return individual instances based on some parameters see corresponding helpers.

Constant Summary collapse

NO_NEW_FORMS =
%w{Confidence Attribution ObservationMatrixRowItem ObservationMatrixColumnItem ObservationMatrixRow ObservationMatrixColumn Note Tag
Citation Identifier DataAttribute AlternateValue TaxonNameClassification
GeographicArea ContainerItem ProtocolRelationship Download}.freeze
NOT_DATA_PATHS =
%w{/project /administration /user}.freeze

Instance Method Summary collapse

Instance Method Details



231
232
233
234
235
236
237
238
# File 'app/helpers/workbench/navigation_helper.rb', line 231

def a_to_z_links(targets = [])
  letters = targets.empty? ? ('A'..'Z') : ('A'..'Z').to_a & targets
  (:div, class: 'navigation-bar-left', id: 'alphabet_nav') do
    (:ul, class: 'left_justified_navbar context-menu') do
      letters.collect{|l| (:li, link_to("#{l}", "\##{l}")) }.join.html_safe
    end
  end
end


191
192
193
194
195
196
197
# File 'app/helpers/workbench/navigation_helper.rb', line 191

def batch_load_link
  if self.controller.respond_to?(:batch_load)
    link_to('Batch load', {action: :batch_load, controller: self.controller_name}, 'data-icon' => 'batch')
  else
    (:span, 'Batch load', class: 'disabled', 'data-icon' => 'batch')
  end
end


183
184
185
186
187
188
189
# File 'app/helpers/workbench/navigation_helper.rb', line 183

def destroy_object_link(object)
  if object.is_destroyable?(sessions_current_user)
    link_to((:span, 'Destroy', 'data-icon' => 'trash', class: 'small-icon'), object.metamorphosize, method: :delete, data: {confirm: 'Are you sure?'}, class: 'navigation-item')
  else
    (:div, (:span, 'Destroy', 'data-icon' => 'trash', class: 'small-icon'), class: 'navigation-item disable')
  end
end


114
115
116
117
118
119
120
# File 'app/helpers/workbench/navigation_helper.rb', line 114

def download_for_model_link(model)
  if self.controller.respond_to?(:download)
    link_to('Download', download_path_for_model(model), data: { icon: :download, turbolinks: false })
  else
    (:em, 'Download not yet available.')
  end
end

#download_path_for_model(model) ⇒ Object



122
123
124
125
126
127
128
129
# File 'app/helpers/workbench/navigation_helper.rb', line 122

def download_path_for_model(model)
  if model.name == 'Documentation'
    # some weirdness with ninflections
    download_documentation_index_path
  else
    send("download_#{model.name.tableize}_path")
  end
end

return [A tag, nil]

a link, or disabled link


175
176
177
178
179
180
181
# File 'app/helpers/workbench/navigation_helper.rb', line 175

def edit_object_link(object)
  if has_route_for_edit?(object) && object.is_editable?(sessions_current_user)
    link_to((:span, 'Edit', 'data-icon' => 'edit', class: 'small-icon'), edit_object_path(metamorphosize_if(object)), class: 'navigation-item')
  else
    (:div, (:span, 'Edit', 'data-icon' => 'edit', class: 'small-icon'), class: 'navigation-item disable')
  end
end

#edit_object_path(object) ⇒ Object



146
147
148
# File 'app/helpers/workbench/navigation_helper.rb', line 146

def edit_object_path(object)
  send(edit_object_path_string(object), object)
end

#edit_object_path_string(object) ⇒ Object

Determine wether an edit or destroy route exists Custom routes can be added per Class by adding a corresponding ‘_string` postfixed method.

Returns:

  • String



154
155
156
157
158
159
160
161
162
# File 'app/helpers/workbench/navigation_helper.rb', line 154

def edit_object_path_string(object)
  default  = "edit_#{metamorphosize_if(object).class.base_class.name.underscore}_path"
  specific = default + '_string'
  if self.respond_to?(specific)
    self.send(specific, object)
  else
    default
  end
end


56
57
58
# File 'app/helpers/workbench/navigation_helper.rb', line 56

def forward_back_links(instance)
  (:span, (previous_link(instance) + ' | ' + next_link(instance)).html_safe)
end

#has_route_for_edit?(object) ⇒ Boolean

return [Boolean]

true if there is a route to edit for the object (some objects are not editable, like Tags)

Returns:

  • (Boolean)


166
167
168
# File 'app/helpers/workbench/navigation_helper.rb', line 166

def has_route_for_edit?(object)
  self.respond_to?(edit_object_path_string(object))
end


106
107
108
109
110
111
112
# File 'app/helpers/workbench/navigation_helper.rb', line 106

def list_for_model_link(model)
  if model.any?
    link_to('List', list_path_for_model(model), 'data-icon' => 'list')
  else
    (:span, 'List', class: :disabled, 'data-icon' => 'list')
  end
end

#list_path_for_model(model) ⇒ Object



92
93
94
# File 'app/helpers/workbench/navigation_helper.rb', line 92

def list_path_for_model(model)
  url_for(controller: model.name.tableize.pluralize.downcase, action: :list)
end


96
97
98
99
100
101
102
103
104
# File 'app/helpers/workbench/navigation_helper.rb', line 96

def new_for_model_link(model)
  if NO_NEW_FORMS.include?(model.name)
    nil
  elsif model.name == 'ProjectSource'
    link_to('New', new_source_path, 'class' => 'small-icon', 'data-icon' => 'new')
  else
    link_to((:span, 'New', 'class' => 'small-icon', data: { icon: :new }), new_path_for_model(model), 'class' => 'navigation-item')
  end
end

#new_path_for_model(model) ⇒ Object



88
89
90
# File 'app/helpers/workbench/navigation_helper.rb', line 88

def new_path_for_model(model)
  send("new_#{model.name.tableize.singularize}_path")
end

Used in show/REST A next record link.



76
77
78
79
80
81
82
83
84
85
86
# File 'app/helpers/workbench/navigation_helper.rb', line 76

def next_link(instance, text: 'Next', target: nil)
  link_text = (:span, text, 'class' => 'small-icon icon-right', 'data-icon' => 'arrow-right')
  link_object = instance.next
  return (:div, link_text, 'class' => 'navigation-item disable') if link_object.nil?
  if target.nil?
    target ||= link_object.metamorphosize
  else
    target = send(target, id: link_object.id)
  end
  link_to(link_text, target, 'data-arrow' => 'next', 'class' => 'navigation-item')
end

If a “home” is provided, use it instead of show link See also over ride in object_link - which we might need to merge



251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'app/helpers/workbench/navigation_helper.rb', line 251

def object_home_link(object)
  k = object.class
  klass = OBJECT_RADIALS[k.name] ? k.name : k.base_class.name

  p = OBJECT_RADIALS[klass]
  if p && p['home']
    link_to(
      object_tag(object),
      send("#{p['home']}_path", "#{klass.tableize.singularize}_id" => object.id)
    )
  else
    object_link(object)
  end
end


131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/helpers/workbench/navigation_helper.rb', line 131

def object_link(object)
  return nil if object.nil?
  klass_name = object.class.base_class.name.underscore
  link_method = klass_name + '_link'

  # If a customized link to method is available use that, otherwise use a generic
  if self.respond_to?(link_method)
    send(link_method, object)
  else
    t = object_tag(object)
    return "Unable to link to data #{object.class.name} id:#{object.id}." if t.blank?
    link_to(t.html_safe, metamorphosize_if(object))
  end
end

#on_workbench?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/helpers/workbench/navigation_helper.rb', line 38

def on_workbench?
  !(request.path =~ /#{NOT_DATA_PATHS.join('|')}/)
end

Used in show/REST A previous record link.



62
63
64
65
66
67
68
69
70
71
72
# File 'app/helpers/workbench/navigation_helper.rb', line 62

def previous_link(instance, text: 'Previous', target: nil)
  link_text = (:span, text,  'data-icon' => 'arrow-left', 'class' => 'small-icon')
  link_object = instance.previous
  return (:div, link_text, 'class' => 'navigation-item disable') if link_object.nil?
  if target.nil?
    target ||= link_object.metamorphosize
  else
    target = send(target, id: link_object.id)
  end
  link_to(link_text, target, 'data-arrow' => 'back', 'class' => 'navigation-item')
end

#quick_barObject



42
43
44
# File 'app/helpers/workbench/navigation_helper.rb', line 42

def quick_bar
  render(partial: '/workbench/navigation/quick_bar')  if sessions_signed_in?
end


46
47
48
49
50
# File 'app/helpers/workbench/navigation_helper.rb', line 46

def quick_bar_link(related_model)
  model = Hub::Data::BY_NAME[ related_model.kind_of?(Hash) ? related_model.keys.first : related_model ]
  return nil if model.nil?
  (:li, data_link(model))
end

#radial_navigation_tag(object) ⇒ Object



245
246
247
# File 'app/helpers/workbench/navigation_helper.rb', line 245

def radial_navigation_tag(object)
  (:span, '', data: { 'global-id' => object.to_global_id.to_s, 'radial-object' => 'true'})
end


210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/helpers/workbench/navigation_helper.rb', line 210

def recent_route_link(hsh)
  route = hsh.keys.first
  o = safe_object_from_attributes(hsh[route])
  if o.nil?
    link_to(route.parameterize(separator: ' - ').humanize.capitalize, route)
  elsif o
    o = o.metamorphosize if o.respond_to?(:metamorphosize)
    link_to(object_tag(o) + " [#{hsh[route]['object_type']}]", route)
  else
    (:em, 'Data no longer available.', class: :warning)
  end
end

Returns a link to the related data page.

Returns:

  • (<a> tag, nil)

    a link to the related data page



225
226
227
228
229
# File 'app/helpers/workbench/navigation_helper.rb', line 225

def related_data_link_tag(object)
  return nil if object.nil?
  p = "related_#{member_base_path(metamorphosize_if(object))}_path"
  (:li, link_to('Related data', send(p, object))) if controller.respond_to?(p)
end

#safe_object_from_attributes(hsh) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'app/helpers/workbench/navigation_helper.rb', line 199

def safe_object_from_attributes(hsh)
  if hsh['object_type'] && hsh['object_type']
    begin
      return hsh['object_type'].constantize.find(hsh['object_id'])
    rescue ActiveRecord::RecordNotFound
      return false
    end
  end
  nil
end

#slideout_clipboardObject



21
22
23
# File 'app/helpers/workbench/navigation_helper.rb', line 21

def slideout_clipboard
  render(partial: '/shared/data/slideout/clipboard')
end

#slideout_pdf_viewerObject



33
34
35
# File 'app/helpers/workbench/navigation_helper.rb', line 33

def slideout_pdf_viewer
  render(partial: '/shared/data/slideout/document')
end

#slideout_pinboardObject



25
26
27
# File 'app/helpers/workbench/navigation_helper.rb', line 25

def slideout_pinboard
  render(partial: '/shared/data/slideout/pinboard')
end

#slideout_recentObject



29
30
31
# File 'app/helpers/workbench/navigation_helper.rb', line 29

def slideout_recent
  render(partial: '/shared/data/slideout/recent')
end

#slideoutsObject

Slideout panels



13
14
15
16
17
18
19
# File 'app/helpers/workbench/navigation_helper.rb', line 13

def slideouts
  if sessions_current_project && sessions_signed_in? && on_workbench?
    [ slideout_pinboard,
      slideout_pdf_viewer,
      slideout_clipboard ].join.html_safe
  end
end

#task_barObject



52
53
54
# File 'app/helpers/workbench/navigation_helper.rb', line 52

def task_bar
  render(partial: '/workbench/navigation/task_bar') if is_task_controller?
end

#title_tagObject



240
241
242
243
# File 'app/helpers/workbench/navigation_helper.rb', line 240

def title_tag
  splash = request.path =~ /task/ ? request.path.demodulize.humanize : request.path.split('/')&.second&.humanize
  (:title, ['TaxonWorks', splash].compact.join(' - ') )
end