Class: AnatomicalPart
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- AnatomicalPart
- Includes:
- Housekeeping, Shared::AutoUuid, Shared::BiologicalAssociations, Shared::Citations, Shared::Confidences, Shared::Containable, Shared::Conveyances, Shared::DataAttributes, Shared::Depictions, Shared::HasPapertrail, Shared::Identifiers, Shared::IsData, Shared::Loanable, Shared::Notes, Shared::Observations, Shared::OriginRelationship, Shared::ProtocolRelationships, Shared::PurlDeprecation, Shared::Tags, Shared::Taxonomy, Shared::Uri
- Defined in:
- app/models/anatomical_part.rb
Overview
A lightweight but extensible reference to anatomy. It refers to physical objects as instances or concepts (see is_material) The concept is a data node, with inference happening if/when linkages to an Ontology exist. I.e. we are storing only very light-weight data. Use cases:
* Collector goes to field, takes a punch of a fish fin, and returns it ->
AnatomicalPart has_origin FieldOccurrence
* A wing is dissected from a specimen -> AnatomicalPart has_origin Specimen
* A feather is removed from a bird, and a punch from the feather ->
AnatomicalPart has_origin AnatomicalPart has_origin FieldOccurrence
* A louse is collected from the Ear of a bat, the louse is collected, the
Bat is not - AnatomicalPart (ear) has_origin FieldOCcurrence (bat) ->
BiologicalAssociation <- CollectionObject (louse)
Constant Summary collapse
- GRAPH_ENTRY_POINTS =
[:biological_associations, :origin_relationships]
Instance Attribute Summary collapse
-
#is_material ⇒ Boolean
Whether or not the anatomical part is based on a physical object - a head vs.
-
#name ⇒ String
Name of the anatomical part.
-
#no_cached ⇒ Object
Returns the value of attribute no_cached.
-
#origin_object ⇒ Object
origin of this part, only used on create.
-
#preparation_type_id ⇒ Integer
Preparation type of this part.
-
#project_id ⇒ Integer
the project ID.
-
#uri ⇒ Object
Override the :uri defined by Identifiers.
-
#uri_label ⇒ String
Uri label of the uri for the anatomical part.
Class Method Summary collapse
-
.construct_graph_from_low_node(node_id, node_type) ⇒ Object
private
Construct an AnatomicalPart graph starting from the direct non-anatomical-part child of an anatomical part, like an Extract or a Sound.
-
.construct_graph_from_mid_node(anatomical_part_id, forward_only: false) ⇒ Object
private
Construct an AnatomicalPart graph starting from an AnatomicalPart.
-
.construct_graph_from_top_node(node_id, node_type) ⇒ Object
private
Construct anatomical part graph starting at a top node, like CollectionObject, FieldOccurrence, OTU.
-
.graph(id, type) ⇒ Array[Array, Array]
component is edges, i.e.
-
.select_optimized(user_id, project_id, target = nil) ⇒ Hash
Anatomical_parts optimized for user selection.
-
.used_recently(user_id, project_id, used_on = '') ⇒ Scope
The max 10 most recently used anatomical_parts.
Instance Method Summary collapse
- #abort_if_has_descendant_anatomical_part ⇒ Object private
-
#allow_origin_relationship_create?(origin_relationship) ⇒ Boolean
Callback on OriginRelationship#create.
-
#allow_origin_relationship_destroy?(origin_relationship) ⇒ Boolean
Callback on OriginRelationship#destroy.
- #containable? ⇒ Boolean
- #is_material_matches_origin ⇒ Object private
- #is_material_matches_parent_anatomical_part ⇒ Object private
- #name_or_uri_not_both ⇒ Object private
- #propagate_change_to_is_material! ⇒ Object private
- #set_cached ⇒ Object private
- #setOriginRelationshipDestroyContext ⇒ Object private
-
#taxonomic_origin_object ⇒ Object
The top object in the OriginRelationship chain.
- #top_origin_is_valid_origin ⇒ Object private
- #uuid_required ⇒ Object
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::PurlDeprecation
#head_follow, #looks_obsolete_landing?, #purl_value, #sv_purl_not_deprecated
Methods included from Shared::Tags
#reject_tags, #tag_with, #tagged?, #tagged_with?
Methods included from Shared::ProtocolRelationships
#machine_output?, #protocolled?, #reject_protocols
Methods included from Shared::OriginRelationship
#new_objects, #old_objects, #reject_origin_relationships, #set_origin
Methods included from Shared::Containable
#contain, containable_types, #contained?, #contained_by?, #contained_siblings, #enclosing_containers, #put_in_container
Methods included from Shared::Notes
#concatenated_notes_string, #reject_notes
Methods included from Shared::Taxonomy
Methods included from Shared::BiologicalAssociations
Methods included from Shared::Loanable
#all_loan_items, #all_loans, #container_loan_items, #container_loaned?, #container_loans, #container_times_loaned, #current_loan, #current_loan_item, #has_been_loaned?, #is_loanable?, #loan_return_date, #loaned_in_container, #on_loan?, #times_loaned
Methods included from Shared::AutoUuid
#create_object_uuid, #generate_uuid_if_required
Methods included from Shared::Identifiers
#dwc_occurrence_id, #identified?, #next_by_identifier, #previous_by_identifier, #reject_identifiers, #uuid
Methods included from Shared::HasPapertrail
#attribute_updated, #attribute_updater, #detect_version
Methods included from Shared::Conveyances
#has_conveyances?, #reject_conveyances, #reject_sounds, #sound_array=
Methods included from Shared::Depictions
#has_depictions?, #image_array=, #reject_depictions, #reject_images
Methods included from Shared::DataAttributes
#import_attributes, #internal_attributes, #keyword_value_hash, #reject_data_attributes
Methods included from Shared::Confidences
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 Shared::Uri
Methods included from Housekeeping
#has_polymorphic_relationship?
Methods inherited from ApplicationRecord
Instance Attribute Details
#is_material ⇒ Boolean
Whether or not the anatomical part is based on a physical object - a head vs. some head.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'app/models/anatomical_part.rb', line 45 class AnatomicalPart < ApplicationRecord include Housekeeping include Shared::Uri include Shared::Citations include Shared::Confidences include Shared::DataAttributes include Shared::Depictions include Shared::Conveyances include Shared::HasPapertrail include Shared::Identifiers include Shared::AutoUuid # Override the :uri defined by Identifiers. Bad. def uri self[:uri] end include Shared::Loanable include Shared::BiologicalAssociations include Shared::Taxonomy include Shared::Notes include Shared::Observations include Shared::Containable include Shared::OriginRelationship include Shared::ProtocolRelationships include Shared::Tags include Shared::PurlDeprecation include Shared::IsData is_origin_for 'AnatomicalPart', 'Extract', 'Sequence', 'Sound' originates_from 'Otu', 'Specimen', 'Lot', 'CollectionObject', 'AnatomicalPart', 'FieldOccurrence' GRAPH_ENTRY_POINTS = [:biological_associations, :origin_relationships] attr_accessor :origin_object # origin of this part, only used on create attr_accessor :no_cached belongs_to :origin_otu, class_name: 'Otu', foreign_key: :cached_otu_id, inverse_of: :anatomical_parts belongs_to :preparation_type, inverse_of: :anatomical_parts # Needed so we can build the origin chain at the same time we're using it to # verify that a new AnitomicalPart has an OTU ancestor. # Probably not needed outside of creation. has_one :inbound_origin_relationship, -> { where(new_object_type: 'AnatomicalPart') }, as: :new_object, class_name: 'OriginRelationship', inverse_of: :new_object accepts_nested_attributes_for :inbound_origin_relationship # Must run before associated origin_relationships are destroyed. before_destroy :abort_if_has_descendant_anatomical_part, prepend: true around_destroy :setOriginRelationshipDestroyContext, prepend: true after_save :set_cached, unless: -> { self.no_cached } # Runs in the update transaction, so any errors will abort the udpate. after_update :propagate_change_to_is_material!, if: :saved_change_to_is_material? validates :inbound_origin_relationship, presence: true, on: :create validate :name_or_uri_not_both validate :top_origin_is_valid_origin validate :is_material_matches_origin validate :is_material_matches_parent_anatomical_part def uuid_required is_material end # Callback on OriginRelationship#create def allow_origin_relationship_create?(origin_relationship) # Disallow if we already have a parent. return false if origin_relationship.new_object == self && OriginRelationship.where(new_object: self).exists? true end # Callback on OriginRelationship#destroy def allow_origin_relationship_destroy?(origin_relationship) # Allow if we originated the destroy because we're being destroyed (relies # on abort_if_has_descendant_anatomical_part to catch cases where that # shouldn't be allowed). return true if OriginRelationshipDestroyContext.objects_in_destroy&.include?({ id:, type: 'AnatomicalPart' }) # Disallow if origin_relationship is part of our required ancestry chain. return false if origin_relationship.new_object == self true end # @return [Scope] # the max 10 most recently used anatomical_parts def self.used_recently(user_id, project_id, used_on = '') old_types = valid_old_object_classes new_types = valid_new_object_classes return [] if !old_types.include?(used_on) && !new_types.include?(used_on) t = used_on.constantize.arel_table i = t[:updated_at].gt(1.week.ago) .and(t[:updated_by_id].eq(user_id)) .and(t[:project_id].eq(project_id)) p = AnatomicalPart.arel_table o = OriginRelationship.arel_table = used_on.tableize = used_on if old_types.include?(used_on) && new_types.include?(used_on) a = AnatomicalPart.joins(:related_origin_relationships).joins("JOIN #{} AS t1 ON t1.id = origin_relationships.old_object_id AND origin_relationships.old_object_type = '#{}'").where(i) b = AnatomicalPart.joins(:origin_relationships).joins("JOIN #{} AS t2 ON t2.id = origin_relationships.new_object_id AND origin_relationships.new_object_type = '#{}'").where(i) ::Queries.union(AnatomicalPart, [a,b]).pluck(:id).uniq elsif old_types.include?(used_on) AnatomicalPart.joins(:related_origin_relationships).joins("JOIN #{} ON #{}.id = origin_relationships.old_object_id AND origin_relationships.old_object_type = '#{}'").where(i).pluck(:id).uniq else AnatomicalPart.joins(:origin_relationships).joins("JOIN #{} ON #{}.id = origin_relationships.new_object_id AND origin_relationships.new_object_type = '#{}'").where(i).pluck(:id).uniq end end # @return [Hash] anatomical_parts optimized for user selection def self.select_optimized(user_id, project_id, target = nil) r = used_recently(user_id, project_id, target) h = { quick: [], pinboard: AnatomicalPart.pinned_by(user_id).where(project_id:).to_a, recent: [] } if target && !r.empty? n = target.tableize.to_sym h[:recent] = AnatomicalPart.where('"anatomical_parts"."id" IN (?)', r.first(10) ).to_a h[:quick] = (AnatomicalPart.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a + AnatomicalPart.where('"anatomical_parts"."id" IN (?)', r.first(4) ).to_a).uniq else h[:recent] = AnatomicalPart.where(project_id:, updated_by_id: user_id).order('updated_at DESC').limit(10).to_a h[:quick] = AnatomicalPart.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a end h end # @return [Array[Array, Array]] where the first component is nodes, the second # component is edges, i.e. originRelationships. def self.graph(id, type) case type when *(AnatomicalPart.valid_old_object_classes - ['AnatomicalPart']) construct_graph_from_top_node(id, type) when *(AnatomicalPart.valid_new_object_classes - ['AnatomicalPart']) construct_graph_from_low_node(id, type) when 'AnatomicalPart' part = AnatomicalPart.find_by(id:) return [[], []] if part.nil? origin = part.taxonomic_origin_object # This constructs the full graph starting from origin - if we decide we # only want the chain containing the original input then call # graph_from_mid_node instead. construct_graph_from_top_node(origin.id, origin.class.base_class.name) end end # The top object in the OriginRelationship chain. def taxonomic_origin_object return @top_origin_object if @top_origin_object.present? origin = inbound_origin_relationship return nil if origin.nil? # invalid object next_parent = nil while (next_parent = OriginRelationship.where(new_object_id: origin.old_object_id, new_object_type: origin.old_object_type).first) origin = next_parent end @top_origin_object = origin.old_object @top_origin_object end def containable? is_material end private def set_cached update_column(:cached, name || uri_label) # cached_otu_id is determined by external objects, so is never changed by an # update, only a create. return if cached_otu_id top_origin = taxonomic_origin_object if top_origin.class.name == 'Otu' update_column(:cached_otu_id, top_origin.id) else update_column(:cached_otu_id, top_origin.current_otu.id) end end def name_or_uri_not_both has_labelled_uri = uri.present? && uri_label.present? return if has_labelled_uri && !name.present? return if name.present? && !uri.present? && !uri_label.present? errors.add(:base, 'Exactly one of 1) name, or 2) uri *and* uri_label, must be present') end def top_origin_is_valid_origin origin = taxonomic_origin_object errors.add(:base, 'Anatomical parts must be endpoints of an OriginRelationship!') && return if origin.nil? valid = valid_old_object_classes - ['AnatomicalPart'] t = origin.class.base_class.name errors.add(:base, "Class of original origin must be in [#{valid.join(', ')}], not #{t}") && return if !valid.include?(t) return if origin.class.name == 'Otu' errors.add(:base, "Original origin must have a taxon determination!") && return if !origin.taxon_determinations.exists? end def is_material_matches_origin origin = taxonomic_origin_object return if origin.nil? # invalid if origin.class.base_class.name == 'CollectionObject' && is_material != true errors.add(:base, 'CollectionObject parts must have is_material true!') return elsif origin.class.base_class.name == 'Otu' && is_material == true errors.add(:base, 'OTU parts must have is_material false!') return end end def is_material_matches_parent_anatomical_part return if inbound_origin_relationship.nil? # invalid parent = inbound_origin_relationship.old_object return if parent.class.name != 'AnatomicalPart' if parent.is_material != is_material errors.add(:base, 'is_material must match parent value!') end end def propagate_change_to_is_material! # is_material can only be changed on the top AnatomicalPart, so change the # value on all AnatomicalPart descendants. new_is_material = is_material current = [self] while(current.present?) nxt = [] current.each do |o| o.update_column(:is_material, new_is_material) nxt += o.new_objects end current = nxt.filter { |x| x.class.name == 'AnatomicalPart' } end end # Construct an AnatomicalPart graph starting from an AnatomicalPart. Going up # the graph is a chain ending in the source of the AnatomicalPart, going down # we include everything, even after we no longer see AnatomicalParts. def self.construct_graph_from_mid_node(anatomical_part_id, forward_only: false) nodes = [] origin_relationships = [] origin = AnatomicalPart.find(anatomical_part_id) current = [origin] # Go down the graph: going down we show everything, since it all has origin # some anatomical part. while(current.present?) nxt = [] current.each do |o| nodes << o # No restriction on old_object_type. r = OriginRelationship.where(old_object: o) origin_relationships += r nxt += o.new_objects end current = nxt end # Go up the graph (chain). if !forward_only current = origin while (current.present? && current.class.name == 'AnatomicalPart') r = OriginRelationship.where(new_object_id: current.id, new_object_type: 'AnatomicalPart').first if r.present? origin_relationships << r nodes << current.old_objects.first end current = current.old_objects.first end end return nodes, origin_relationships end # Construct anatomical part graph starting at a top node, like # CollectionObject, FieldOccurrence, OTU. This returns *all* AnatomicalPart # children of the top node. def self.construct_graph_from_top_node(node_id, node_type) origin = node_type.constantize.find(node_id) r = OriginRelationship .where( old_object_id: node_id, old_object_type: node_type, new_object_type: 'AnatomicalPart' ) origin_relationships = r nodes = [origin] r.each do |rln| anatomical_part = rln.new_object new_nodes, new_origin_relationships = construct_graph_from_mid_node( anatomical_part.id, forward_only: true ) nodes += new_nodes origin_relationships += new_origin_relationships end return nodes, origin_relationships end # Construct an AnatomicalPart graph starting from the direct # non-anatomical-part child of an anatomical part, like an Extract or a Sound. # @param node_type is *not* AnatomicalPart, but we assume its origin *is*. def self.construct_graph_from_low_node(node_id, node_type) origin = node_type.constantize.find(node_id) r = OriginRelationship .where( new_object_id: node_id, new_object_type: node_type, old_object_type: 'AnatomicalPart' ) anatomical_part = r.first&.old_object # reverse paths are always a chain (for anatomical parts) nodes = [origin] origin_relationships = [] if anatomical_part # This can return more children of anatomical_part than just node. nodes, origin_relationships = construct_graph_from_mid_node( anatomical_part.id ) end return nodes, origin_relationships end def abort_if_has_descendant_anatomical_part if OriginRelationship.where(old_object: self).exists? errors.add(:base, "This part can't be deleted because it's not the end of an AnatomicalPart chain.") throw(:abort) end end def setOriginRelationshipDestroyContext OriginRelationshipDestroyContext.objects_in_destroy ||= Set.new OriginRelationshipDestroyContext.objects_in_destroy << { id:, type: 'AnatomicalPart' } yield ensure OriginRelationshipDestroyContext.objects_in_destroy.delete({ id:, type: 'AnatomicalPart' }) end end |
#name ⇒ String
Name of the anatomical part.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'app/models/anatomical_part.rb', line 45 class AnatomicalPart < ApplicationRecord include Housekeeping include Shared::Uri include Shared::Citations include Shared::Confidences include Shared::DataAttributes include Shared::Depictions include Shared::Conveyances include Shared::HasPapertrail include Shared::Identifiers include Shared::AutoUuid # Override the :uri defined by Identifiers. Bad. def uri self[:uri] end include Shared::Loanable include Shared::BiologicalAssociations include Shared::Taxonomy include Shared::Notes include Shared::Observations include Shared::Containable include Shared::OriginRelationship include Shared::ProtocolRelationships include Shared::Tags include Shared::PurlDeprecation include Shared::IsData is_origin_for 'AnatomicalPart', 'Extract', 'Sequence', 'Sound' originates_from 'Otu', 'Specimen', 'Lot', 'CollectionObject', 'AnatomicalPart', 'FieldOccurrence' GRAPH_ENTRY_POINTS = [:biological_associations, :origin_relationships] attr_accessor :origin_object # origin of this part, only used on create attr_accessor :no_cached belongs_to :origin_otu, class_name: 'Otu', foreign_key: :cached_otu_id, inverse_of: :anatomical_parts belongs_to :preparation_type, inverse_of: :anatomical_parts # Needed so we can build the origin chain at the same time we're using it to # verify that a new AnitomicalPart has an OTU ancestor. # Probably not needed outside of creation. has_one :inbound_origin_relationship, -> { where(new_object_type: 'AnatomicalPart') }, as: :new_object, class_name: 'OriginRelationship', inverse_of: :new_object accepts_nested_attributes_for :inbound_origin_relationship # Must run before associated origin_relationships are destroyed. before_destroy :abort_if_has_descendant_anatomical_part, prepend: true around_destroy :setOriginRelationshipDestroyContext, prepend: true after_save :set_cached, unless: -> { self.no_cached } # Runs in the update transaction, so any errors will abort the udpate. after_update :propagate_change_to_is_material!, if: :saved_change_to_is_material? validates :inbound_origin_relationship, presence: true, on: :create validate :name_or_uri_not_both validate :top_origin_is_valid_origin validate :is_material_matches_origin validate :is_material_matches_parent_anatomical_part def uuid_required is_material end # Callback on OriginRelationship#create def allow_origin_relationship_create?(origin_relationship) # Disallow if we already have a parent. return false if origin_relationship.new_object == self && OriginRelationship.where(new_object: self).exists? true end # Callback on OriginRelationship#destroy def allow_origin_relationship_destroy?(origin_relationship) # Allow if we originated the destroy because we're being destroyed (relies # on abort_if_has_descendant_anatomical_part to catch cases where that # shouldn't be allowed). return true if OriginRelationshipDestroyContext.objects_in_destroy&.include?({ id:, type: 'AnatomicalPart' }) # Disallow if origin_relationship is part of our required ancestry chain. return false if origin_relationship.new_object == self true end # @return [Scope] # the max 10 most recently used anatomical_parts def self.used_recently(user_id, project_id, used_on = '') old_types = valid_old_object_classes new_types = valid_new_object_classes return [] if !old_types.include?(used_on) && !new_types.include?(used_on) t = used_on.constantize.arel_table i = t[:updated_at].gt(1.week.ago) .and(t[:updated_by_id].eq(user_id)) .and(t[:project_id].eq(project_id)) p = AnatomicalPart.arel_table o = OriginRelationship.arel_table = used_on.tableize = used_on if old_types.include?(used_on) && new_types.include?(used_on) a = AnatomicalPart.joins(:related_origin_relationships).joins("JOIN #{} AS t1 ON t1.id = origin_relationships.old_object_id AND origin_relationships.old_object_type = '#{}'").where(i) b = AnatomicalPart.joins(:origin_relationships).joins("JOIN #{} AS t2 ON t2.id = origin_relationships.new_object_id AND origin_relationships.new_object_type = '#{}'").where(i) ::Queries.union(AnatomicalPart, [a,b]).pluck(:id).uniq elsif old_types.include?(used_on) AnatomicalPart.joins(:related_origin_relationships).joins("JOIN #{} ON #{}.id = origin_relationships.old_object_id AND origin_relationships.old_object_type = '#{}'").where(i).pluck(:id).uniq else AnatomicalPart.joins(:origin_relationships).joins("JOIN #{} ON #{}.id = origin_relationships.new_object_id AND origin_relationships.new_object_type = '#{}'").where(i).pluck(:id).uniq end end # @return [Hash] anatomical_parts optimized for user selection def self.select_optimized(user_id, project_id, target = nil) r = used_recently(user_id, project_id, target) h = { quick: [], pinboard: AnatomicalPart.pinned_by(user_id).where(project_id:).to_a, recent: [] } if target && !r.empty? n = target.tableize.to_sym h[:recent] = AnatomicalPart.where('"anatomical_parts"."id" IN (?)', r.first(10) ).to_a h[:quick] = (AnatomicalPart.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a + AnatomicalPart.where('"anatomical_parts"."id" IN (?)', r.first(4) ).to_a).uniq else h[:recent] = AnatomicalPart.where(project_id:, updated_by_id: user_id).order('updated_at DESC').limit(10).to_a h[:quick] = AnatomicalPart.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a end h end # @return [Array[Array, Array]] where the first component is nodes, the second # component is edges, i.e. originRelationships. def self.graph(id, type) case type when *(AnatomicalPart.valid_old_object_classes - ['AnatomicalPart']) construct_graph_from_top_node(id, type) when *(AnatomicalPart.valid_new_object_classes - ['AnatomicalPart']) construct_graph_from_low_node(id, type) when 'AnatomicalPart' part = AnatomicalPart.find_by(id:) return [[], []] if part.nil? origin = part.taxonomic_origin_object # This constructs the full graph starting from origin - if we decide we # only want the chain containing the original input then call # graph_from_mid_node instead. construct_graph_from_top_node(origin.id, origin.class.base_class.name) end end # The top object in the OriginRelationship chain. def taxonomic_origin_object return @top_origin_object if @top_origin_object.present? origin = inbound_origin_relationship return nil if origin.nil? # invalid object next_parent = nil while (next_parent = OriginRelationship.where(new_object_id: origin.old_object_id, new_object_type: origin.old_object_type).first) origin = next_parent end @top_origin_object = origin.old_object @top_origin_object end def containable? is_material end private def set_cached update_column(:cached, name || uri_label) # cached_otu_id is determined by external objects, so is never changed by an # update, only a create. return if cached_otu_id top_origin = taxonomic_origin_object if top_origin.class.name == 'Otu' update_column(:cached_otu_id, top_origin.id) else update_column(:cached_otu_id, top_origin.current_otu.id) end end def name_or_uri_not_both has_labelled_uri = uri.present? && uri_label.present? return if has_labelled_uri && !name.present? return if name.present? && !uri.present? && !uri_label.present? errors.add(:base, 'Exactly one of 1) name, or 2) uri *and* uri_label, must be present') end def top_origin_is_valid_origin origin = taxonomic_origin_object errors.add(:base, 'Anatomical parts must be endpoints of an OriginRelationship!') && return if origin.nil? valid = valid_old_object_classes - ['AnatomicalPart'] t = origin.class.base_class.name errors.add(:base, "Class of original origin must be in [#{valid.join(', ')}], not #{t}") && return if !valid.include?(t) return if origin.class.name == 'Otu' errors.add(:base, "Original origin must have a taxon determination!") && return if !origin.taxon_determinations.exists? end def is_material_matches_origin origin = taxonomic_origin_object return if origin.nil? # invalid if origin.class.base_class.name == 'CollectionObject' && is_material != true errors.add(:base, 'CollectionObject parts must have is_material true!') return elsif origin.class.base_class.name == 'Otu' && is_material == true errors.add(:base, 'OTU parts must have is_material false!') return end end def is_material_matches_parent_anatomical_part return if inbound_origin_relationship.nil? # invalid parent = inbound_origin_relationship.old_object return if parent.class.name != 'AnatomicalPart' if parent.is_material != is_material errors.add(:base, 'is_material must match parent value!') end end def propagate_change_to_is_material! # is_material can only be changed on the top AnatomicalPart, so change the # value on all AnatomicalPart descendants. new_is_material = is_material current = [self] while(current.present?) nxt = [] current.each do |o| o.update_column(:is_material, new_is_material) nxt += o.new_objects end current = nxt.filter { |x| x.class.name == 'AnatomicalPart' } end end # Construct an AnatomicalPart graph starting from an AnatomicalPart. Going up # the graph is a chain ending in the source of the AnatomicalPart, going down # we include everything, even after we no longer see AnatomicalParts. def self.construct_graph_from_mid_node(anatomical_part_id, forward_only: false) nodes = [] origin_relationships = [] origin = AnatomicalPart.find(anatomical_part_id) current = [origin] # Go down the graph: going down we show everything, since it all has origin # some anatomical part. while(current.present?) nxt = [] current.each do |o| nodes << o # No restriction on old_object_type. r = OriginRelationship.where(old_object: o) origin_relationships += r nxt += o.new_objects end current = nxt end # Go up the graph (chain). if !forward_only current = origin while (current.present? && current.class.name == 'AnatomicalPart') r = OriginRelationship.where(new_object_id: current.id, new_object_type: 'AnatomicalPart').first if r.present? origin_relationships << r nodes << current.old_objects.first end current = current.old_objects.first end end return nodes, origin_relationships end # Construct anatomical part graph starting at a top node, like # CollectionObject, FieldOccurrence, OTU. This returns *all* AnatomicalPart # children of the top node. def self.construct_graph_from_top_node(node_id, node_type) origin = node_type.constantize.find(node_id) r = OriginRelationship .where( old_object_id: node_id, old_object_type: node_type, new_object_type: 'AnatomicalPart' ) origin_relationships = r nodes = [origin] r.each do |rln| anatomical_part = rln.new_object new_nodes, new_origin_relationships = construct_graph_from_mid_node( anatomical_part.id, forward_only: true ) nodes += new_nodes origin_relationships += new_origin_relationships end return nodes, origin_relationships end # Construct an AnatomicalPart graph starting from the direct # non-anatomical-part child of an anatomical part, like an Extract or a Sound. # @param node_type is *not* AnatomicalPart, but we assume its origin *is*. def self.construct_graph_from_low_node(node_id, node_type) origin = node_type.constantize.find(node_id) r = OriginRelationship .where( new_object_id: node_id, new_object_type: node_type, old_object_type: 'AnatomicalPart' ) anatomical_part = r.first&.old_object # reverse paths are always a chain (for anatomical parts) nodes = [origin] origin_relationships = [] if anatomical_part # This can return more children of anatomical_part than just node. nodes, origin_relationships = construct_graph_from_mid_node( anatomical_part.id ) end return nodes, origin_relationships end def abort_if_has_descendant_anatomical_part if OriginRelationship.where(old_object: self).exists? errors.add(:base, "This part can't be deleted because it's not the end of an AnatomicalPart chain.") throw(:abort) end end def setOriginRelationshipDestroyContext OriginRelationshipDestroyContext.objects_in_destroy ||= Set.new OriginRelationshipDestroyContext.objects_in_destroy << { id:, type: 'AnatomicalPart' } yield ensure OriginRelationshipDestroyContext.objects_in_destroy.delete({ id:, type: 'AnatomicalPart' }) end end |
#no_cached ⇒ Object
Returns the value of attribute no_cached.
78 79 80 |
# File 'app/models/anatomical_part.rb', line 78 def no_cached @no_cached end |
#origin_object ⇒ Object
origin of this part, only used on create
77 78 79 |
# File 'app/models/anatomical_part.rb', line 77 def origin_object @origin_object end |
#preparation_type_id ⇒ Integer
Preparation type of this part.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'app/models/anatomical_part.rb', line 45 class AnatomicalPart < ApplicationRecord include Housekeeping include Shared::Uri include Shared::Citations include Shared::Confidences include Shared::DataAttributes include Shared::Depictions include Shared::Conveyances include Shared::HasPapertrail include Shared::Identifiers include Shared::AutoUuid # Override the :uri defined by Identifiers. Bad. def uri self[:uri] end include Shared::Loanable include Shared::BiologicalAssociations include Shared::Taxonomy include Shared::Notes include Shared::Observations include Shared::Containable include Shared::OriginRelationship include Shared::ProtocolRelationships include Shared::Tags include Shared::PurlDeprecation include Shared::IsData is_origin_for 'AnatomicalPart', 'Extract', 'Sequence', 'Sound' originates_from 'Otu', 'Specimen', 'Lot', 'CollectionObject', 'AnatomicalPart', 'FieldOccurrence' GRAPH_ENTRY_POINTS = [:biological_associations, :origin_relationships] attr_accessor :origin_object # origin of this part, only used on create attr_accessor :no_cached belongs_to :origin_otu, class_name: 'Otu', foreign_key: :cached_otu_id, inverse_of: :anatomical_parts belongs_to :preparation_type, inverse_of: :anatomical_parts # Needed so we can build the origin chain at the same time we're using it to # verify that a new AnitomicalPart has an OTU ancestor. # Probably not needed outside of creation. has_one :inbound_origin_relationship, -> { where(new_object_type: 'AnatomicalPart') }, as: :new_object, class_name: 'OriginRelationship', inverse_of: :new_object accepts_nested_attributes_for :inbound_origin_relationship # Must run before associated origin_relationships are destroyed. before_destroy :abort_if_has_descendant_anatomical_part, prepend: true around_destroy :setOriginRelationshipDestroyContext, prepend: true after_save :set_cached, unless: -> { self.no_cached } # Runs in the update transaction, so any errors will abort the udpate. after_update :propagate_change_to_is_material!, if: :saved_change_to_is_material? validates :inbound_origin_relationship, presence: true, on: :create validate :name_or_uri_not_both validate :top_origin_is_valid_origin validate :is_material_matches_origin validate :is_material_matches_parent_anatomical_part def uuid_required is_material end # Callback on OriginRelationship#create def allow_origin_relationship_create?(origin_relationship) # Disallow if we already have a parent. return false if origin_relationship.new_object == self && OriginRelationship.where(new_object: self).exists? true end # Callback on OriginRelationship#destroy def allow_origin_relationship_destroy?(origin_relationship) # Allow if we originated the destroy because we're being destroyed (relies # on abort_if_has_descendant_anatomical_part to catch cases where that # shouldn't be allowed). return true if OriginRelationshipDestroyContext.objects_in_destroy&.include?({ id:, type: 'AnatomicalPart' }) # Disallow if origin_relationship is part of our required ancestry chain. return false if origin_relationship.new_object == self true end # @return [Scope] # the max 10 most recently used anatomical_parts def self.used_recently(user_id, project_id, used_on = '') old_types = valid_old_object_classes new_types = valid_new_object_classes return [] if !old_types.include?(used_on) && !new_types.include?(used_on) t = used_on.constantize.arel_table i = t[:updated_at].gt(1.week.ago) .and(t[:updated_by_id].eq(user_id)) .and(t[:project_id].eq(project_id)) p = AnatomicalPart.arel_table o = OriginRelationship.arel_table = used_on.tableize = used_on if old_types.include?(used_on) && new_types.include?(used_on) a = AnatomicalPart.joins(:related_origin_relationships).joins("JOIN #{} AS t1 ON t1.id = origin_relationships.old_object_id AND origin_relationships.old_object_type = '#{}'").where(i) b = AnatomicalPart.joins(:origin_relationships).joins("JOIN #{} AS t2 ON t2.id = origin_relationships.new_object_id AND origin_relationships.new_object_type = '#{}'").where(i) ::Queries.union(AnatomicalPart, [a,b]).pluck(:id).uniq elsif old_types.include?(used_on) AnatomicalPart.joins(:related_origin_relationships).joins("JOIN #{} ON #{}.id = origin_relationships.old_object_id AND origin_relationships.old_object_type = '#{}'").where(i).pluck(:id).uniq else AnatomicalPart.joins(:origin_relationships).joins("JOIN #{} ON #{}.id = origin_relationships.new_object_id AND origin_relationships.new_object_type = '#{}'").where(i).pluck(:id).uniq end end # @return [Hash] anatomical_parts optimized for user selection def self.select_optimized(user_id, project_id, target = nil) r = used_recently(user_id, project_id, target) h = { quick: [], pinboard: AnatomicalPart.pinned_by(user_id).where(project_id:).to_a, recent: [] } if target && !r.empty? n = target.tableize.to_sym h[:recent] = AnatomicalPart.where('"anatomical_parts"."id" IN (?)', r.first(10) ).to_a h[:quick] = (AnatomicalPart.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a + AnatomicalPart.where('"anatomical_parts"."id" IN (?)', r.first(4) ).to_a).uniq else h[:recent] = AnatomicalPart.where(project_id:, updated_by_id: user_id).order('updated_at DESC').limit(10).to_a h[:quick] = AnatomicalPart.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a end h end # @return [Array[Array, Array]] where the first component is nodes, the second # component is edges, i.e. originRelationships. def self.graph(id, type) case type when *(AnatomicalPart.valid_old_object_classes - ['AnatomicalPart']) construct_graph_from_top_node(id, type) when *(AnatomicalPart.valid_new_object_classes - ['AnatomicalPart']) construct_graph_from_low_node(id, type) when 'AnatomicalPart' part = AnatomicalPart.find_by(id:) return [[], []] if part.nil? origin = part.taxonomic_origin_object # This constructs the full graph starting from origin - if we decide we # only want the chain containing the original input then call # graph_from_mid_node instead. construct_graph_from_top_node(origin.id, origin.class.base_class.name) end end # The top object in the OriginRelationship chain. def taxonomic_origin_object return @top_origin_object if @top_origin_object.present? origin = inbound_origin_relationship return nil if origin.nil? # invalid object next_parent = nil while (next_parent = OriginRelationship.where(new_object_id: origin.old_object_id, new_object_type: origin.old_object_type).first) origin = next_parent end @top_origin_object = origin.old_object @top_origin_object end def containable? is_material end private def set_cached update_column(:cached, name || uri_label) # cached_otu_id is determined by external objects, so is never changed by an # update, only a create. return if cached_otu_id top_origin = taxonomic_origin_object if top_origin.class.name == 'Otu' update_column(:cached_otu_id, top_origin.id) else update_column(:cached_otu_id, top_origin.current_otu.id) end end def name_or_uri_not_both has_labelled_uri = uri.present? && uri_label.present? return if has_labelled_uri && !name.present? return if name.present? && !uri.present? && !uri_label.present? errors.add(:base, 'Exactly one of 1) name, or 2) uri *and* uri_label, must be present') end def top_origin_is_valid_origin origin = taxonomic_origin_object errors.add(:base, 'Anatomical parts must be endpoints of an OriginRelationship!') && return if origin.nil? valid = valid_old_object_classes - ['AnatomicalPart'] t = origin.class.base_class.name errors.add(:base, "Class of original origin must be in [#{valid.join(', ')}], not #{t}") && return if !valid.include?(t) return if origin.class.name == 'Otu' errors.add(:base, "Original origin must have a taxon determination!") && return if !origin.taxon_determinations.exists? end def is_material_matches_origin origin = taxonomic_origin_object return if origin.nil? # invalid if origin.class.base_class.name == 'CollectionObject' && is_material != true errors.add(:base, 'CollectionObject parts must have is_material true!') return elsif origin.class.base_class.name == 'Otu' && is_material == true errors.add(:base, 'OTU parts must have is_material false!') return end end def is_material_matches_parent_anatomical_part return if inbound_origin_relationship.nil? # invalid parent = inbound_origin_relationship.old_object return if parent.class.name != 'AnatomicalPart' if parent.is_material != is_material errors.add(:base, 'is_material must match parent value!') end end def propagate_change_to_is_material! # is_material can only be changed on the top AnatomicalPart, so change the # value on all AnatomicalPart descendants. new_is_material = is_material current = [self] while(current.present?) nxt = [] current.each do |o| o.update_column(:is_material, new_is_material) nxt += o.new_objects end current = nxt.filter { |x| x.class.name == 'AnatomicalPart' } end end # Construct an AnatomicalPart graph starting from an AnatomicalPart. Going up # the graph is a chain ending in the source of the AnatomicalPart, going down # we include everything, even after we no longer see AnatomicalParts. def self.construct_graph_from_mid_node(anatomical_part_id, forward_only: false) nodes = [] origin_relationships = [] origin = AnatomicalPart.find(anatomical_part_id) current = [origin] # Go down the graph: going down we show everything, since it all has origin # some anatomical part. while(current.present?) nxt = [] current.each do |o| nodes << o # No restriction on old_object_type. r = OriginRelationship.where(old_object: o) origin_relationships += r nxt += o.new_objects end current = nxt end # Go up the graph (chain). if !forward_only current = origin while (current.present? && current.class.name == 'AnatomicalPart') r = OriginRelationship.where(new_object_id: current.id, new_object_type: 'AnatomicalPart').first if r.present? origin_relationships << r nodes << current.old_objects.first end current = current.old_objects.first end end return nodes, origin_relationships end # Construct anatomical part graph starting at a top node, like # CollectionObject, FieldOccurrence, OTU. This returns *all* AnatomicalPart # children of the top node. def self.construct_graph_from_top_node(node_id, node_type) origin = node_type.constantize.find(node_id) r = OriginRelationship .where( old_object_id: node_id, old_object_type: node_type, new_object_type: 'AnatomicalPart' ) origin_relationships = r nodes = [origin] r.each do |rln| anatomical_part = rln.new_object new_nodes, new_origin_relationships = construct_graph_from_mid_node( anatomical_part.id, forward_only: true ) nodes += new_nodes origin_relationships += new_origin_relationships end return nodes, origin_relationships end # Construct an AnatomicalPart graph starting from the direct # non-anatomical-part child of an anatomical part, like an Extract or a Sound. # @param node_type is *not* AnatomicalPart, but we assume its origin *is*. def self.construct_graph_from_low_node(node_id, node_type) origin = node_type.constantize.find(node_id) r = OriginRelationship .where( new_object_id: node_id, new_object_type: node_type, old_object_type: 'AnatomicalPart' ) anatomical_part = r.first&.old_object # reverse paths are always a chain (for anatomical parts) nodes = [origin] origin_relationships = [] if anatomical_part # This can return more children of anatomical_part than just node. nodes, origin_relationships = construct_graph_from_mid_node( anatomical_part.id ) end return nodes, origin_relationships end def abort_if_has_descendant_anatomical_part if OriginRelationship.where(old_object: self).exists? errors.add(:base, "This part can't be deleted because it's not the end of an AnatomicalPart chain.") throw(:abort) end end def setOriginRelationshipDestroyContext OriginRelationshipDestroyContext.objects_in_destroy ||= Set.new OriginRelationshipDestroyContext.objects_in_destroy << { id:, type: 'AnatomicalPart' } yield ensure OriginRelationshipDestroyContext.objects_in_destroy.delete({ id:, type: 'AnatomicalPart' }) end end |
#project_id ⇒ Integer
the project ID
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'app/models/anatomical_part.rb', line 45 class AnatomicalPart < ApplicationRecord include Housekeeping include Shared::Uri include Shared::Citations include Shared::Confidences include Shared::DataAttributes include Shared::Depictions include Shared::Conveyances include Shared::HasPapertrail include Shared::Identifiers include Shared::AutoUuid # Override the :uri defined by Identifiers. Bad. def uri self[:uri] end include Shared::Loanable include Shared::BiologicalAssociations include Shared::Taxonomy include Shared::Notes include Shared::Observations include Shared::Containable include Shared::OriginRelationship include Shared::ProtocolRelationships include Shared::Tags include Shared::PurlDeprecation include Shared::IsData is_origin_for 'AnatomicalPart', 'Extract', 'Sequence', 'Sound' originates_from 'Otu', 'Specimen', 'Lot', 'CollectionObject', 'AnatomicalPart', 'FieldOccurrence' GRAPH_ENTRY_POINTS = [:biological_associations, :origin_relationships] attr_accessor :origin_object # origin of this part, only used on create attr_accessor :no_cached belongs_to :origin_otu, class_name: 'Otu', foreign_key: :cached_otu_id, inverse_of: :anatomical_parts belongs_to :preparation_type, inverse_of: :anatomical_parts # Needed so we can build the origin chain at the same time we're using it to # verify that a new AnitomicalPart has an OTU ancestor. # Probably not needed outside of creation. has_one :inbound_origin_relationship, -> { where(new_object_type: 'AnatomicalPart') }, as: :new_object, class_name: 'OriginRelationship', inverse_of: :new_object accepts_nested_attributes_for :inbound_origin_relationship # Must run before associated origin_relationships are destroyed. before_destroy :abort_if_has_descendant_anatomical_part, prepend: true around_destroy :setOriginRelationshipDestroyContext, prepend: true after_save :set_cached, unless: -> { self.no_cached } # Runs in the update transaction, so any errors will abort the udpate. after_update :propagate_change_to_is_material!, if: :saved_change_to_is_material? validates :inbound_origin_relationship, presence: true, on: :create validate :name_or_uri_not_both validate :top_origin_is_valid_origin validate :is_material_matches_origin validate :is_material_matches_parent_anatomical_part def uuid_required is_material end # Callback on OriginRelationship#create def allow_origin_relationship_create?(origin_relationship) # Disallow if we already have a parent. return false if origin_relationship.new_object == self && OriginRelationship.where(new_object: self).exists? true end # Callback on OriginRelationship#destroy def allow_origin_relationship_destroy?(origin_relationship) # Allow if we originated the destroy because we're being destroyed (relies # on abort_if_has_descendant_anatomical_part to catch cases where that # shouldn't be allowed). return true if OriginRelationshipDestroyContext.objects_in_destroy&.include?({ id:, type: 'AnatomicalPart' }) # Disallow if origin_relationship is part of our required ancestry chain. return false if origin_relationship.new_object == self true end # @return [Scope] # the max 10 most recently used anatomical_parts def self.used_recently(user_id, project_id, used_on = '') old_types = valid_old_object_classes new_types = valid_new_object_classes return [] if !old_types.include?(used_on) && !new_types.include?(used_on) t = used_on.constantize.arel_table i = t[:updated_at].gt(1.week.ago) .and(t[:updated_by_id].eq(user_id)) .and(t[:project_id].eq(project_id)) p = AnatomicalPart.arel_table o = OriginRelationship.arel_table = used_on.tableize = used_on if old_types.include?(used_on) && new_types.include?(used_on) a = AnatomicalPart.joins(:related_origin_relationships).joins("JOIN #{} AS t1 ON t1.id = origin_relationships.old_object_id AND origin_relationships.old_object_type = '#{}'").where(i) b = AnatomicalPart.joins(:origin_relationships).joins("JOIN #{} AS t2 ON t2.id = origin_relationships.new_object_id AND origin_relationships.new_object_type = '#{}'").where(i) ::Queries.union(AnatomicalPart, [a,b]).pluck(:id).uniq elsif old_types.include?(used_on) AnatomicalPart.joins(:related_origin_relationships).joins("JOIN #{} ON #{}.id = origin_relationships.old_object_id AND origin_relationships.old_object_type = '#{}'").where(i).pluck(:id).uniq else AnatomicalPart.joins(:origin_relationships).joins("JOIN #{} ON #{}.id = origin_relationships.new_object_id AND origin_relationships.new_object_type = '#{}'").where(i).pluck(:id).uniq end end # @return [Hash] anatomical_parts optimized for user selection def self.select_optimized(user_id, project_id, target = nil) r = used_recently(user_id, project_id, target) h = { quick: [], pinboard: AnatomicalPart.pinned_by(user_id).where(project_id:).to_a, recent: [] } if target && !r.empty? n = target.tableize.to_sym h[:recent] = AnatomicalPart.where('"anatomical_parts"."id" IN (?)', r.first(10) ).to_a h[:quick] = (AnatomicalPart.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a + AnatomicalPart.where('"anatomical_parts"."id" IN (?)', r.first(4) ).to_a).uniq else h[:recent] = AnatomicalPart.where(project_id:, updated_by_id: user_id).order('updated_at DESC').limit(10).to_a h[:quick] = AnatomicalPart.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a end h end # @return [Array[Array, Array]] where the first component is nodes, the second # component is edges, i.e. originRelationships. def self.graph(id, type) case type when *(AnatomicalPart.valid_old_object_classes - ['AnatomicalPart']) construct_graph_from_top_node(id, type) when *(AnatomicalPart.valid_new_object_classes - ['AnatomicalPart']) construct_graph_from_low_node(id, type) when 'AnatomicalPart' part = AnatomicalPart.find_by(id:) return [[], []] if part.nil? origin = part.taxonomic_origin_object # This constructs the full graph starting from origin - if we decide we # only want the chain containing the original input then call # graph_from_mid_node instead. construct_graph_from_top_node(origin.id, origin.class.base_class.name) end end # The top object in the OriginRelationship chain. def taxonomic_origin_object return @top_origin_object if @top_origin_object.present? origin = inbound_origin_relationship return nil if origin.nil? # invalid object next_parent = nil while (next_parent = OriginRelationship.where(new_object_id: origin.old_object_id, new_object_type: origin.old_object_type).first) origin = next_parent end @top_origin_object = origin.old_object @top_origin_object end def containable? is_material end private def set_cached update_column(:cached, name || uri_label) # cached_otu_id is determined by external objects, so is never changed by an # update, only a create. return if cached_otu_id top_origin = taxonomic_origin_object if top_origin.class.name == 'Otu' update_column(:cached_otu_id, top_origin.id) else update_column(:cached_otu_id, top_origin.current_otu.id) end end def name_or_uri_not_both has_labelled_uri = uri.present? && uri_label.present? return if has_labelled_uri && !name.present? return if name.present? && !uri.present? && !uri_label.present? errors.add(:base, 'Exactly one of 1) name, or 2) uri *and* uri_label, must be present') end def top_origin_is_valid_origin origin = taxonomic_origin_object errors.add(:base, 'Anatomical parts must be endpoints of an OriginRelationship!') && return if origin.nil? valid = valid_old_object_classes - ['AnatomicalPart'] t = origin.class.base_class.name errors.add(:base, "Class of original origin must be in [#{valid.join(', ')}], not #{t}") && return if !valid.include?(t) return if origin.class.name == 'Otu' errors.add(:base, "Original origin must have a taxon determination!") && return if !origin.taxon_determinations.exists? end def is_material_matches_origin origin = taxonomic_origin_object return if origin.nil? # invalid if origin.class.base_class.name == 'CollectionObject' && is_material != true errors.add(:base, 'CollectionObject parts must have is_material true!') return elsif origin.class.base_class.name == 'Otu' && is_material == true errors.add(:base, 'OTU parts must have is_material false!') return end end def is_material_matches_parent_anatomical_part return if inbound_origin_relationship.nil? # invalid parent = inbound_origin_relationship.old_object return if parent.class.name != 'AnatomicalPart' if parent.is_material != is_material errors.add(:base, 'is_material must match parent value!') end end def propagate_change_to_is_material! # is_material can only be changed on the top AnatomicalPart, so change the # value on all AnatomicalPart descendants. new_is_material = is_material current = [self] while(current.present?) nxt = [] current.each do |o| o.update_column(:is_material, new_is_material) nxt += o.new_objects end current = nxt.filter { |x| x.class.name == 'AnatomicalPart' } end end # Construct an AnatomicalPart graph starting from an AnatomicalPart. Going up # the graph is a chain ending in the source of the AnatomicalPart, going down # we include everything, even after we no longer see AnatomicalParts. def self.construct_graph_from_mid_node(anatomical_part_id, forward_only: false) nodes = [] origin_relationships = [] origin = AnatomicalPart.find(anatomical_part_id) current = [origin] # Go down the graph: going down we show everything, since it all has origin # some anatomical part. while(current.present?) nxt = [] current.each do |o| nodes << o # No restriction on old_object_type. r = OriginRelationship.where(old_object: o) origin_relationships += r nxt += o.new_objects end current = nxt end # Go up the graph (chain). if !forward_only current = origin while (current.present? && current.class.name == 'AnatomicalPart') r = OriginRelationship.where(new_object_id: current.id, new_object_type: 'AnatomicalPart').first if r.present? origin_relationships << r nodes << current.old_objects.first end current = current.old_objects.first end end return nodes, origin_relationships end # Construct anatomical part graph starting at a top node, like # CollectionObject, FieldOccurrence, OTU. This returns *all* AnatomicalPart # children of the top node. def self.construct_graph_from_top_node(node_id, node_type) origin = node_type.constantize.find(node_id) r = OriginRelationship .where( old_object_id: node_id, old_object_type: node_type, new_object_type: 'AnatomicalPart' ) origin_relationships = r nodes = [origin] r.each do |rln| anatomical_part = rln.new_object new_nodes, new_origin_relationships = construct_graph_from_mid_node( anatomical_part.id, forward_only: true ) nodes += new_nodes origin_relationships += new_origin_relationships end return nodes, origin_relationships end # Construct an AnatomicalPart graph starting from the direct # non-anatomical-part child of an anatomical part, like an Extract or a Sound. # @param node_type is *not* AnatomicalPart, but we assume its origin *is*. def self.construct_graph_from_low_node(node_id, node_type) origin = node_type.constantize.find(node_id) r = OriginRelationship .where( new_object_id: node_id, new_object_type: node_type, old_object_type: 'AnatomicalPart' ) anatomical_part = r.first&.old_object # reverse paths are always a chain (for anatomical parts) nodes = [origin] origin_relationships = [] if anatomical_part # This can return more children of anatomical_part than just node. nodes, origin_relationships = construct_graph_from_mid_node( anatomical_part.id ) end return nodes, origin_relationships end def abort_if_has_descendant_anatomical_part if OriginRelationship.where(old_object: self).exists? errors.add(:base, "This part can't be deleted because it's not the end of an AnatomicalPart chain.") throw(:abort) end end def setOriginRelationshipDestroyContext OriginRelationshipDestroyContext.objects_in_destroy ||= Set.new OriginRelationshipDestroyContext.objects_in_destroy << { id:, type: 'AnatomicalPart' } yield ensure OriginRelationshipDestroyContext.objects_in_destroy.delete({ id:, type: 'AnatomicalPart' }) end end |
#uri ⇒ Object
Override the :uri defined by Identifiers. Bad.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'app/models/anatomical_part.rb', line 45 class AnatomicalPart < ApplicationRecord include Housekeeping include Shared::Uri include Shared::Citations include Shared::Confidences include Shared::DataAttributes include Shared::Depictions include Shared::Conveyances include Shared::HasPapertrail include Shared::Identifiers include Shared::AutoUuid # Override the :uri defined by Identifiers. Bad. def uri self[:uri] end include Shared::Loanable include Shared::BiologicalAssociations include Shared::Taxonomy include Shared::Notes include Shared::Observations include Shared::Containable include Shared::OriginRelationship include Shared::ProtocolRelationships include Shared::Tags include Shared::PurlDeprecation include Shared::IsData is_origin_for 'AnatomicalPart', 'Extract', 'Sequence', 'Sound' originates_from 'Otu', 'Specimen', 'Lot', 'CollectionObject', 'AnatomicalPart', 'FieldOccurrence' GRAPH_ENTRY_POINTS = [:biological_associations, :origin_relationships] attr_accessor :origin_object # origin of this part, only used on create attr_accessor :no_cached belongs_to :origin_otu, class_name: 'Otu', foreign_key: :cached_otu_id, inverse_of: :anatomical_parts belongs_to :preparation_type, inverse_of: :anatomical_parts # Needed so we can build the origin chain at the same time we're using it to # verify that a new AnitomicalPart has an OTU ancestor. # Probably not needed outside of creation. has_one :inbound_origin_relationship, -> { where(new_object_type: 'AnatomicalPart') }, as: :new_object, class_name: 'OriginRelationship', inverse_of: :new_object accepts_nested_attributes_for :inbound_origin_relationship # Must run before associated origin_relationships are destroyed. before_destroy :abort_if_has_descendant_anatomical_part, prepend: true around_destroy :setOriginRelationshipDestroyContext, prepend: true after_save :set_cached, unless: -> { self.no_cached } # Runs in the update transaction, so any errors will abort the udpate. after_update :propagate_change_to_is_material!, if: :saved_change_to_is_material? validates :inbound_origin_relationship, presence: true, on: :create validate :name_or_uri_not_both validate :top_origin_is_valid_origin validate :is_material_matches_origin validate :is_material_matches_parent_anatomical_part def uuid_required is_material end # Callback on OriginRelationship#create def allow_origin_relationship_create?(origin_relationship) # Disallow if we already have a parent. return false if origin_relationship.new_object == self && OriginRelationship.where(new_object: self).exists? true end # Callback on OriginRelationship#destroy def allow_origin_relationship_destroy?(origin_relationship) # Allow if we originated the destroy because we're being destroyed (relies # on abort_if_has_descendant_anatomical_part to catch cases where that # shouldn't be allowed). return true if OriginRelationshipDestroyContext.objects_in_destroy&.include?({ id:, type: 'AnatomicalPart' }) # Disallow if origin_relationship is part of our required ancestry chain. return false if origin_relationship.new_object == self true end # @return [Scope] # the max 10 most recently used anatomical_parts def self.used_recently(user_id, project_id, used_on = '') old_types = valid_old_object_classes new_types = valid_new_object_classes return [] if !old_types.include?(used_on) && !new_types.include?(used_on) t = used_on.constantize.arel_table i = t[:updated_at].gt(1.week.ago) .and(t[:updated_by_id].eq(user_id)) .and(t[:project_id].eq(project_id)) p = AnatomicalPart.arel_table o = OriginRelationship.arel_table = used_on.tableize = used_on if old_types.include?(used_on) && new_types.include?(used_on) a = AnatomicalPart.joins(:related_origin_relationships).joins("JOIN #{} AS t1 ON t1.id = origin_relationships.old_object_id AND origin_relationships.old_object_type = '#{}'").where(i) b = AnatomicalPart.joins(:origin_relationships).joins("JOIN #{} AS t2 ON t2.id = origin_relationships.new_object_id AND origin_relationships.new_object_type = '#{}'").where(i) ::Queries.union(AnatomicalPart, [a,b]).pluck(:id).uniq elsif old_types.include?(used_on) AnatomicalPart.joins(:related_origin_relationships).joins("JOIN #{} ON #{}.id = origin_relationships.old_object_id AND origin_relationships.old_object_type = '#{}'").where(i).pluck(:id).uniq else AnatomicalPart.joins(:origin_relationships).joins("JOIN #{} ON #{}.id = origin_relationships.new_object_id AND origin_relationships.new_object_type = '#{}'").where(i).pluck(:id).uniq end end # @return [Hash] anatomical_parts optimized for user selection def self.select_optimized(user_id, project_id, target = nil) r = used_recently(user_id, project_id, target) h = { quick: [], pinboard: AnatomicalPart.pinned_by(user_id).where(project_id:).to_a, recent: [] } if target && !r.empty? n = target.tableize.to_sym h[:recent] = AnatomicalPart.where('"anatomical_parts"."id" IN (?)', r.first(10) ).to_a h[:quick] = (AnatomicalPart.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a + AnatomicalPart.where('"anatomical_parts"."id" IN (?)', r.first(4) ).to_a).uniq else h[:recent] = AnatomicalPart.where(project_id:, updated_by_id: user_id).order('updated_at DESC').limit(10).to_a h[:quick] = AnatomicalPart.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a end h end # @return [Array[Array, Array]] where the first component is nodes, the second # component is edges, i.e. originRelationships. def self.graph(id, type) case type when *(AnatomicalPart.valid_old_object_classes - ['AnatomicalPart']) construct_graph_from_top_node(id, type) when *(AnatomicalPart.valid_new_object_classes - ['AnatomicalPart']) construct_graph_from_low_node(id, type) when 'AnatomicalPart' part = AnatomicalPart.find_by(id:) return [[], []] if part.nil? origin = part.taxonomic_origin_object # This constructs the full graph starting from origin - if we decide we # only want the chain containing the original input then call # graph_from_mid_node instead. construct_graph_from_top_node(origin.id, origin.class.base_class.name) end end # The top object in the OriginRelationship chain. def taxonomic_origin_object return @top_origin_object if @top_origin_object.present? origin = inbound_origin_relationship return nil if origin.nil? # invalid object next_parent = nil while (next_parent = OriginRelationship.where(new_object_id: origin.old_object_id, new_object_type: origin.old_object_type).first) origin = next_parent end @top_origin_object = origin.old_object @top_origin_object end def containable? is_material end private def set_cached update_column(:cached, name || uri_label) # cached_otu_id is determined by external objects, so is never changed by an # update, only a create. return if cached_otu_id top_origin = taxonomic_origin_object if top_origin.class.name == 'Otu' update_column(:cached_otu_id, top_origin.id) else update_column(:cached_otu_id, top_origin.current_otu.id) end end def name_or_uri_not_both has_labelled_uri = uri.present? && uri_label.present? return if has_labelled_uri && !name.present? return if name.present? && !uri.present? && !uri_label.present? errors.add(:base, 'Exactly one of 1) name, or 2) uri *and* uri_label, must be present') end def top_origin_is_valid_origin origin = taxonomic_origin_object errors.add(:base, 'Anatomical parts must be endpoints of an OriginRelationship!') && return if origin.nil? valid = valid_old_object_classes - ['AnatomicalPart'] t = origin.class.base_class.name errors.add(:base, "Class of original origin must be in [#{valid.join(', ')}], not #{t}") && return if !valid.include?(t) return if origin.class.name == 'Otu' errors.add(:base, "Original origin must have a taxon determination!") && return if !origin.taxon_determinations.exists? end def is_material_matches_origin origin = taxonomic_origin_object return if origin.nil? # invalid if origin.class.base_class.name == 'CollectionObject' && is_material != true errors.add(:base, 'CollectionObject parts must have is_material true!') return elsif origin.class.base_class.name == 'Otu' && is_material == true errors.add(:base, 'OTU parts must have is_material false!') return end end def is_material_matches_parent_anatomical_part return if inbound_origin_relationship.nil? # invalid parent = inbound_origin_relationship.old_object return if parent.class.name != 'AnatomicalPart' if parent.is_material != is_material errors.add(:base, 'is_material must match parent value!') end end def propagate_change_to_is_material! # is_material can only be changed on the top AnatomicalPart, so change the # value on all AnatomicalPart descendants. new_is_material = is_material current = [self] while(current.present?) nxt = [] current.each do |o| o.update_column(:is_material, new_is_material) nxt += o.new_objects end current = nxt.filter { |x| x.class.name == 'AnatomicalPart' } end end # Construct an AnatomicalPart graph starting from an AnatomicalPart. Going up # the graph is a chain ending in the source of the AnatomicalPart, going down # we include everything, even after we no longer see AnatomicalParts. def self.construct_graph_from_mid_node(anatomical_part_id, forward_only: false) nodes = [] origin_relationships = [] origin = AnatomicalPart.find(anatomical_part_id) current = [origin] # Go down the graph: going down we show everything, since it all has origin # some anatomical part. while(current.present?) nxt = [] current.each do |o| nodes << o # No restriction on old_object_type. r = OriginRelationship.where(old_object: o) origin_relationships += r nxt += o.new_objects end current = nxt end # Go up the graph (chain). if !forward_only current = origin while (current.present? && current.class.name == 'AnatomicalPart') r = OriginRelationship.where(new_object_id: current.id, new_object_type: 'AnatomicalPart').first if r.present? origin_relationships << r nodes << current.old_objects.first end current = current.old_objects.first end end return nodes, origin_relationships end # Construct anatomical part graph starting at a top node, like # CollectionObject, FieldOccurrence, OTU. This returns *all* AnatomicalPart # children of the top node. def self.construct_graph_from_top_node(node_id, node_type) origin = node_type.constantize.find(node_id) r = OriginRelationship .where( old_object_id: node_id, old_object_type: node_type, new_object_type: 'AnatomicalPart' ) origin_relationships = r nodes = [origin] r.each do |rln| anatomical_part = rln.new_object new_nodes, new_origin_relationships = construct_graph_from_mid_node( anatomical_part.id, forward_only: true ) nodes += new_nodes origin_relationships += new_origin_relationships end return nodes, origin_relationships end # Construct an AnatomicalPart graph starting from the direct # non-anatomical-part child of an anatomical part, like an Extract or a Sound. # @param node_type is *not* AnatomicalPart, but we assume its origin *is*. def self.construct_graph_from_low_node(node_id, node_type) origin = node_type.constantize.find(node_id) r = OriginRelationship .where( new_object_id: node_id, new_object_type: node_type, old_object_type: 'AnatomicalPart' ) anatomical_part = r.first&.old_object # reverse paths are always a chain (for anatomical parts) nodes = [origin] origin_relationships = [] if anatomical_part # This can return more children of anatomical_part than just node. nodes, origin_relationships = construct_graph_from_mid_node( anatomical_part.id ) end return nodes, origin_relationships end def abort_if_has_descendant_anatomical_part if OriginRelationship.where(old_object: self).exists? errors.add(:base, "This part can't be deleted because it's not the end of an AnatomicalPart chain.") throw(:abort) end end def setOriginRelationshipDestroyContext OriginRelationshipDestroyContext.objects_in_destroy ||= Set.new OriginRelationshipDestroyContext.objects_in_destroy << { id:, type: 'AnatomicalPart' } yield ensure OriginRelationshipDestroyContext.objects_in_destroy.delete({ id:, type: 'AnatomicalPart' }) end end |
#uri_label ⇒ String
Uri label of the uri for the anatomical part.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'app/models/anatomical_part.rb', line 45 class AnatomicalPart < ApplicationRecord include Housekeeping include Shared::Uri include Shared::Citations include Shared::Confidences include Shared::DataAttributes include Shared::Depictions include Shared::Conveyances include Shared::HasPapertrail include Shared::Identifiers include Shared::AutoUuid # Override the :uri defined by Identifiers. Bad. def uri self[:uri] end include Shared::Loanable include Shared::BiologicalAssociations include Shared::Taxonomy include Shared::Notes include Shared::Observations include Shared::Containable include Shared::OriginRelationship include Shared::ProtocolRelationships include Shared::Tags include Shared::PurlDeprecation include Shared::IsData is_origin_for 'AnatomicalPart', 'Extract', 'Sequence', 'Sound' originates_from 'Otu', 'Specimen', 'Lot', 'CollectionObject', 'AnatomicalPart', 'FieldOccurrence' GRAPH_ENTRY_POINTS = [:biological_associations, :origin_relationships] attr_accessor :origin_object # origin of this part, only used on create attr_accessor :no_cached belongs_to :origin_otu, class_name: 'Otu', foreign_key: :cached_otu_id, inverse_of: :anatomical_parts belongs_to :preparation_type, inverse_of: :anatomical_parts # Needed so we can build the origin chain at the same time we're using it to # verify that a new AnitomicalPart has an OTU ancestor. # Probably not needed outside of creation. has_one :inbound_origin_relationship, -> { where(new_object_type: 'AnatomicalPart') }, as: :new_object, class_name: 'OriginRelationship', inverse_of: :new_object accepts_nested_attributes_for :inbound_origin_relationship # Must run before associated origin_relationships are destroyed. before_destroy :abort_if_has_descendant_anatomical_part, prepend: true around_destroy :setOriginRelationshipDestroyContext, prepend: true after_save :set_cached, unless: -> { self.no_cached } # Runs in the update transaction, so any errors will abort the udpate. after_update :propagate_change_to_is_material!, if: :saved_change_to_is_material? validates :inbound_origin_relationship, presence: true, on: :create validate :name_or_uri_not_both validate :top_origin_is_valid_origin validate :is_material_matches_origin validate :is_material_matches_parent_anatomical_part def uuid_required is_material end # Callback on OriginRelationship#create def allow_origin_relationship_create?(origin_relationship) # Disallow if we already have a parent. return false if origin_relationship.new_object == self && OriginRelationship.where(new_object: self).exists? true end # Callback on OriginRelationship#destroy def allow_origin_relationship_destroy?(origin_relationship) # Allow if we originated the destroy because we're being destroyed (relies # on abort_if_has_descendant_anatomical_part to catch cases where that # shouldn't be allowed). return true if OriginRelationshipDestroyContext.objects_in_destroy&.include?({ id:, type: 'AnatomicalPart' }) # Disallow if origin_relationship is part of our required ancestry chain. return false if origin_relationship.new_object == self true end # @return [Scope] # the max 10 most recently used anatomical_parts def self.used_recently(user_id, project_id, used_on = '') old_types = valid_old_object_classes new_types = valid_new_object_classes return [] if !old_types.include?(used_on) && !new_types.include?(used_on) t = used_on.constantize.arel_table i = t[:updated_at].gt(1.week.ago) .and(t[:updated_by_id].eq(user_id)) .and(t[:project_id].eq(project_id)) p = AnatomicalPart.arel_table o = OriginRelationship.arel_table = used_on.tableize = used_on if old_types.include?(used_on) && new_types.include?(used_on) a = AnatomicalPart.joins(:related_origin_relationships).joins("JOIN #{} AS t1 ON t1.id = origin_relationships.old_object_id AND origin_relationships.old_object_type = '#{}'").where(i) b = AnatomicalPart.joins(:origin_relationships).joins("JOIN #{} AS t2 ON t2.id = origin_relationships.new_object_id AND origin_relationships.new_object_type = '#{}'").where(i) ::Queries.union(AnatomicalPart, [a,b]).pluck(:id).uniq elsif old_types.include?(used_on) AnatomicalPart.joins(:related_origin_relationships).joins("JOIN #{} ON #{}.id = origin_relationships.old_object_id AND origin_relationships.old_object_type = '#{}'").where(i).pluck(:id).uniq else AnatomicalPart.joins(:origin_relationships).joins("JOIN #{} ON #{}.id = origin_relationships.new_object_id AND origin_relationships.new_object_type = '#{}'").where(i).pluck(:id).uniq end end # @return [Hash] anatomical_parts optimized for user selection def self.select_optimized(user_id, project_id, target = nil) r = used_recently(user_id, project_id, target) h = { quick: [], pinboard: AnatomicalPart.pinned_by(user_id).where(project_id:).to_a, recent: [] } if target && !r.empty? n = target.tableize.to_sym h[:recent] = AnatomicalPart.where('"anatomical_parts"."id" IN (?)', r.first(10) ).to_a h[:quick] = (AnatomicalPart.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a + AnatomicalPart.where('"anatomical_parts"."id" IN (?)', r.first(4) ).to_a).uniq else h[:recent] = AnatomicalPart.where(project_id:, updated_by_id: user_id).order('updated_at DESC').limit(10).to_a h[:quick] = AnatomicalPart.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a end h end # @return [Array[Array, Array]] where the first component is nodes, the second # component is edges, i.e. originRelationships. def self.graph(id, type) case type when *(AnatomicalPart.valid_old_object_classes - ['AnatomicalPart']) construct_graph_from_top_node(id, type) when *(AnatomicalPart.valid_new_object_classes - ['AnatomicalPart']) construct_graph_from_low_node(id, type) when 'AnatomicalPart' part = AnatomicalPart.find_by(id:) return [[], []] if part.nil? origin = part.taxonomic_origin_object # This constructs the full graph starting from origin - if we decide we # only want the chain containing the original input then call # graph_from_mid_node instead. construct_graph_from_top_node(origin.id, origin.class.base_class.name) end end # The top object in the OriginRelationship chain. def taxonomic_origin_object return @top_origin_object if @top_origin_object.present? origin = inbound_origin_relationship return nil if origin.nil? # invalid object next_parent = nil while (next_parent = OriginRelationship.where(new_object_id: origin.old_object_id, new_object_type: origin.old_object_type).first) origin = next_parent end @top_origin_object = origin.old_object @top_origin_object end def containable? is_material end private def set_cached update_column(:cached, name || uri_label) # cached_otu_id is determined by external objects, so is never changed by an # update, only a create. return if cached_otu_id top_origin = taxonomic_origin_object if top_origin.class.name == 'Otu' update_column(:cached_otu_id, top_origin.id) else update_column(:cached_otu_id, top_origin.current_otu.id) end end def name_or_uri_not_both has_labelled_uri = uri.present? && uri_label.present? return if has_labelled_uri && !name.present? return if name.present? && !uri.present? && !uri_label.present? errors.add(:base, 'Exactly one of 1) name, or 2) uri *and* uri_label, must be present') end def top_origin_is_valid_origin origin = taxonomic_origin_object errors.add(:base, 'Anatomical parts must be endpoints of an OriginRelationship!') && return if origin.nil? valid = valid_old_object_classes - ['AnatomicalPart'] t = origin.class.base_class.name errors.add(:base, "Class of original origin must be in [#{valid.join(', ')}], not #{t}") && return if !valid.include?(t) return if origin.class.name == 'Otu' errors.add(:base, "Original origin must have a taxon determination!") && return if !origin.taxon_determinations.exists? end def is_material_matches_origin origin = taxonomic_origin_object return if origin.nil? # invalid if origin.class.base_class.name == 'CollectionObject' && is_material != true errors.add(:base, 'CollectionObject parts must have is_material true!') return elsif origin.class.base_class.name == 'Otu' && is_material == true errors.add(:base, 'OTU parts must have is_material false!') return end end def is_material_matches_parent_anatomical_part return if inbound_origin_relationship.nil? # invalid parent = inbound_origin_relationship.old_object return if parent.class.name != 'AnatomicalPart' if parent.is_material != is_material errors.add(:base, 'is_material must match parent value!') end end def propagate_change_to_is_material! # is_material can only be changed on the top AnatomicalPart, so change the # value on all AnatomicalPart descendants. new_is_material = is_material current = [self] while(current.present?) nxt = [] current.each do |o| o.update_column(:is_material, new_is_material) nxt += o.new_objects end current = nxt.filter { |x| x.class.name == 'AnatomicalPart' } end end # Construct an AnatomicalPart graph starting from an AnatomicalPart. Going up # the graph is a chain ending in the source of the AnatomicalPart, going down # we include everything, even after we no longer see AnatomicalParts. def self.construct_graph_from_mid_node(anatomical_part_id, forward_only: false) nodes = [] origin_relationships = [] origin = AnatomicalPart.find(anatomical_part_id) current = [origin] # Go down the graph: going down we show everything, since it all has origin # some anatomical part. while(current.present?) nxt = [] current.each do |o| nodes << o # No restriction on old_object_type. r = OriginRelationship.where(old_object: o) origin_relationships += r nxt += o.new_objects end current = nxt end # Go up the graph (chain). if !forward_only current = origin while (current.present? && current.class.name == 'AnatomicalPart') r = OriginRelationship.where(new_object_id: current.id, new_object_type: 'AnatomicalPart').first if r.present? origin_relationships << r nodes << current.old_objects.first end current = current.old_objects.first end end return nodes, origin_relationships end # Construct anatomical part graph starting at a top node, like # CollectionObject, FieldOccurrence, OTU. This returns *all* AnatomicalPart # children of the top node. def self.construct_graph_from_top_node(node_id, node_type) origin = node_type.constantize.find(node_id) r = OriginRelationship .where( old_object_id: node_id, old_object_type: node_type, new_object_type: 'AnatomicalPart' ) origin_relationships = r nodes = [origin] r.each do |rln| anatomical_part = rln.new_object new_nodes, new_origin_relationships = construct_graph_from_mid_node( anatomical_part.id, forward_only: true ) nodes += new_nodes origin_relationships += new_origin_relationships end return nodes, origin_relationships end # Construct an AnatomicalPart graph starting from the direct # non-anatomical-part child of an anatomical part, like an Extract or a Sound. # @param node_type is *not* AnatomicalPart, but we assume its origin *is*. def self.construct_graph_from_low_node(node_id, node_type) origin = node_type.constantize.find(node_id) r = OriginRelationship .where( new_object_id: node_id, new_object_type: node_type, old_object_type: 'AnatomicalPart' ) anatomical_part = r.first&.old_object # reverse paths are always a chain (for anatomical parts) nodes = [origin] origin_relationships = [] if anatomical_part # This can return more children of anatomical_part than just node. nodes, origin_relationships = construct_graph_from_mid_node( anatomical_part.id ) end return nodes, origin_relationships end def abort_if_has_descendant_anatomical_part if OriginRelationship.where(old_object: self).exists? errors.add(:base, "This part can't be deleted because it's not the end of an AnatomicalPart chain.") throw(:abort) end end def setOriginRelationshipDestroyContext OriginRelationshipDestroyContext.objects_in_destroy ||= Set.new OriginRelationshipDestroyContext.objects_in_destroy << { id:, type: 'AnatomicalPart' } yield ensure OriginRelationshipDestroyContext.objects_in_destroy.delete({ id:, type: 'AnatomicalPart' }) end end |
Class Method Details
.construct_graph_from_low_node(node_id, node_type) ⇒ Object (private)
Construct an AnatomicalPart graph starting from the direct non-anatomical-part child of an anatomical part, like an Extract or a Sound.
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'app/models/anatomical_part.rb', line 380 def self.construct_graph_from_low_node(node_id, node_type) origin = node_type.constantize.find(node_id) r = OriginRelationship .where( new_object_id: node_id, new_object_type: node_type, old_object_type: 'AnatomicalPart' ) anatomical_part = r.first&.old_object # reverse paths are always a chain (for anatomical parts) nodes = [origin] origin_relationships = [] if anatomical_part # This can return more children of anatomical_part than just node. nodes, origin_relationships = construct_graph_from_mid_node( anatomical_part.id ) end return nodes, origin_relationships end |
.construct_graph_from_mid_node(anatomical_part_id, forward_only: false) ⇒ Object (private)
Construct an AnatomicalPart graph starting from an AnatomicalPart. Going up the graph is a chain ending in the source of the AnatomicalPart, going down we include everything, even after we no longer see AnatomicalParts.
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'app/models/anatomical_part.rb', line 312 def self.construct_graph_from_mid_node(anatomical_part_id, forward_only: false) nodes = [] origin_relationships = [] origin = AnatomicalPart.find(anatomical_part_id) current = [origin] # Go down the graph: going down we show everything, since it all has origin # some anatomical part. while(current.present?) nxt = [] current.each do |o| nodes << o # No restriction on old_object_type. r = OriginRelationship.where(old_object: o) origin_relationships += r nxt += o.new_objects end current = nxt end # Go up the graph (chain). if !forward_only current = origin while (current.present? && current.class.name == 'AnatomicalPart') r = OriginRelationship.where(new_object_id: current.id, new_object_type: 'AnatomicalPart').first if r.present? origin_relationships << r nodes << current.old_objects.first end current = current.old_objects.first end end return nodes, origin_relationships end |
.construct_graph_from_top_node(node_id, node_type) ⇒ Object (private)
Construct anatomical part graph starting at a top node, like CollectionObject, FieldOccurrence, OTU. This returns all AnatomicalPart children of the top node.
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'app/models/anatomical_part.rb', line 354 def self.construct_graph_from_top_node(node_id, node_type) origin = node_type.constantize.find(node_id) r = OriginRelationship .where( old_object_id: node_id, old_object_type: node_type, new_object_type: 'AnatomicalPart' ) origin_relationships = r nodes = [origin] r.each do |rln| anatomical_part = rln.new_object new_nodes, new_origin_relationships = construct_graph_from_mid_node( anatomical_part.id, forward_only: true ) nodes += new_nodes origin_relationships += new_origin_relationships end return nodes, origin_relationships end |
.graph(id, type) ⇒ Array[Array, Array]
component is edges, i.e. originRelationships.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'app/models/anatomical_part.rb', line 190 def self.graph(id, type) case type when *(AnatomicalPart.valid_old_object_classes - ['AnatomicalPart']) construct_graph_from_top_node(id, type) when *(AnatomicalPart.valid_new_object_classes - ['AnatomicalPart']) construct_graph_from_low_node(id, type) when 'AnatomicalPart' part = AnatomicalPart.find_by(id:) return [[], []] if part.nil? origin = part.taxonomic_origin_object # This constructs the full graph starting from origin - if we decide we # only want the chain containing the original input then call # graph_from_mid_node instead. construct_graph_from_top_node(origin.id, origin.class.base_class.name) end end |
.select_optimized(user_id, project_id, target = nil) ⇒ Hash
Returns anatomical_parts optimized for user selection.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'app/models/anatomical_part.rb', line 167 def self.select_optimized(user_id, project_id, target = nil) r = used_recently(user_id, project_id, target) h = { quick: [], pinboard: AnatomicalPart.pinned_by(user_id).where(project_id:).to_a, recent: [] } if target && !r.empty? n = target.tableize.to_sym h[:recent] = AnatomicalPart.where('"anatomical_parts"."id" IN (?)', r.first(10) ).to_a h[:quick] = (AnatomicalPart.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a + AnatomicalPart.where('"anatomical_parts"."id" IN (?)', r.first(4) ).to_a).uniq else h[:recent] = AnatomicalPart.where(project_id:, updated_by_id: user_id).order('updated_at DESC').limit(10).to_a h[:quick] = AnatomicalPart.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a end h end |
.used_recently(user_id, project_id, used_on = '') ⇒ Scope
Returns the max 10 most recently used anatomical_parts.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'app/models/anatomical_part.rb', line 138 def self.used_recently(user_id, project_id, used_on = '') old_types = valid_old_object_classes new_types = valid_new_object_classes return [] if !old_types.include?(used_on) && !new_types.include?(used_on) t = used_on.constantize.arel_table i = t[:updated_at].gt(1.week.ago) .and(t[:updated_by_id].eq(user_id)) .and(t[:project_id].eq(project_id)) p = AnatomicalPart.arel_table o = OriginRelationship.arel_table = used_on.tableize = used_on if old_types.include?(used_on) && new_types.include?(used_on) a = AnatomicalPart.joins(:related_origin_relationships).joins("JOIN #{} AS t1 ON t1.id = origin_relationships.old_object_id AND origin_relationships.old_object_type = '#{}'").where(i) b = AnatomicalPart.joins(:origin_relationships).joins("JOIN #{} AS t2 ON t2.id = origin_relationships.new_object_id AND origin_relationships.new_object_type = '#{}'").where(i) ::Queries.union(AnatomicalPart, [a,b]).pluck(:id).uniq elsif old_types.include?(used_on) AnatomicalPart.joins(:related_origin_relationships).joins("JOIN #{} ON #{}.id = origin_relationships.old_object_id AND origin_relationships.old_object_type = '#{}'").where(i).pluck(:id).uniq else AnatomicalPart.joins(:origin_relationships).joins("JOIN #{} ON #{}.id = origin_relationships.new_object_id AND origin_relationships.new_object_type = '#{}'").where(i).pluck(:id).uniq end end |
Instance Method Details
#abort_if_has_descendant_anatomical_part ⇒ Object (private)
403 404 405 406 407 408 |
# File 'app/models/anatomical_part.rb', line 403 def abort_if_has_descendant_anatomical_part if OriginRelationship.where(old_object: self).exists? errors.add(:base, "This part can't be deleted because it's not the end of an AnatomicalPart chain.") throw(:abort) end end |
#allow_origin_relationship_create?(origin_relationship) ⇒ Boolean
Callback on OriginRelationship#create
113 114 115 116 117 118 119 |
# File 'app/models/anatomical_part.rb', line 113 def allow_origin_relationship_create?(origin_relationship) # Disallow if we already have a parent. return false if origin_relationship.new_object == self && OriginRelationship.where(new_object: self).exists? true end |
#allow_origin_relationship_destroy?(origin_relationship) ⇒ Boolean
Callback on OriginRelationship#destroy
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'app/models/anatomical_part.rb', line 122 def allow_origin_relationship_destroy?(origin_relationship) # Allow if we originated the destroy because we're being destroyed (relies # on abort_if_has_descendant_anatomical_part to catch cases where that # shouldn't be allowed). return true if OriginRelationshipDestroyContext.objects_in_destroy&.include?({ id:, type: 'AnatomicalPart' }) # Disallow if origin_relationship is part of our required ancestry chain. return false if origin_relationship.new_object == self true end |
#containable? ⇒ Boolean
224 225 226 |
# File 'app/models/anatomical_part.rb', line 224 def containable? is_material end |
#is_material_matches_origin ⇒ Object (private)
268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'app/models/anatomical_part.rb', line 268 def is_material_matches_origin origin = taxonomic_origin_object return if origin.nil? # invalid if origin.class.base_class.name == 'CollectionObject' && is_material != true errors.add(:base, 'CollectionObject parts must have is_material true!') return elsif origin.class.base_class.name == 'Otu' && is_material == true errors.add(:base, 'OTU parts must have is_material false!') return end end |
#is_material_matches_parent_anatomical_part ⇒ Object (private)
281 282 283 284 285 286 287 288 289 |
# File 'app/models/anatomical_part.rb', line 281 def is_material_matches_parent_anatomical_part return if inbound_origin_relationship.nil? # invalid parent = inbound_origin_relationship.old_object return if parent.class.name != 'AnatomicalPart' if parent.is_material != is_material errors.add(:base, 'is_material must match parent value!') end end |
#name_or_uri_not_both ⇒ Object (private)
246 247 248 249 250 251 252 |
# File 'app/models/anatomical_part.rb', line 246 def name_or_uri_not_both has_labelled_uri = uri.present? && uri_label.present? return if has_labelled_uri && !name.present? return if name.present? && !uri.present? && !uri_label.present? errors.add(:base, 'Exactly one of 1) name, or 2) uri *and* uri_label, must be present') end |
#propagate_change_to_is_material! ⇒ Object (private)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'app/models/anatomical_part.rb', line 291 def propagate_change_to_is_material! # is_material can only be changed on the top AnatomicalPart, so change the # value on all AnatomicalPart descendants. new_is_material = is_material current = [self] while(current.present?) nxt = [] current.each do |o| o.update_column(:is_material, new_is_material) nxt += o.new_objects end current = nxt.filter { |x| x.class.name == 'AnatomicalPart' } end end |
#set_cached ⇒ Object (private)
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'app/models/anatomical_part.rb', line 230 def set_cached update_column(:cached, name || uri_label) # cached_otu_id is determined by external objects, so is never changed by an # update, only a create. return if cached_otu_id top_origin = taxonomic_origin_object if top_origin.class.name == 'Otu' update_column(:cached_otu_id, top_origin.id) else update_column(:cached_otu_id, top_origin.current_otu.id) end end |
#setOriginRelationshipDestroyContext ⇒ Object (private)
410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'app/models/anatomical_part.rb', line 410 def setOriginRelationshipDestroyContext OriginRelationshipDestroyContext.objects_in_destroy ||= Set.new OriginRelationshipDestroyContext.objects_in_destroy << { id:, type: 'AnatomicalPart' } yield ensure OriginRelationshipDestroyContext.objects_in_destroy.delete({ id:, type: 'AnatomicalPart' }) end |
#taxonomic_origin_object ⇒ Object
The top object in the OriginRelationship chain.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'app/models/anatomical_part.rb', line 209 def taxonomic_origin_object return @top_origin_object if @top_origin_object.present? origin = inbound_origin_relationship return nil if origin.nil? # invalid object next_parent = nil while (next_parent = OriginRelationship.where(new_object_id: origin.old_object_id, new_object_type: origin.old_object_type).first) origin = next_parent end @top_origin_object = origin.old_object @top_origin_object end |
#top_origin_is_valid_origin ⇒ Object (private)
254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'app/models/anatomical_part.rb', line 254 def top_origin_is_valid_origin origin = taxonomic_origin_object errors.add(:base, 'Anatomical parts must be endpoints of an OriginRelationship!') && return if origin.nil? valid = valid_old_object_classes - ['AnatomicalPart'] t = origin.class.base_class.name errors.add(:base, "Class of original origin must be in [#{valid.join(', ')}], not #{t}") && return if !valid.include?(t) return if origin.class.name == 'Otu' errors.add(:base, "Original origin must have a taxon determination!") && return if !origin.taxon_determinations.exists? end |
#uuid_required ⇒ Object
108 109 110 |
# File 'app/models/anatomical_part.rb', line 108 def uuid_required is_material end |