Class: ActiveFedora::Associations::BelongsToAssociation

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

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from Association

#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, #scope, #set_inverse_instance, #stale_target?, #target_scope

Constructor Details

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

Instance Method Details

#id_readerObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_fedora/associations/belongs_to_association.rb', line 12

def id_reader
  @full_result ||= begin
    # need to find the id with the correct class
    ids = @owner.ids_for_outbound(@reflection.options[:property])

    return if ids.empty? 
    # This incurs a lot of overhead, but it's necessary if the users use one property for more than one association.
    # e.g.
    #   belongs_to :author, property: :has_member, class_name: 'Person'
    #   belongs_to :publisher, property: :has_member

    results = SolrService.query(construct_query(ids))
    if results.present?
      results.first
    end
  end
  @full_result['id'] if @full_result
end

#id_writer(id) ⇒ Object



5
6
7
8
9
10
# File 'lib/active_fedora/associations/belongs_to_association.rb', line 5

def id_writer(id)
  @full_result = nil
  remove_matching_property_relationship
  return if id.blank? or id == ActiveFedora::UnsavedDigitalObject::PLACEHOLDER
  @owner.add_relationship(@reflection.options[:property], ActiveFedora::Base.internal_uri(id))
end

#replace(record) ⇒ Object



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

def replace(record)
  if record.nil?
    id_writer(nil)
  else
    raise_on_type_mismatch(record)
    id_writer(record.id)
    @updated = true

    #= (AssociationProxy === record ? record.target : record)
  end
  self.target = record

  loaded!
  record
end

#resetObject



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

def reset
  super
  @updated = false
end

#updated?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/active_fedora/associations/belongs_to_association.rb', line 52

def updated?
  @updated
end