Module: LeadsHelper

Defined in:
app/helpers/leads_helper.rb

Instance Method Summary collapse

Instance Method Details

#couplets_count(lead) ⇒ Object



271
272
273
274
275
276
# File 'app/helpers/leads_helper.rb', line 271

def couplets_count(lead)
  # Couplets - which can have more than two options - are in 1-1
  # correspondence with nodes that have children (via 'couplet' <--> 'parent
  # of that couplet').
  lead.self_and_descendants.count - lead.leaves.count
end

#key_data(lead, metadata, lead_items: false, back_couplets: false) ⇒ Object

An index of lead.id pointing to its content. lead_items is for internal use only.



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
179
180
181
182
183
184
185
186
187
188
# File 'app/helpers/leads_helper.rb', line 140

def key_data(lead, , lead_items: false, back_couplets: false)
  data = {}
  data[:back_couplets] = {} if back_couplets
  lead.self_and_descendants.find_each do |l|
    d = {
      text: l.text,
      parent_id: l.parent_id,
      position: l.position,
    }

    if .dig(l.id, :children)&.any?
      couplet_number = .dig(l.id, :couplet_number)
      d.merge!(
        target_label: couplet_number,
        target_type: :internal,
      )
      if back_couplets && (b = .dig(l.parent_id, :couplet_number))
        data[:back_couplets][couplet_number] = b
      end
    elsif l.otu
      d.merge!(
        target_label: ( l.otu ? label_for_otu(l.otu) : nil ),
        target_id: l.otu&.id,
        target_type: '/api/v1/otus',  # some concept of pointing to the
      )
    elsif l.link_out_text
      d.merge!(
        target_label: l.link_out_text,
        target_type: :link_out,
        target_link: l.link_out
      )
    end

    if lead_items && d[:target_type] != :internal &&
       (lio = lead_item_otus(l)).present?
      d.merge!(
        target_type: 'lead_item_otus',
        lead_item_otus: lio,
        )
    end

    if l.depictions.load.any?
      d.merge!( figures: l.depictions.order(:position).collect{|d| depiction_to_json(d)}  )
    end

    data[l.id] = d
  end
  data
end

#key_metadata(lead, hsh: {}, depth: 1, couplet_number: { num: 0 }) ⇒ Object

A depth-first traversal that numbers each entry. Renderers navigate this list in order to draw the key.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/helpers/leads_helper.rb', line 117

def (lead, hsh: {}, depth: 1, couplet_number: { num: 0 }  )
  if lead.children.any?
    couplet_number[:num] += 1
    hsh[lead.id] = {
      children: [],
      parent_id: lead.parent_id,
      position: lead.position,
      couplet_number: lead.origin_label || couplet_number[:num],
      # depth_vector: [depth, lead.parent_id, lead.position],
      depth:
    }
  end

  lead.children.order(:position).each do |l|
    hsh[l.parent_id][:children].push l.id
    (l, hsh:, depth: depth + 1, couplet_number:)
  end

  hsh
end

#key_to_json(lead) ⇒ Object

Used to serve Keys to the API. Targeting a “standard” for exchanging keys to be used in the front end at github.com/SpeciesFileGroup/pinpoint



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/helpers/leads_helper.rb', line 202

def key_to_json(lead)
  m = (lead)
  d = key_data(lead, m)
  return {
    metadata: {
      server: root_url,
      key_version: '0.0.1',
      title: lead.text,
      origin_citation: lead.source&.cached,
      attribution: attribution_to_json(lead.attribution),
      taxonomic_scope: label_for_otu(lead.otu) # perhaps extend with identifiers, probably ultimately nested, with IDs
    },
    data: {
      entries: m,
      leads: d
    }
  }
end

#label_for_lead(lead) ⇒ Object



31
32
33
# File 'app/helpers/leads_helper.rb', line 31

def label_for_lead(lead)
  lead_tag(lead)
end

#lead_autocomplete_tag(lead) ⇒ Object



27
28
29
# File 'app/helpers/leads_helper.rb', line 27

def lead_autocomplete_tag(lead)
  lead_tag(lead)
end

#lead_id(lead) ⇒ Object



2
3
4
5
# File 'app/helpers/leads_helper.rb', line 2

def lead_id(lead)
  return nil if lead.nil?
  lead.origin_label ? "[#{lead.origin_label}]" : ''
end

#lead_item_otus(lead) ⇒ Object



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

def lead_item_otus(lead)
  otu_ids = lead.lead_items.map(&:otu_id)
  if otu_ids.empty?
    return []
  end

  Otu.where(id: otu_ids).map { |o| label_for_otu(o) }.sort!
end


22
23
24
25
# File 'app/helpers/leads_helper.rb', line 22

def lead_link(lead)
  return nil if lead.nil?
  link_to(lead_tag(lead), lead)
end

#lead_no_textObject



7
8
9
# File 'app/helpers/leads_helper.rb', line 7

def lead_no_text
  '(No text)'
end

#lead_tag(lead) ⇒ Object



17
18
19
20
# File 'app/helpers/leads_helper.rb', line 17

def lead_tag(lead)
  return nil if lead.nil?
  lead_id(lead) + ' ' + lead_truncated_text(lead)
end

#lead_truncated_text(lead) ⇒ Object



11
12
13
14
15
# File 'app/helpers/leads_helper.rb', line 11

def lead_truncated_text(lead)
  return nil if lead.nil?
  text = lead.text || lead_no_text
  text.slice(0..25) + (text.size > 25 ? '...' : '')
end

#leads_recent_objects_partialTrue

Returns indicates a custom partial should be used, see list_helper.rb.

Returns:

  • (True)

    indicates a custom partial should be used, see list_helper.rb



41
42
43
# File 'app/helpers/leads_helper.rb', line 41

def leads_recent_objects_partial
  true
end

#leads_search_formObject



35
36
37
# File 'app/helpers/leads_helper.rb', line 35

def leads_search_form
  render('/leads/quick_search_form')
end


45
46
47
48
49
50
51
# File 'app/helpers/leads_helper.rb', line 45

def print_key(lead)
   = (lead)

  t = tag.h1(lead.text)
  t << print_key_body(lead, )
  t.html_safe
end


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/helpers/leads_helper.rb', line 92

def print_key_body(lead, )
  data = key_data(lead, )
  x = []

  .keys.each do |k|
    [k][:children].each do |lid|
      a = data.dig(lid, :position) == 0 ? .dig(k, :couplet_number).to_s + '.' : '&mdash;'
      b = data.dig(lid, :text)

      if label = data.dig(lid, :target_label)
        c = tag.b(label)
      end

      c = 'TODO: PROVIDE ENDPOINT' if c.blank?

      x.push [a, b, '&mldr;', c, '<br/>']
    end
    x.push '<br/>'
  end

  x.flatten.join(' ').html_safe
end


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/helpers/leads_helper.rb', line 221

def print_key_markdown(lead)
   = (lead)

  data = key_data(lead, )

  t = ["# #{lead.text}\n\n"]

  # key is couplet number, value is which couplet was sent to that number
  backlinks = {}

  .keys.each do |parent|
    [parent][:children].each do |child|
      # Construct child lines of the form:
      # 1. Child 1 text ... [next couplet # | otu] OR
      # -- Child 2 text ... [next couplet # | otu]
      # a        b                   c          (a,b,c defined below)
      cplt_num = .dig(parent, :couplet_number).to_s
      if data.dig(child, :position) == 0
        backlink = ''
        if p = backlinks[cplt_num]
          backlink = "([#{p}](#cplt-#{p}))"
        end
        # The empty <a> tag here provides an id object to link *to*.
        a = "#{cplt_num}#{backlink}\.<a id=\"cplt-#{cplt_num}\"></a>"

      else
        a = '--'
      end

      b = data.dig(child, :text)

      if (target = data.dig(child, :target_label))
        c = "**#{target}**"
        if data.dig(child, :target_type) == :internal # points to next couplet
          backlinks[target.to_s] = cplt_num
          c = "[#{c}](#cplt-#{target})"
        end
      end

      c = 'TODO: PROVIDE ENDPOINT' if c.blank?

      t.push [a, b, '...', c, " \n"].join(' ')
    end
    t.push "\n"
  end

  t.join.html_safe

end


53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/leads_helper.rb', line 53

def print_key_table(lead)
   = (lead)

  t = tag.h1(lead.text)
  t << tag.table(
    print_key_table_body(lead, ),
    id: 'key_table'
  )

  t
end


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
# File 'app/helpers/leads_helper.rb', line 65

def print_key_table_body(lead, )
  data = key_data(lead, )
  x = []

  .keys.each do |k|
    [k][:children].each do |lid|
      a = data.dig(lid, :position) == 0 ? .dig(k, :couplet_number).to_s : '&mdash;'
      b = data.dig(lid, :text)

      if label = data.dig(lid, :target_label)
        c = tag.b(label)
      end

      c = 'TODO: PROVIDE ENDPOINT' if c.blank?

      x.push tag.tr(
        [ tag.td(a.html_safe),
          tag.td(b),
          tag.td(c)
        ].join.html_safe
      )
    end
  end

  x.join.html_safe
end