Module: TaxonName::OtuSyncronization::ClassMethods

Defined in:
app/models/taxon_name/otu_syncronization.rb

Instance Method Summary collapse

Instance Method Details

#synchronize_otus(taxon_name_id: nil, user_id: nil, mode: :all_valid) ⇒ Int, false

Returns:

  • (Int, false)


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
# File 'app/models/taxon_name/otu_syncronization.rb', line 14

def synchronize_otus(taxon_name_id: nil, user_id: nil, mode: :all_valid)
  return false if mode.nil? || taxon_name_id.nil? || user_id.nil?
  t = TaxonName.find(taxon_name_id)
  i = 0
  names = case mode
          when :all_valid
            t.self_and_descendants.that_is_valid.without_otus
          when :child_valid
            t.children.that_is_valid.without_otus
          when :all_without
            t.self_and_descendants.without_otus
          when :all_invalid
            t.self_and_descendants.that_is_invalid.without_otus
          when :child_invalid
            t.children.that_is_invalid.without_otus
          else
            []
          end
  begin
    names.each do |n|
      Otu.create!(by: user_id, taxon_name: n, project_id: t.project_id)
      i += 1
    end
  rescue ActiveRecord::RecordInvalid
    return false
  end
  i
end