Class: Observation

Overview

Observations are of various types, e.g. Qualitative, Quantitative, Statistical. Made on an Otu or CollectionObject. They are where you store the data for concepts like traits, phenotypes, measurements, character matrices, descriptive matrices etc.

Direct Known Subclasses

Continuous, Media, PresenceAbsence, Qualitative, Sample, Working

Defined Under Namespace

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Shared::Confidences

#reject_confidences

Methods included from Shared::Tags

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

Methods included from Shared::Notes

#concatenated_notes_string, #reject_notes

Methods included from Shared::Depictions

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

Methods included from Shared::Identifiers

#identified?, #next_by_identifier, #previous_by_identifier, #reject_identifiers

Methods included from Shared::DataAttributes

#import_attributes, #internal_attributes, #keyword_value_hash, #reject_data_attributes

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

Instance Attribute Details

#observation_object_global_idString

TODO: this is not memoized correctly ?!

Returns:

  • (String)


18
19
20
# File 'app/models/observation.rb', line 18

def observation_object_global_id
  @observation_object_global_id
end

Class Method Details

.by_descriptors_and_rows(descriptor_ids, row_object_global_ids) ⇒ Object



57
58
59
60
61
62
63
# File 'app/models/observation.rb', line 57

def self.by_descriptors_and_rows(descriptor_ids, row_object_global_ids)
  collection_object_ids = ::GlobalIdHelper.ids_by_class_name(row_object_global_ids, 'CollectionObject')
  otu_ids = ::GlobalIdHelper.ids_by_class_name(row_object_global_ids, 'Otu')

  where(descriptor_id: descriptor_ids, otu_id: otu_ids).or(
    where(descriptor_id: descriptor_ids, collection_object_id: collection_object_ids))
end

.copy(old_global_id, new_global_id) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/observation.rb', line 113

def self.copy(old_global_id, new_global_id)
  begin
    old = GlobalID::Locator.locate(old_global_id)

    Observation.transaction do
      old.observations.each do |o|
        d = o.dup
        d.update(observation_object_global_id: new_global_id) 
      end
    end
    true
  rescue
    return false
  end
  true
end

.destroy_row(observation_matrix_row_id) ⇒ Object

Remove all observations for the set of descriptors in a given row



131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/observation.rb', line 131

def self.destroy_row(observation_matrix_row_id)
  r = ObservationMatrixRow.find(observation_matrix_row_id)
  begin
    Observation.transaction do
      r.observations.destroy_all
    end
  rescue
    raise
  end
  true
end

.human_nameObject



72
73
74
# File 'app/models/observation.rb', line 72

def self.human_name
  'YAY'
end

.in_observation_matrix(observation_matrix_id) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/observation.rb', line 44

def self.in_observation_matrix(observation_matrix_id)
  om = ObservationMatrix.find(observation_matrix_id)

  # where(descriptor: om.descriptors, otu: om.otus).or(
  # where(descriptor: om.descriptors, collection_object: om.collection_objects))

  a = Observation.where(descriptor: om.descriptors, otu: om.otus)
  b = Observation.where(descriptor: om.descriptors, collection_object: om.collection_objects)

  Observation.from("((#{a.to_sql}) UNION (#{b.to_sql})) as observations").distinct
end

.object_scope(object) ⇒ Object



65
66
67
68
69
70
# File 'app/models/observation.rb', line 65

def self.object_scope(object)
  return Observation.none if object.nil?
  return Observation.where(otu_id: object.id) if object.class.name == 'Otu'
  return Observation.where(collection_object_id: object.id) if object.metamorphosize.class.name == 'CollectionObject'
  Observation.none 
end

Instance Method Details

#continuous?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/models/observation.rb', line 40

def continuous?
  type == 'Observation::Continuous'
end

#convert_observation_object_global_idObject (protected)



157
158
159
# File 'app/models/observation.rb', line 157

def convert_observation_object_global_id
  set_observation_object_id(GlobalID::Locator.locate(observation_object_global_id)) if observation_object_global_id 
end

#observation_objectObject



76
77
78
# File 'app/models/observation.rb', line 76

def observation_object
  [otu, collection_object].compact.first
end

#otu_or_collection_object_setObject (protected)



161
162
163
164
165
# File 'app/models/observation.rb', line 161

def otu_or_collection_object_set
  if otu_id.blank? && collection_object_id.blank? && otu.blank? && collection_object.blank?
    errors.add(:base, 'observations must reference an Otu or collection object')
  end
end

#presence_absence?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/models/observation.rb', line 36

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

#qualitative?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/models/observation.rb', line 32

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

#set_observation_object_id(object) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'app/models/observation.rb', line 85

def set_observation_object_id(object)
  case object.metamorphosize.class.name
  when 'Otu'
    write_attribute(:otu_id, object.id)
  when 'CollectionObject'
    write_attribute(:collection_object_id, object.id)
  else
    return false
  end 
end

#set_type_from_descriptorObject (protected)



145
146
147
148
149
# File 'app/models/observation.rb', line 145

def set_type_from_descriptor
  if type.blank? && descriptor&.type
    write_attribute(:type, 'Observation::' + descriptor.type.split('::').last)
  end
end

#type_matches_descriptorObject (protected)



151
152
153
154
155
# File 'app/models/observation.rb', line 151

def type_matches_descriptor
  a = type&.split('::')&.last
  b = descriptor&.type&.split('::')&.last
  errors.add(:type, 'type of Observation does not match type of Descriptor') if a && b && a != b
end