Class: ActiveFedora::Associations::IndirectlyContainsAssociation

Inherits:
ContainsAssociation show all
Defined in:
lib/active_fedora/associations/indirectly_contains_association.rb

Overview

TODO: we may want to split this into two subclasses, one for has_member_relation and the other for is_member_of_relation

Instance Attribute Summary

Attributes inherited from CollectionAssociation

#proxy

Attributes inherited from Association

#inversed, #owner, #reflection, #target

Instance Method Summary collapse

Methods inherited from ContainsAssociation

#include?, #reader

Methods inherited from CollectionAssociation

#add_to_target, #any?, #build, #concat_records, #count, #create, #create!, #delete, #delete_all, #destroy, #destroy_all, #empty?, #find, #first, #ids_writer, #include?, #last, #load_from_solr, #load_target, #null_scope?, #reader, #replace, #reset, #scope, #select, #size, #target=, #to_ary, #writer

Methods inherited from Association

#association_scope, #initialize, #load_target, #loaded!, #loaded?, #reload, #reset, #reset_scope, #scope, #set_inverse_instance, #stale_target?, #target_scope

Constructor Details

This class inherits a constructor from ActiveFedora::Associations::Association

Instance Method Details

#concat(*records) ⇒ Object

Add records to this association. Returns self so method calls may be chained. Since << flattens its argument list and inserts each record, push and concat behave identically.



8
9
10
# File 'lib/active_fedora/associations/indirectly_contains_association.rb', line 8

def concat(*records)
  concat_records(records)
end

#containerObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/active_fedora/associations/indirectly_contains_association.rb', line 49

def container
  @container ||= begin
    IndirectContainer.find_or_initialize(ActiveFedora::Base.uri_to_id(uri)).tap do |container|
      container.parent = @owner
      container.has_member_relation = Array(options[:has_member_relation])
      container.is_member_of_relation = Array(options[:is_member_of_relation])
      container.inserted_content_relation = Array(options[:inserted_content_relation])
    end
  end
end

#find_targetObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_fedora/associations/indirectly_contains_association.rb', line 37

def find_target
  if container_predicate = options[:has_member_relation]
    uris = owner.resource.query(predicate: container_predicate).map { |r| r.object.to_s }
    uris.map { |object_uri| klass.find(klass.uri_to_id(object_uri)) }
  else # is_member_of_relation
    # TODO this is a lot of reads. Avoid this path
    container_predicate = ::RDF::Vocab::LDP.contains
    proxy_uris = container.resource.query(predicate: container_predicate).map { |r| r.object.to_s }
    proxy_uris.map { |uri| proxy_class.find(proxy_class.uri_to_id(uri))[options[:foreign_key]] }
  end
end

#ids_readerObject

Implements the ids reader method, e.g. foo.item_ids for Foo.indirectly_contains :items, …



27
28
29
30
31
32
33
34
35
# File 'lib/active_fedora/associations/indirectly_contains_association.rb', line 27

def ids_reader
  predicate = reflection.options.fetch(:has_member_relation)
  if loaded?
    target.map(&:id)
  else
    owner.resource.query(predicate: predicate)
         .map { |s| ActiveFedora::Base.uri_to_id(s.object) } | target.map(&:id)
  end
end

#insert_record(record, force = true, validate = true) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/active_fedora/associations/indirectly_contains_association.rb', line 12

def insert_record(record, force = true, validate = true)
  container.save!
  if force
    record.save!
  else
    return false unless record.save(validate: validate)
  end

  save_through_record(record)

  true
end