Module: Shared::PolymorphicAnnotator

Extended by:
ActiveSupport::Concern
Included in:
AlternateValue, Attribution, Citation, Confidence, DataAttribute, Depiction, Documentation, Identifier, Label, Note, OriginRelationship, ProtocolRelationship, Role, Tag
Defined in:
app/models/concerns/shared/polymorphic_annotator.rb

Overview

Helper methods for polymorphic annotators. Extends annotators so that global_id strings can be used as attributes referencing the polymorphic object.

To implement:

include Shared::PolymorphicAnnotator
polymorphic_annotates('belongs_to_name', 'foreign_key')

The foreign_key argument is optional, and only necessary when it can’t be derived from the belongs_to_name.

Implementing concerns, for example Shared::Taggable, should push foreign keys, like:

Tag.related_foreign_keys.push self.name.foreign_key

Instance Method Summary collapse

Instance Method Details

#annotated_object_is_persisted?Boolean (private)

Since we can’t FK on polymorphics we use this to test that the annotated_object actually is present in the database.

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/concerns/shared/polymorphic_annotator.rb', line 118

def annotated_object_is_persisted?
  if annotated_object
    # We can't test `persisted?` since destroyed in-memory objects will
    # be interfered with.
    if !annotated_object.id.nil?
      begin
        # !! Do not use annotate_object.reload as it resets
        # the object in memory. !!
        annotated_object.class.find(annotated_object.id)
      rescue ActiveRecord::RecordNotFound
        errors.add(:base, 'annotated object no longer exists')
      end
    end
  end
end