Class: ActiveFedora::Associations::Association
- Inherits:
-
Object
- Object
- ActiveFedora::Associations::Association
- Defined in:
- lib/active_fedora/associations/association.rb
Overview
This is the root class of all associations:
Association
BelongsToAssociation
AssociationCollection
HasManyAssociation
Direct Known Subclasses
Builder::Property, CollectionAssociation, SingularAssociation
Instance Attribute Summary collapse
-
#inversed ⇒ Object
Returns the value of attribute inversed.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
-
#target ⇒ Object
Returns the value of attribute target.
Instance Method Summary collapse
-
#association_scope ⇒ Object
The scope for this association.
-
#initialize(owner, reflection) ⇒ Association
constructor
A new instance of Association.
-
#initialize_attributes(record) ⇒ Object
:nodoc:.
-
#load_target ⇒ Object
Loads the target if needed and returns it.
-
#loaded! ⇒ Object
Asserts the target has been loaded setting the loaded flag to
true
. -
#loaded? ⇒ Boolean
Has the target been already loaded?.
-
#reload ⇒ Object
Reloads the target and returns
self
on success. -
#reset ⇒ Object
Resets the loaded flag to
false
and sets the target tonil
. - #scope ⇒ Object
-
#set_inverse_instance(record) ⇒ Object
Set the inverse association, if possible.
-
#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.
-
#target_scope ⇒ Object
Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the through association’s scope).
Constructor Details
#initialize(owner, reflection) ⇒ Association
Returns a new instance of Association.
16 17 18 19 20 21 22 |
# File 'lib/active_fedora/associations/association.rb', line 16 def initialize(owner, reflection) reflection.check_validity! @owner = owner @reflection = reflection reset # construct_scope end |
Instance Attribute Details
#inversed ⇒ Object
Returns the value of attribute inversed.
13 14 15 |
# File 'lib/active_fedora/associations/association.rb', line 13 def inversed @inversed end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
12 13 14 |
# File 'lib/active_fedora/associations/association.rb', line 12 def owner @owner end |
#reflection ⇒ Object (readonly)
Returns the value of attribute reflection.
12 13 14 |
# File 'lib/active_fedora/associations/association.rb', line 12 def reflection @reflection end |
#target ⇒ Object
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_scope ⇒ Object
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.
78 79 80 |
# File 'lib/active_fedora/associations/association.rb', line 78 def association_scope @association_scope ||= AssociationScope.new(self).scope if klass end |
#initialize_attributes(record) ⇒ Object
:nodoc:
116 117 118 119 120 121 |
# File 'lib/active_fedora/associations/association.rb', line 116 def initialize_attributes(record) #:nodoc: skip_assign = [reflection.foreign_key].compact attributes = create_scope.except(*(record.changed - skip_assign)) record.attributes = attributes set_inverse_instance(record) end |
#load_target ⇒ Object
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.
110 111 112 113 114 |
# File 'lib/active_fedora/associations/association.rb', line 110 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
.
46 47 48 49 50 |
# File 'lib/active_fedora/associations/association.rb', line 46 def loaded! @loaded = true @stale_state = stale_state @inversed = false end |
#loaded? ⇒ Boolean
Has the target been already loaded?
41 42 43 |
# File 'lib/active_fedora/associations/association.rb', line 41 def loaded? @loaded end |
#reload ⇒ Object
Reloads the target and returns self
on success.
33 34 35 36 37 38 |
# File 'lib/active_fedora/associations/association.rb', line 33 def reload reset # construct_scope load_target self unless @target.nil? end |
#reset ⇒ Object
Resets the loaded flag to false
and sets the target to nil
.
25 26 27 28 29 30 |
# File 'lib/active_fedora/associations/association.rb', line 25 def reset @loaded = false @target = nil @stale_state = nil @inversed = false end |
#scope ⇒ Object
68 69 70 |
# File 'lib/active_fedora/associations/association.rb', line 68 def scope target_scope.merge(association_scope) end |
#set_inverse_instance(record) ⇒ Object
Set the inverse association, if possible
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/active_fedora/associations/association.rb', line 83 def set_inverse_instance(record) return unless record && invertible_for?(record) inverse = record.association(inverse_reflection_for(record).name.to_sym) if inverse.is_a? ActiveFedora::Associations::HasAndBelongsToManyAssociation inverse.target << owner else inverse.target = owner end inverse.inversed = true 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.
58 59 60 |
# File 'lib/active_fedora/associations/association.rb', line 58 def stale_target? !inversed && loaded? && @stale_state != stale_state end |
#target_scope ⇒ Object
Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the through association’s scope)
96 97 98 |
# File 'lib/active_fedora/associations/association.rb', line 96 def target_scope klass.all end |