Module: Shared::OriginRelationship

Extended by:
ActiveSupport::Concern
Included in:
AssertedDistribution, CollectionObject, Extract, FieldOccurrence, ImportDataset, Otu, Person, Sequence, Source::Bibtex, Source::Verbatim
Defined in:
app/models/concerns/shared/origin_relationship.rb

Overview

Shared code for extending data classes with an OriginRelationship

How to use this concern:
  1) In BOTH related models, Include this concern (`include Shared::OriginRelationship`)
  2) In the "old" model call `is_origin_for` with valid class names, as strings, e.g.:
     `is_origin_for 'CollectionObject', 'CollectionObject::BiologicalCollectionObject'`
  3) In the "old" model call `originates_from` if required.
  4) Repeat assertions in the "new" model.

  5) `has_many :derived_<foo> associations are created for each `is_origin_for()`
  6) `has_many :origin_<foo> associtations are created for each `originates_from()`

  !! You must redundantly provide STI subclasses and parent classes if you want to allow both.  Providing
     a superclass does *not* provide the subclasses.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#new_objectsObjects

Returns an array of instances.

Returns:

  • (Objects)

    an array of instances



99
100
101
# File 'app/models/concerns/shared/origin_relationship.rb', line 99

def new_objects
  origin_relationships.collect{|a| a.new_object}
end

#old_objectsObjects

Returns an array of instances, the source of this object.

Returns:

  • (Objects)

    an array of instances, the source of this object



93
94
95
# File 'app/models/concerns/shared/origin_relationship.rb', line 93

def old_objects
  related_origin_relationships.collect{|a| a.old_object}
end

#reject_origin_relationships(attributes) ⇒ Object (private)



105
106
107
108
109
110
111
112
# File 'app/models/concerns/shared/origin_relationship.rb', line 105

def reject_origin_relationships(attributes)
  o = attributes['new_object']
  if !defined? valid_new_object_classes
    raise NoMethodError.new("#{self.class.name} missing module 'Shared::OriginRelationship' or \"is_origin_for()\" is not being called")
  end

  o.blank? || !valid_new_object_classes.include?(o.class.name)
end

#set_originObject



37
38
39
40
41
# File 'app/models/concerns/shared/origin_relationship.rb', line 37

def set_origin
  [origin].flatten.each do |object|
    related_origin_relationships.build(old_object: object)
  end
end