Module: Relationships

Included in:
Accession, Classification, ClassificationTerm, Container, ContainerProfile, Event, Instance, Resource, SubContainer, Subject, TopContainer
Defined in:
backend/app/model/mixins/relationships.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) cached_relationships (readonly)

Store a list of the relationships that this object participates in. Saves looking up the DB for each one.



355
356
357
# File 'backend/app/model/mixins/relationships.rb', line 355

def cached_relationships
  @cached_relationships
end

Class Method Details

+ (Object) included(base)



330
331
332
333
334
335
336
337
# File 'backend/app/model/mixins/relationships.rb', line 330

def self.included(base)
  base.instance_eval do
    @relationships ||= {}
    @relationship_dependencies ||= {}
  end

  base.extend(ClassMethods)
end

Instance Method Details

- (Object) assimilate(victims)

Find all relationships involving the records in ‘victims’ and rewrite them to refer to us instead.



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'backend/app/model/mixins/relationships.rb', line 393

def assimilate(victims)
  victims = victims.reject {|v| (v.class == self.class) && (v.id == self.id)}

  self.class.relationship_dependencies.each do |relationship, models|
    models.each do |model|
      model.transfer(relationship, self, victims)
    end
  end

  DB.attempt {
    victims.each(&:delete)
  }.and_if_constraint_fails {
    raise MergeRequestFailed.new("Can't complete merge: record still in use")
  }

  trigger_reindex_of_dependants
end

- (Object) cache_relationships(relationship_defn, relationship_objects)



356
357
358
359
# File 'backend/app/model/mixins/relationships.rb', line 356

def cache_relationships(relationship_defn, relationship_objects)
  @cached_relationships ||= {}
  @cached_relationships[relationship_defn] = relationship_objects
end

- (Object) my_relationships(name)

Added to the mixed in class itself: return a list of the relationship instances involving this object



376
377
378
# File 'backend/app/model/mixins/relationships.rb', line 376

def my_relationships(name)
  self.class.find_relationship(name).find_by_participant(self)
end

Return all object instances that are related to the current record by the relationship named by ‘name’.



383
384
385
386
387
388
# File 'backend/app/model/mixins/relationships.rb', line 383

def related_records(name)
  relationship = self.class.find_relationship(name)
  records = relationship.who_participates_with(self)

  relationship.wants_array? ? records : records.first
end

- (Object) transfer_to_repository(repository, transfer_group = [])



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'backend/app/model/mixins/relationships.rb', line 412

def transfer_to_repository(repository, transfer_group = [])
  # When a record is being transferred to another repository, any
  # relationships it has to records within the current repository must be
  # cleared.

  predicate = proc {|relationship|
    referent = relationship.other_referent_than(self)

    # Delete the relationship if we're repository-scoped and the referent is
    # in the old repository.  Don't worry about relationships to any of the
    # records that are going to be transferred along with us (listed in
    # transfer_group)
    (referent.class.model_scope == :repository &&
     referent.repo_id != repository.id &&
     !transfer_group.any?{|obj| obj.id == referent.id && obj.model == referent.model})
  }


  ([self.class] + self.class.dependent_models).each do |model|
    model.delete_existing_relationships(self, false, false, predicate)
  end

  super
end

- (Object) trigger_reindex_of_dependants



362
363
364
365
366
367
368
369
370
371
# File 'backend/app/model/mixins/relationships.rb', line 362

def trigger_reindex_of_dependants
  # Update the mtime of any record with a relationship to this one.  This
  # encourages the indexer to reindex records when, say, a subject is renamed.
  #
  # Once we have our list of unique models, inform each of them that our
  # instance has been updated (using a class method defined below).
  self.class.dependent_models.each do |model|
    model.touch_mtime_of_anyone_related_to(self)
  end
end

- (Object) update_from_json(json, opts = {}, apply_nested_records = true)



340
341
342
343
344
345
346
347
348
349
350
# File 'backend/app/model/mixins/relationships.rb', line 340

def update_from_json(json, opts = {}, apply_nested_records = true)
  obj = super

  # Call this before and after the change since relationships might have been
  # removed and the previously linked objects might need reindexing.
  trigger_reindex_of_dependants
  self.class.apply_relationships(obj, json, opts)
  trigger_reindex_of_dependants

  obj
end