Class: Descriptor

Overview

require_dependency Rails.root.to_s + '/app/models/observation.rb' Descriptors are the general mechanism for describing CollectionObjects (individual specimens) or Otus (taxa).

They come in various types, reflecting the approaches commonly used to describe specimens and OTUs:

Working - raw notes or images, "primal" observations, not yet refined to states or measurements Qualitative - Represents character/character_state expressions as traditionally understood. Quantitative - Single measurements. Sample - Summaries of multiple observations recorded in a statistical manner. Only valid for Otus. For example "length 42-77mm (n=5, min: 42, max:Only valid for Otus."

Defined Under Namespace

Classes: Continuous, Gene, Media, PresenceAbsence, Qualitative, Sample, Working

Constant Summary collapse

ALTERNATE_VALUES_FOR =
[:name, :short_name, :description_name, :key_name].freeze

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SoftValidation

#clear_soft_validations, #fix_for, #fix_soft_validations, #soft_fixed?, #soft_valid?, #soft_validate, #soft_validated?, #soft_validations, #soft_validators

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_in_use?, #similar

Methods included from Shared::Documentation

#document_array=, #documented?, #reject_documentation, #reject_documents

Methods included from Shared::Confidences

#reject_confidences

Methods included from Shared::AlternateValues

#all_values_for, #alternate_valued?

Methods included from Shared::DataAttributes

#import_attributes, #internal_attributes, #keyword_value_hash, #reject_data_attributes

Methods included from Shared::Depictions

#has_depictions?, #image_array=, #reject_depictions, #reject_images

Methods included from Shared::Notes

#concatenated_notes_string, #reject_notes

Methods included from Shared::Tags

#reject_tags, #tag_with, #tagged?, #tagged_with?

Methods included from Shared::Identifiers

#dwc_occurrence_id, #identified?, #next_by_identifier, #previous_by_identifier, #reject_identifiers, #uri, #uuid, #visible_identifiers_for

Methods included from Shared::Citations

#cited?, #mark_citations_for_destruction, #nomenclature_date, #origin_citation_source_id, #reject_citations, #requires_citation?, #sources_by_topic_id

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Class Method Details

.human_name ⇒ Object



55
56
57
# File 'app/models/descriptor.rb', line 55

def self.human_name
  self.name.demodulize.humanize
end

.reindex_positions(project_id) ⇒ Object

Positions are made unique and sequential, nil positions last



134
135
136
137
138
# File 'app/models/descriptor.rb', line 134

def self.reindex_positions(project_id)
  Descriptor.where(project_id:).order(Arel.sql('position NULLS LAST, id')).pluck(:id).each_with_index do |id, index|
    Descriptor.where(id:).update_all(position: index + 1)
  end
end

.sort(descriptor_ids, project_id) ⇒ Object

Returns true sort descriptors (global order) by re-using the positions they already occupy, descriptors not in descriptor_ids keep their position.

Parameters:

  • descriptor_ids (Array)
  • project_id (Integer)

Returns:

  • true sort descriptors (global order) by re-using the positions they already occupy, descriptors not in descriptor_ids keep their position



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/descriptor.rb', line 64

def self.sort(descriptor_ids, project_id)
  descriptors = Descriptor.where(project_id:)
  ids = descriptor_ids.map(&:to_i).uniq & descriptors.where(id: descriptor_ids).pluck(:id)

  transaction do
    descriptors.where(id: ids).order(:id).lock.pluck(:id)

    reindex_positions(project_id) if descriptors.where(position: nil).exists? || descriptors.group(:position).having('COUNT(*) > 1').exists?

    positions = descriptors.where(id: ids).order(:position).pluck(:position)
    ids.each_with_index do |id, index|
      descriptors.where(id:).update_all(position: positions[index])
    end
  end

  true
end

Instance Method Details

#gene? ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'app/models/descriptor.rb', line 127

def gene?
  type == 'Descriptor::Gene'
end

#observation_type ⇒ Object

Returns String, nil the corresponding Observation STI class name.

Returns:

  • String, nil the corresponding Observation STI class name



84
85
86
87
# File 'app/models/descriptor.rb', line 84

def observation_type
  return nil if type.blank?
  'Observation::' + type.split('::').last
end

#presence_absence? ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'app/models/descriptor.rb', line 123

def presence_absence?
  type == 'Descriptor::PresenceAbsence'
end

#qualitative? ⇒ Boolean

TODO: these should be is_ to follow design pattern

Returns:

  • (Boolean)


119
120
121
# File 'app/models/descriptor.rb', line 119

def qualitative?
  type == 'Descriptor::Qualitative'
end

#short_name_is_shorter ⇒ Object (protected)



147
148
149
# File 'app/models/descriptor.rb', line 147

def short_name_is_shorter
  errors.add(:short_name, 'is longer than name!') if short_name && name && (short_name.length > name.length)
end

#sv_short_name_is_short ⇒ Object (protected)



151
152
153
# File 'app/models/descriptor.rb', line 151

def sv_short_name_is_short
  soft_validations.add(:short_name, 'should likely be less than 12 characters long') if short_name && short_name.length > 12
end

#target_name(target, language_id) ⇒ String

Returns name of the descriptor in a particular language.

Returns:

  • (String) —

    name of the descriptor in a particular language



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
# File 'app/models/descriptor.rb', line 91

def target_name(target, language_id)
  n = self.name
  a = nil
  case target
  when :key
    n = self.key_name.nil? ? n : self.key_name
  when :description
    n = self.description_name.nil? ? n : self.description_name
  end
  unless language_id.nil?
    case target
    when :key
      a = AlternateValue::Translation.
        where(alternate_value_object: self, language_id: language_id).
        where("(alternate_values.alternate_value_object_attribute = 'key_name' OR alternate_values.alternate_value_object_attribute = 'name')").
        order(:alternate_value_object_attribute).pluck(:value).first
    when :description
      a = AlternateValue::Translation.
        where(alternate_value_object: self, language_id: language_id).
        where("(alternate_values.alternate_value_object_attribute = 'description_name' OR alternate_values.alternate_value_object_attribute = 'name')").
        order(:alternate_value_object_attribute).pluck(:value).first
    end
  end
  return a.nil? ? n : a
end

#type_is_subclassed ⇒ Object (protected)

TODO: get rid of this



141
142
143
144
145
# File 'app/models/descriptor.rb', line 141

def type_is_subclassed
  if !DESCRIPTOR_TYPES[type]
    errors.add(:type, 'type must be a valid subclass')
  end
end

#unify_relations ⇒ Object

observation_matrix_column_items carries class_name: so inferred_relations drops it. Force it back so column items are moved to the surviving descriptor during unify rather than being destroyed with the removed one.



49
50
51
52
53
# File 'app/models/descriptor.rb', line 49

def unify_relations
  ApplicationEnumeration.klass_reflections(self.class, :has_many).select { |r|
    r.name == :observation_matrix_column_items
  }
end