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



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

def id_reader
  # 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(ActiveFedora::SolrService.construct_query_for_pids(ids))
  results.each do |result|
    return result['id'] if SolrService.class_from_solr_document(result) == klass
  end
  return nil
end

#id_writer(id) ⇒ Object



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

def id_writer(id)
  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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_fedora/associations/belongs_to_association.rb', line 27

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



43
44
45
46
# File 'lib/active_fedora/associations/belongs_to_association.rb', line 43

def reset
  super
  @updated = false
end

#updated?Boolean

Returns:

  • (Boolean)


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

def updated?
  @updated
end