Module: LeadsHelper

Defined in:
app/helpers/leads_helper.rb

Instance Method Summary collapse

Instance Method Details

#key_data(lead, metadata) ⇒ Object

An index of lead.id pointing to its content



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

def key_data(lead, )
  data = {}
  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?
      d.merge!(
        target_label: .dig(l.id, :couplet_number),
        target_type: :internal,
      )
    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 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.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/helpers/leads_helper.rb', line 123

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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'app/helpers/leads_helper.rb', line 185

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



37
38
39
# File 'app/helpers/leads_helper.rb', line 37

def label_for_lead(lead)
  lead_tag(lead)
end

#lead_autocomplete_tag(lead) ⇒ Object



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

def lead_autocomplete_tag(lead)
  lead_tag(lead)
end

#lead_edges(lead) ⇒ Object



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

def lead_edges(lead)
  edges =
    (lead.parent_id ? '' : '') +
    (lead.children.size > 0 ? '' : '')
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


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

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



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

def lead_tag(lead)
  return nil if lead.nil?
  lead_edges(lead) + 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



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

def leads_recent_objects_partial
  true
end

#leads_search_formObject



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

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


51
52
53
54
55
56
57
# File 'app/helpers/leads_helper.rb', line 51

def print_key(lead)
   = (lead)

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


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/helpers/leads_helper.rb', line 98

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 data.dig(lid, :target_label)
        c = tag.b(data.dig(lid, :target_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


204
205
# File 'app/helpers/leads_helper.rb', line 204

def print_key_markdown(lead)
end


59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/leads_helper.rb', line 59

def print_key_table(lead)
   = (lead)

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

  t
end


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

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 data.dig(lid, :target_label)
        c = tag.b(data.dig(lid, :target_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