Module: Shared::OriginRelationship

Extended by:
ActiveSupport::Concern
Included in:
CollectionObject, Extract, Sequence
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) has_many :derived_<foo> associations are created for each is_origin_for()

  !! 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

- (Objects) new_objects

Returns an array of instances

Returns:

  • (Objects)

    an array of instances



70
71
72
# File 'app/models/concerns/shared/origin_relationship.rb', line 70

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

- (Objects) old_objects

Returns an array of instances, the source of this object

Returns:

  • (Objects)

    an array of instances, the source of this object



64
65
66
# File 'app/models/concerns/shared/origin_relationship.rb', line 64

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

- (Object) reject_origin_relationships(attributes) (private)



76
77
78
79
80
81
82
83
# File 'app/models/concerns/shared/origin_relationship.rb', line 76

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

- (Object) set_origin



31
32
33
34
35
# File 'app/models/concerns/shared/origin_relationship.rb', line 31

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