Class: ActiveFedora::Associations::DirectlyContainsOneAssociation

Inherits:
SingularAssociation show all
Defined in:
lib/active_fedora/associations/directly_contains_one_association.rb

Overview

Filters a DirectContainer relationship, returning the first item that matches the given :type

Instance Attribute Summary

Attributes inherited from Association

#inversed, #owner, #reflection, #target

Instance Method Summary collapse

Methods inherited from SingularAssociation

#build, #create, #create!, #reader, #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

#add_to_container(record) ⇒ Object

Adds record to the DirectContainer identified by the container_association Relies on container_association.initialize_attributes to appropriately set things like record.uri



26
27
28
29
30
# File 'lib/active_fedora/associations/directly_contains_one_association.rb', line 26

def add_to_container(record)
  container_association.add_to_target(record) # adds record to corresponding Container
  # TODO is send necessary?
  container_association.send(:initialize_attributes, record) # Uses the :through association initialize the record with things like the correct URI for a direclty contained object
end

#find_targetObject

Finds objects contained by the container predicate (either the configured has_member_relation or ldp:contains) TODO: Refactor this to use solr (for efficiency) instead of parsing the RDF graph. Requires indexing ActiveFedora::File objects into solr, including their RDF.type and, if possible, the id of their container



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_fedora/associations/directly_contains_one_association.rb', line 7

def find_target
  # filtered_objects = container_association_proxy.to_a.select { |o| o.metadata_node.type.include?(options[:type]) }
  query_node = if container_predicate = options[:has_member_relation]
                 owner
               else
                 container_predicate = ::RDF::Vocab::LDP.contains
                 container_association.container # Use the :through association's container
               end

  contained_uris = query_node.resource.query(predicate: container_predicate).map { |r| r.object.to_s }
  contained_uris.each do |object_uri|
    contained_object = klass.find(klass.uri_to_id(object_uri))
    return contained_object if get_type_from_record(contained_object).include?(options[:type])
  end
  nil # if nothing was found & returned while iterating on contained_uris, return nil
end

#replace(record) ⇒ Object

Replaces association target with record Ensures that this association’s type is set on the record and adds the record to the association’s DirectContainer



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_fedora/associations/directly_contains_one_association.rb', line 34

def replace(record, *)
  if record
    raise_on_type_mismatch!(record)
    remove_existing_target
    add_type_to_record(record, options[:type])
    add_to_container(record)
  else
    remove_existing_target
  end

  self.target = record
end

#updated?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/active_fedora/associations/directly_contains_one_association.rb', line 47

def updated?
  @updated
end