Class: ActiveFedora::Associations::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fedora/associations/association.rb

Overview

This is the root class of all associations:

Association
  BelongsToAssociation
  AssociationCollection
    HasManyAssociation

Direct Known Subclasses

CollectionAssociation, SingularAssociation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, reflection) ⇒ Association

Returns a new instance of Association.



15
16
17
18
19
20
21
# File 'lib/active_fedora/associations/association.rb', line 15

def initialize(owner, reflection)
  #reflection.check_validity!
  @owner, @reflection = owner, reflection
  @updated = false
  reset
  # construct_scope
end

Instance Attribute Details

#ownerObject (readonly)

Returns the value of attribute owner.



12
13
14
# File 'lib/active_fedora/associations/association.rb', line 12

def owner
  @owner
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



12
13
14
# File 'lib/active_fedora/associations/association.rb', line 12

def reflection
  @reflection
end

#targetObject

Returns the value of attribute target.



12
13
14
# File 'lib/active_fedora/associations/association.rb', line 12

def target
  @target
end

Instance Method Details

#association_scopeObject

The scope for this association.

Note that the association_scope is merged into the target_scope only when the scope method is called. This is because at that point the call may be surrounded by scope.scoping { … } or with_scope { … } etc, which affects the scope which actually gets built.



74
75
76
77
78
# File 'lib/active_fedora/associations/association.rb', line 74

def association_scope
  if klass
    @association_scope ||= AssociationScope.new(self).scope
  end
end

#load_targetObject

Loads the target if needed and returns it.

This method is abstract in the sense that it relies on find_target, which is expected to be provided by descendants.

If the target is already loaded it is just returned. Thus, you can call load_target unconditionally to get the target.

ActiveFedora::ObjectNotFoundError is rescued within the method, and it is not reraised. The proxy is reset and nil is the return value.



105
106
107
108
109
# File 'lib/active_fedora/associations/association.rb', line 105

def load_target
  @target = find_target if (@stale_state && stale_target?) || find_target?
  loaded! unless loaded?
  target
end

#loaded!Object

Asserts the target has been loaded setting the loaded flag to true.



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

def loaded!
  @loaded = true
  @stale_state = stale_state
end

#loaded?Boolean

Has the target been already loaded?

Returns:

  • (Boolean)


38
39
40
# File 'lib/active_fedora/associations/association.rb', line 38

def loaded?
  @loaded
end

#reloadObject

Reloads the target and returns self on success.



30
31
32
33
34
35
# File 'lib/active_fedora/associations/association.rb', line 30

def reload
  reset
  # construct_scope
  load_target
  self unless @target.nil?
end

#resetObject

Resets the loaded flag to false and sets the target to nil.



24
25
26
27
# File 'lib/active_fedora/associations/association.rb', line 24

def reset
  @loaded = false
  @target = nil
end

#scopeObject



64
65
66
# File 'lib/active_fedora/associations/association.rb', line 64

def scope
  target_scope.merge(association_scope)
end

#set_inverse_instance(record) ⇒ Object

Set the inverse association, if possible



81
82
83
84
85
86
# File 'lib/active_fedora/associations/association.rb', line 81

def set_inverse_instance(record)
  if record && invertible_for?(record)
    inverse = record.association(inverse_reflection_for(record).name)
    inverse.target = owner
  end
end

#stale_target?Boolean

The target is stale if the target no longer points to the record(s) that the relevant foreign_key(s) refers to. If stale, the association accessor method on the owner will reload the target. It’s up to subclasses to implement the state_state method if relevant.

Note that if the target has not been loaded, it is not considered stale.

Returns:

  • (Boolean)


54
55
56
# File 'lib/active_fedora/associations/association.rb', line 54

def stale_target?
  loaded? && @stale_state != stale_state
end

#target_scopeObject

Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the through association’s scope)



91
92
93
# File 'lib/active_fedora/associations/association.rb', line 91

def target_scope
  klass.all
end