Module: Specify::Model::TreeQueryable

Included in:
Geography, Taxonomy
Defined in:
lib/specify/models/tree_queryable.rb

Overview

Mixin that provides convienience methods for nested hierarchies.

Constant Summary collapse

AMBIGUOUS_MATCH_ERROR =
'Ambiguous results during tree search'

Instance Method Summary collapse

Instance Method Details

#rank(rank_name) ⇒ Object

->


9
10
11
# File 'lib/specify/models/tree_queryable.rb', line 9

def rank(rank_name)
  ranks_dataset.first(Name: rank_name.capitalize)
end

#search_tree(hash) ⇒ Object

-> hash: { 'Highest Rank' => 'Name',

...
'Lowest Rank' => 'Name' }

17
18
19
20
21
22
23
24
25
26
# File 'lib/specify/models/tree_queryable.rb', line 17

def search_tree(hash)
  hash.reduce(nil) do |item, (rank_name, name)|
    searchset = item&.children_dataset || names_dataset
    item = searchset.where(Name: name,
                           rank: rank(rank_name))
    next item.first unless item.count > 1
    raise AMBIGUOUS_MATCH_ERROR +
          " for #{name}: #{item.to_a.map(&:FullName)}"
  end
end