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 ProjectOrganization}.freeze
NOT_DATA_PATHS =
%w{/project /administration /user}.freeze

Instance Method Summary collapse

Instance Method Details



278
279
280
281
282
283
284
285
# File 'app/helpers/workbench/navigation_helper.rb', line 278

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') do
      letters.collect{|l| (:li, link_to("#{l}", "\##{l}"), data: { turbolinks: :false }) }.join.html_safe
    end
  end
end


241
242
243
244
245
246
247
248
249
250
251
252
# File 'app/helpers/workbench/navigation_helper.rb', line 241

def batch_load_link
  content = safe_join([
    (:span, '', class: 'icon', data: { icon: 'batch' }),
    'Batch load'],
  '')

  if self.controller.respond_to?(:batch_load)
    link_to(content, { action: :batch_load, controller: self.controller_name })
  else
    (:span, content, class: 'disabled')
  end
end

#class_navigation_json(klass) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/workbench/navigation_helper.rb', line 12

def class_navigation_json(klass)
  k = klass
  b = {}
  tasks = {}

  if OBJECT_RADIALS[k]
    %w{new edit home}.each do |t|
      if c = OBJECT_RADIALS[k][t]
        b[t] = send(c + '_path')
      end
    end

    if OBJECT_RADIALS[k]['tasks']
      tasks = OBJECT_RADIALS[k]['tasks'].inject({}) { |hsh, t| hsh[t] = send(t + '_path'); hsh }
    end
  end

  return {
    klass: k,
    id: k.tableize.singularize + '_id',
    resource_path: '/' + k.tableize,
    tasks: tasks,
    base: b
  }
end


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

def destroy_object_link(object)
  content = safe_join([(:span, '', data: { icon: 'trash' }, class: 'small-icon'), 'Destroy'], '')

  if object.is_destroyable?(sessions_current_user)
    link_to(content, object.metamorphosize, method: :delete, data: {confirm: 'Are you sure?'}, class: 'navigation-item')
  else
    (:div, content, class: 'navigation-item disable')
  end
end


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

def download_for_model_link(model)
  content = safe_join([
    (:span, '', class: 'icon', data: { icon: :download }),
    'Download'],
  '')

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

#download_path_for_model(model) ⇒ Object



168
169
170
171
172
173
174
175
# File 'app/helpers/workbench/navigation_helper.rb', line 168

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



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

def edit_object_link(object)
  content = safe_join([(:span, '', data: { icon: 'edit' }, class: 'small-icon'), 'Edit'], '')

  if has_route_for_edit?(object) && object.is_editable?(sessions_current_user)
    link_to(content, edit_object_path(metamorphosize_if(object)), class: 'navigation-item')
  else
    (:div, content, class: 'navigation-item disable')
  end
end

#edit_object_path(object) ⇒ Object



192
193
194
# File 'app/helpers/workbench/navigation_helper.rb', line 192

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

#edit_object_path_string(object) ⇒ String

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

Returns:

  • (String)


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

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


83
84
85
# File 'app/helpers/workbench/navigation_helper.rb', line 83

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)


212
213
214
# File 'app/helpers/workbench/navigation_helper.rb', line 212

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

#header_path_tag ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'app/helpers/workbench/navigation_helper.rb', line 303

def header_path_tag
  key = UserTasks::INDEXED_TASKS.keys.find do |k|
    t = UserTasks::INDEXED_TASKS[k]
    send(t.path) == request.path rescue false
  end
  task_name = UserTasks::INDEXED_TASKS[key]&.name
  namespace      = controller.controller_path.split('/')&.first&.humanize
  controller_label = controller.controller_name.humanize

  label =
    if namespace == controller_label
      "/ #{namespace}"
    else
      "/ #{namespace} / #{task_name || controller_label}"
    end

  (:span, label)
end


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/helpers/workbench/navigation_helper.rb', line 138

def list_for_model_link(model)
  content = safe_join([
    (:span, '', class: 'icon', data: { icon: 'list'}),
    'List'],
  '')

  has_records = model.has_attribute?(:project_id) ? model.where(project_id: sessions_current_project_id).exists?  : model.any?

  if has_records
    link_to(content, list_path_for_model(model))
  else
    (:span, safe_join([
      (:span, '', data: { icon: :list }),
      (:span, "No #{model.name} to list")
    ], ''), class: :disabled)
  end
end

#list_path_for_model(model) ⇒ Object



119
120
121
# File 'app/helpers/workbench/navigation_helper.rb', line 119

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


123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/helpers/workbench/navigation_helper.rb', line 123

def new_for_model_link(model)
  content = safe_join([
    (:span, '', class: 'icon', data: { icon: 'new' }),
    'New'],
  '')

  if NO_NEW_FORMS.include?(model.name)
    nil
  elsif model.name == 'ProjectSource'
    link_to(content, new_source_path)
  else
    link_to(content, new_path_for_model(model), 'class' => 'navigation-item')
  end
end

#new_path_for_model(model) ⇒ Object



115
116
117
# File 'app/helpers/workbench/navigation_helper.rb', line 115

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

Used in show/REST A next record link.



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

def next_link(instance, text: 'Next', target: nil)
  link_text = safe_join([text, (:span, '', class: 'small-icon margin-small-left', 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



328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'app/helpers/workbench/navigation_helper.rb', line 328

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


177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/helpers/workbench/navigation_helper.rb', line 177

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

TODO: Should be IsData controller check, we don't want to maintain NOT_DATA_PATHS

Returns:

  • (Boolean)


65
66
67
# File 'app/helpers/workbench/navigation_helper.rb', line 65

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

Used in show/REST A previous record link.



89
90
91
92
93
94
95
96
97
98
99
# File 'app/helpers/workbench/navigation_helper.rb', line 89

def previous_link(instance, text: 'Previous', target: nil)
  link_text = safe_join([(:span, '',  data: { icon: 'arrow-left' }, class: 'small-icon'), text], '')
  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_bar ⇒ Object



69
70
71
# File 'app/helpers/workbench/navigation_helper.rb', line 69

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


73
74
75
76
77
# File 'app/helpers/workbench/navigation_helper.rb', line 73

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, teleport = nil) ⇒ Object



322
323
324
# File 'app/helpers/workbench/navigation_helper.rb', line 322

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


265
266
267
268
269
270
271
272
273
274
275
276
# File 'app/helpers/workbench/navigation_helper.rb', line 265

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

#safe_object_from_attributes(hsh) ⇒ Object



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

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_clipboard ⇒ Object



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

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

#slideout_pdf_viewer ⇒ Object



59
60
61
# File 'app/helpers/workbench/navigation_helper.rb', line 59

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

#slideout_pinboard ⇒ Object



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

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

#slideout_recent ⇒ Object



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

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

#slideouts ⇒ Object

Slideout panels



39
40
41
42
43
44
45
# File 'app/helpers/workbench/navigation_helper.rb', line 39

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

#task_bar ⇒ Object



79
80
81
# File 'app/helpers/workbench/navigation_helper.rb', line 79

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

#title_tag ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'app/helpers/workbench/navigation_helper.rb', line 287

def title_tag
  splash =
    if current_page?(root_path)
      'Dashboard'
    elsif request.path.include?('task')
      request.path.demodulize.humanize
    else
      request.path.split('/')&.second&.humanize
    end

  project_name = sessions_current_project&.name || 'TaxonWorks'

  (:title, [project_name, splash].compact.join(' - ') )
end