Module: Workbench::NavigationHelper

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

Overview

Methods for 1) generating paths; or 2) generating links.

Instance Method Summary (collapse)

Instance Method Details



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

def a_to_z_links(targets = [])
  letters = targets.empty? ? a_to_z_range : a_to_z_range.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

- (Object) a_to_z_range



215
216
217
# File 'app/helpers/workbench/navigation_helper.rb', line 215

def a_to_z_range
  ('A'..'Z')
end


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

def annotate_links(object: nil)
  [(:li, add_alternate_value_link(object: object)),
   (:li, add_citation_link(object: object)),
   (:li, add_data_attribute_link(object: object)),
   (:li, add_identifier_link(object: object)),
   (:li, add_note_link(object: object)),
   (:li, add_tag_link(object: object)),
   (:li, add_confidence_link(object: object)),
   (:li, add_protocol_link(object: object))
  ].compact.join.html_safe
end


163
164
165
166
167
168
169
# File 'app/helpers/workbench/navigation_helper.rb', line 163

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


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

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


86
87
88
89
90
91
92
# File 'app/helpers/workbench/navigation_helper.rb', line 86

def download_for_model_link(model)
  if self.controller.respond_to?(:download)
    link_to('Download', download_path_for_model(model), 'data-icon' => 'download')
  else
    (:em, 'Download not yet available.')
  end
end

- (Object) download_path_for_model(model)



94
95
96
# File 'app/helpers/workbench/navigation_helper.rb', line 94

def download_path_for_model(model)
  send("download_#{model.name.tableize}_path")
end

return [A tag, nil]

a link, or disabled link


147
148
149
150
151
152
153
# File 'app/helpers/workbench/navigation_helper.rb', line 147

def edit_object_link(object)
  if is_editable?(object) && user_can_edit?(object)
    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

- (Object) edit_object_path(object)



113
114
115
# File 'app/helpers/workbench/navigation_helper.rb', line 113

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

- (Object) edit_object_path_string(object)



117
118
119
120
121
122
123
124
125
# File 'app/helpers/workbench/navigation_helper.rb', line 117

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


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

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

- (Boolean) is_editable?(object)

return [Boolean]

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

Returns:

  • (Boolean)


129
130
131
# File 'app/helpers/workbench/navigation_helper.rb', line 129

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


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

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

- (Object) list_path_for_model(model)



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

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


68
69
70
71
72
73
74
75
76
# File 'app/helpers/workbench/navigation_helper.rb', line 68

def new_for_model_link(model)
  if %w{Note Tag Citation Identifier DataAttribute AlternateValue GeographicArea ContainerItem ProtocolRelationship}.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

- (Object) new_path_for_model(model)



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

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

A next record link.



50
51
52
53
54
55
56
57
58
# File 'app/helpers/workbench/navigation_helper.rb', line 50

def next_link(instance)
  text = 'Next'
  if instance.respond_to?(:project_id)
    link_object = instance.class.base_class.order(id: :asc).with_project_id(instance.project_id).where(['id > ?', instance.id]).limit(1).first
  else
    link_object = instance.class.base_class.order(id: :asc).where(['id > ?', instance.id]).limit(1).first
  end
  link_object.nil? ? (:div,(:span,text, 'class' => 'small-icon icon-right', 'data-icon' => 'arrow-right'), 'class' => 'navigation-item disable') : link_to((:span,text, 'data-icon' => 'arrow-right', 'class' => 'small-icon icon-right'), link_object.metamorphosize, 'data-arrow' => 'next', 'class' => 'navigation-item')
end


98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/helpers/workbench/navigation_helper.rb', line 98

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

A previous record link.



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

def previous_link(instance)
  text = 'Previous'
  if instance.respond_to?(:project_id)
    link_object = instance.class.base_class.order(id: :desc).with_project_id(instance.project_id).where(['id < ?', instance.id]).limit(1).first
  else
    link_object = instance.class.base_class.order(id: :desc).where(['id < ?', instance.id]).limit(1).first
  end

  link_object.nil? ? (:div,(:span,text, 'class' => 'small-icon', 'data-icon' => 'arrow-left'), 'class' => 'navigation-item disable') : link_to((:span,text, 'data-icon' => 'arrow-left', 'class' => 'small-icon'), link_object.metamorphosize, 'data-arrow' => 'back', 'class' => 'navigation-item')
end

- (Object) quick_bar



19
20
21
# File 'app/helpers/workbench/navigation_helper.rb', line 19

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


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

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


194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/helpers/workbench/navigation_helper.rb', line 194

def recent_route_link(hsh)
  route = hsh.keys.first
  o = safe_object_from_attributes(hsh[route])
  if o.nil?
    link_to(route.parameterize(' - ').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



209
210
211
212
213
# File 'app/helpers/workbench/navigation_helper.rb', line 209

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

- (Object) safe_object_from_attributes(hsh)



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

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

- (Object) slideout_pdf_viewer



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

def slideout_pdf_viewer

  render(partial: '/shared/data/slideout/document')  if sessions_current_project && sessions_signed_in?
end

- (Object) slideout_pinboard

Slideout panels



6
7
8
# File 'app/helpers/workbench/navigation_helper.rb', line 6

def slideout_pinboard
  render(partial: '/shared/data/slideout/pinboard')  if sessions_current_project && sessions_signed_in?
end

- (Object) slideout_recent



10
11
12
# File 'app/helpers/workbench/navigation_helper.rb', line 10

def slideout_recent
  render(partial: '/shared/data/slideout/recent')  if sessions_current_project && sessions_signed_in?
end

- (Object) task_bar



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

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

- (Boolean) user_can_edit?(object)

return [Boolean]

true if the current user has permissions to edit the object in question (does not test whether it is actually editable)

Returns:

  • (Boolean)


135
136
137
# File 'app/helpers/workbench/navigation_helper.rb', line 135

def user_can_edit?(object)
  @sessions_current_user.is_administrator? || user_is_creator?(object)
end

- (Boolean) user_is_creator?(object)

return [Boolean]

true if the current user created this object

Returns:

  • (Boolean)


141
142
143
# File 'app/helpers/workbench/navigation_helper.rb', line 141

def user_is_creator?(object)
  object.created_by_id == $user_id
end