-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathobject_link_helper.rb
More file actions
178 lines (154 loc) · 5.57 KB
/
object_link_helper.rb
File metadata and controls
178 lines (154 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# frozen_string_literal: true
# helpers for creating links in views
module ObjectLinkHelper
# Wrap location name in span: "<span>where (count)</span>"
#
# Where: <%= where_string(obs.place_name) %>
#
def where_string(where, count = nil)
result = where.t
result += " (#{count})" if count
content_tag(:span, result)
end
# Wrap location name in link to show_location / observations/index.
#
# Where: <%= location_link(obs.where, obs.location) %>
#
def location_link(where, location, count = nil, click = false)
if location
location = Location.find(location) unless location.is_a?(AbstractModel)
link_string = where_string(location.display_name, count)
link_string += " [#{:click_for_map.t}]" if click
link_to(link_string, location_path(id: location.id),
{ id: "show_location_link_#{location.id}" })
else
link_string = where_string(where, count)
link_string += " [#{:SEARCH.t}]" if click
link_to(link_string, observations_path(where: where),
{ id: "index_observations_at_where_link" })
end
end
# Wrap name in link to show_name. Takes id or object
#
# Parent: <%= name_link(name.parent) %>
#
def name_link(name, str = nil)
if name.is_a?(Integer)
str ||= "#{:NAME.t} ##{name}"
link_to(str, name_path(name), { id: "show_name_link_#{name}" })
else
str ||= name.display_name_brief_authors.t
link_to(str, name_path(name.id),
{ id: "show_name_link_#{name.id}" })
end
end
# ----- links to names and records at external websites ----------------------
# url for IF record
def index_fungorum_record_url(record_id)
"http://www.indexfungorum.org/Names/NamesRecord.asp?RecordID=#{record_id}"
end
# url for Index Fungorum search. This is a general search.
# IF lacks an entry point that includes the name to be searched.
def index_fungorum_basic_search_url
"http://www.indexfungorum.org/Names/Names.asp"
end
# url for MB record by number
def mycobank_record_url(record_id)
"#{mycobank_host}/MB/#{record_id}"
end
# url for MycoBank name search for text_name
def mycobank_name_search_url(name)
"#{mycobank_basic_search_url}/field/Taxon%20name/#{
name.text_name.gsub(" ", "%20")
}"
end
def mycobank_basic_search_url
"#{mycobank_host}page/Basic%20names%20search"
end
def mycobank_host
"https://www.mycobank.org/"
end
# url for name search on MyCoPortal
def mycoportal_url(name)
"http://mycoportal.org/portal/taxa/index.php?taxauthid=1&taxon=" \
"#{name.text_name.tr(" ", "+")}"
end
# url of SF page with "official" synonyms by category
# works for species, infra-specific ranks
def species_fungorum_gsd_synonymy(record_id)
"http://www.speciesfungorum.org/Names/GSDspecies.asp?RecordID=#{record_id}"
end
# url of SF page with "official" synonyms in alpha order
# works for species, genus, family
def species_fungorum_sf_synonymy(record_id)
"http://www.speciesfungorum.org/Names/SynSpecies.asp?RecordID=#{record_id}"
end
# ----------------------------------------------------------------------------
# Wrap user name in link to show_user.
#
# Owner: <%= user_link(name.user) %>
# Authors: <%= name.authors.map(&:user_link).join(", ") %>
#
# # If you don't have a full User instance handy:
# Modified by: <%= user_link(login, user_id) %>
#
def user_link(user, name = nil, args = {})
if user.is_a?(Integer)
name ||= "#{:USER.t} ##{user}"
link_to(name, user_path(user),
args.merge({ id: "show_user_link_#{user}" }))
elsif user
name ||= user.unique_text_name
link_to(name, user_path(user.id),
args.merge({ id: "show_user_link_#{user.id}" }))
else
"?"
end
end
# Render a list of users on one line. (Renders nothing if user list empty.)
# This renders the following strings:
#
# <%= user_list("Author", name.authors) %>
#
# empty: ""
# [bob]: "Author: Bob"
# [bob,fred,mary]: "Authors: Bob, Fred, Mary"
#
def user_list(title, users = [])
return safe_empty unless users&.any?
title = users.length > 1 ? title.to_s.pluralize.to_sym.t : title.t
links = users.map { |u| user_link(u, u.legal_name) }
# interpolating would require inefficient #sanitize
# or dangerous #html_safe
title + ": " + links.safe_join(", ")
end
# Wrap object's name in link to the object, return nil if no object
# Project: <%= project_link(draft_name.project) %>
# Species List: <%= species_list_link(observation.species_lists.first) %>
def link_to_object(object, name = nil)
return nil unless object
link_to(name || object.title.t, object.show_link_args,
{ id: "show_#{object.type_tag}_link_#{object.id}" })
end
# Wrap description title in link to show_description.
#
# Description: <%= description_link(name.description) %>
#
def description_link(desc)
result = description_title(desc)
return result if result.match?("(#{:private.t})$")
link_with_query(result, desc.show_link_args,
id: "show_description_link_#{desc.id}")
end
def observation_herbarium_record_link(obs)
count = obs.herbarium_records.size
if count.positive?
link_to((count == 1 ? :herbarium_record.t : :herbarium_records.t),
herbarium_records_path(observation_id: obs.id),
{ id: "herbarium_records_for_observation_link" })
else
return :show_observation_specimen_available.t if obs.specimen
:show_observation_specimen_not_available.t
end
end
end