Class: OriginRelationship

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::IsData
Defined in:
app/models/origin_relationship.rb

Overview

An OriginRelationship asserts that one object is derived_from another.

The old object is the source_of or origin_of the new object. The new object originates_from the old_object.

Currently these combinations are planned (* not implemented):

old_object / new_object

  • field_observation / collection_object

collection_object / collection_object collection_object / extract

  • collection_object / part_of (anatomy)

  • part_of / part_of

  • part_of / extract

extract / extract extract / sequence sequence / sequence

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#new_object_idInteger

Returns id of the new (original) object.

Returns:

  • (Integer)

    id of the new (original) object


44
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
# File 'app/models/origin_relationship.rb', line 44

class OriginRelationship < ApplicationRecord
  include Housekeeping
  include Shared::IsData

  acts_as_list scope: [:project_id, :old_object_id, :old_object_type]

  belongs_to :old_object, polymorphic: true
  belongs_to :new_object, polymorphic: true

  # Don't validate presence of old_object or new_object
  # so that nested attributes can work in such a way that
  # old_object and new_object don't have to be saved first
  validate :valid_source_target_pairs

  def valid_source_target_pairs
    if old_object_type.nil? || new_object_type.nil?
      errors.add(:old_object, "can't be nil!") if old_object_type.nil?
      errors.add(:new_object, "can't be nil!") if new_object_type.nil?
      return
    end

    old_object_type_class = old_object_type.constantize

    if !old_object_type_class.respond_to?(:valid_new_object_classes)
      errors.add(:old_object, "#{old_object_type} is not a valid origin relationship old object")
    elsif !old_object_type_class.valid_new_object_classes.include?(new_object_type)
      errors.add(:new_object, "#{new_object_type} is not a valid origin relationship new object for old object #{old_object_type}")
    end
  end

end

#new_object_typeInteger

Returns type of the new (original) object.

Returns:

  • (Integer)

    type of the new (original) object


44
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
# File 'app/models/origin_relationship.rb', line 44

class OriginRelationship < ApplicationRecord
  include Housekeeping
  include Shared::IsData

  acts_as_list scope: [:project_id, :old_object_id, :old_object_type]

  belongs_to :old_object, polymorphic: true
  belongs_to :new_object, polymorphic: true

  # Don't validate presence of old_object or new_object
  # so that nested attributes can work in such a way that
  # old_object and new_object don't have to be saved first
  validate :valid_source_target_pairs

  def valid_source_target_pairs
    if old_object_type.nil? || new_object_type.nil?
      errors.add(:old_object, "can't be nil!") if old_object_type.nil?
      errors.add(:new_object, "can't be nil!") if new_object_type.nil?
      return
    end

    old_object_type_class = old_object_type.constantize

    if !old_object_type_class.respond_to?(:valid_new_object_classes)
      errors.add(:old_object, "#{old_object_type} is not a valid origin relationship old object")
    elsif !old_object_type_class.valid_new_object_classes.include?(new_object_type)
      errors.add(:new_object, "#{new_object_type} is not a valid origin relationship new object for old object #{old_object_type}")
    end
  end

end

#old_object_idInteger

Returns id of the old (original) object.

Returns:

  • (Integer)

    id of the old (original) object


44
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
# File 'app/models/origin_relationship.rb', line 44

class OriginRelationship < ApplicationRecord
  include Housekeeping
  include Shared::IsData

  acts_as_list scope: [:project_id, :old_object_id, :old_object_type]

  belongs_to :old_object, polymorphic: true
  belongs_to :new_object, polymorphic: true

  # Don't validate presence of old_object or new_object
  # so that nested attributes can work in such a way that
  # old_object and new_object don't have to be saved first
  validate :valid_source_target_pairs

  def valid_source_target_pairs
    if old_object_type.nil? || new_object_type.nil?
      errors.add(:old_object, "can't be nil!") if old_object_type.nil?
      errors.add(:new_object, "can't be nil!") if new_object_type.nil?
      return
    end

    old_object_type_class = old_object_type.constantize

    if !old_object_type_class.respond_to?(:valid_new_object_classes)
      errors.add(:old_object, "#{old_object_type} is not a valid origin relationship old object")
    elsif !old_object_type_class.valid_new_object_classes.include?(new_object_type)
      errors.add(:new_object, "#{new_object_type} is not a valid origin relationship new object for old object #{old_object_type}")
    end
  end

end

#old_object_typeInteger

Returns type of the old (original) object.

Returns:

  • (Integer)

    type of the old (original) object


44
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
# File 'app/models/origin_relationship.rb', line 44

class OriginRelationship < ApplicationRecord
  include Housekeeping
  include Shared::IsData

  acts_as_list scope: [:project_id, :old_object_id, :old_object_type]

  belongs_to :old_object, polymorphic: true
  belongs_to :new_object, polymorphic: true

  # Don't validate presence of old_object or new_object
  # so that nested attributes can work in such a way that
  # old_object and new_object don't have to be saved first
  validate :valid_source_target_pairs

  def valid_source_target_pairs
    if old_object_type.nil? || new_object_type.nil?
      errors.add(:old_object, "can't be nil!") if old_object_type.nil?
      errors.add(:new_object, "can't be nil!") if new_object_type.nil?
      return
    end

    old_object_type_class = old_object_type.constantize

    if !old_object_type_class.respond_to?(:valid_new_object_classes)
      errors.add(:old_object, "#{old_object_type} is not a valid origin relationship old object")
    elsif !old_object_type_class.valid_new_object_classes.include?(new_object_type)
      errors.add(:new_object, "#{new_object_type} is not a valid origin relationship new object for old object #{old_object_type}")
    end
  end

end

#positionInteger

Returns order relative to old object.

Returns:

  • (Integer)

    order relative to old object


44
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
# File 'app/models/origin_relationship.rb', line 44

class OriginRelationship < ApplicationRecord
  include Housekeeping
  include Shared::IsData

  acts_as_list scope: [:project_id, :old_object_id, :old_object_type]

  belongs_to :old_object, polymorphic: true
  belongs_to :new_object, polymorphic: true

  # Don't validate presence of old_object or new_object
  # so that nested attributes can work in such a way that
  # old_object and new_object don't have to be saved first
  validate :valid_source_target_pairs

  def valid_source_target_pairs
    if old_object_type.nil? || new_object_type.nil?
      errors.add(:old_object, "can't be nil!") if old_object_type.nil?
      errors.add(:new_object, "can't be nil!") if new_object_type.nil?
      return
    end

    old_object_type_class = old_object_type.constantize

    if !old_object_type_class.respond_to?(:valid_new_object_classes)
      errors.add(:old_object, "#{old_object_type} is not a valid origin relationship old object")
    elsif !old_object_type_class.valid_new_object_classes.include?(new_object_type)
      errors.add(:new_object, "#{new_object_type} is not a valid origin relationship new object for old object #{old_object_type}")
    end
  end

end

#project_idInteger

Returns the project ID.

Returns:

  • (Integer)

    the project ID


44
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
# File 'app/models/origin_relationship.rb', line 44

class OriginRelationship < ApplicationRecord
  include Housekeeping
  include Shared::IsData

  acts_as_list scope: [:project_id, :old_object_id, :old_object_type]

  belongs_to :old_object, polymorphic: true
  belongs_to :new_object, polymorphic: true

  # Don't validate presence of old_object or new_object
  # so that nested attributes can work in such a way that
  # old_object and new_object don't have to be saved first
  validate :valid_source_target_pairs

  def valid_source_target_pairs
    if old_object_type.nil? || new_object_type.nil?
      errors.add(:old_object, "can't be nil!") if old_object_type.nil?
      errors.add(:new_object, "can't be nil!") if new_object_type.nil?
      return
    end

    old_object_type_class = old_object_type.constantize

    if !old_object_type_class.respond_to?(:valid_new_object_classes)
      errors.add(:old_object, "#{old_object_type} is not a valid origin relationship old object")
    elsif !old_object_type_class.valid_new_object_classes.include?(new_object_type)
      errors.add(:new_object, "#{new_object_type} is not a valid origin relationship new object for old object #{old_object_type}")
    end
  end

end

Instance Method Details

#valid_source_target_pairsObject


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/origin_relationship.rb', line 58

def valid_source_target_pairs
  if old_object_type.nil? || new_object_type.nil?
    errors.add(:old_object, "can't be nil!") if old_object_type.nil?
    errors.add(:new_object, "can't be nil!") if new_object_type.nil?
    return
  end

  old_object_type_class = old_object_type.constantize

  if !old_object_type_class.respond_to?(:valid_new_object_classes)
    errors.add(:old_object, "#{old_object_type} is not a valid origin relationship old object")
  elsif !old_object_type_class.valid_new_object_classes.include?(new_object_type)
    errors.add(:new_object, "#{new_object_type} is not a valid origin relationship new object for old object #{old_object_type}")
  end
end