Class: OceanDynamo::Associations::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/ocean-dynamo/associations/association.rb

Overview

This is the root class of all Associations. The class structure is exactly like in ActiveRecord:

Associations
  Association
    CollectionAssociation
      HasAndBelongsToManyAssociation
      HasManyAssociation

It should be noted, however, that the ActiveRecord documentation

is misleading: belongs_to and has_one no longer are implemented using proxies, even though the documentation and the source itself says it is. Furthermore, method_missing is no longer used at all, despite what the documentation and source comments say.

In OceanDynamo, we have removed the unused classes and stripped away the SQL-specific features such as scopes. Neither do we implement counter caches. We have kept the same module and class structure for compatibility, though.

Direct Known Subclasses

CollectionAssociation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, reflection) ⇒ Association

Returns a new instance of Association.



35
36
37
38
# File 'lib/ocean-dynamo/associations/association.rb', line 35

def initialize(owner, reflection)
  @owner, @reflection = owner, reflection
  reset
end

Instance Attribute Details

#ownerObject (readonly)

:nodoc:



27
28
29
# File 'lib/ocean-dynamo/associations/association.rb', line 27

def owner
  @owner
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



28
29
30
# File 'lib/ocean-dynamo/associations/association.rb', line 28

def reflection
  @reflection
end

#targetObject

Returns the value of attribute target.



29
30
31
# File 'lib/ocean-dynamo/associations/association.rb', line 29

def target
  @target
end

Instance Method Details

#klassObject

Returns the class of the target. belongs_to polymorphic used to override this to look at the polymorphic_type field on the owner. However, as belongs_to is no longer implemented in AR using an Assocation, we keep this only for structural compatibility.



110
111
112
# File 'lib/ocean-dynamo/associations/association.rb', line 110

def klass
  reflection.klass
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.

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



96
97
98
99
100
101
102
# File 'lib/ocean-dynamo/associations/association.rb', line 96

def load_target
  @target = find_target if (@stale_state && stale_target?) || find_target?
  loaded! unless loaded?
  target
rescue OceanDynamo::RecordNotFound
  reset
end

#loaded!Object

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



59
60
61
62
# File 'lib/ocean-dynamo/associations/association.rb', line 59

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

#loaded?Boolean

Has the target been already loaded?

Returns:

  • (Boolean)


52
53
54
# File 'lib/ocean-dynamo/associations/association.rb', line 52

def loaded?
  @loaded
end

#reloadObject

Reloads the target and returns self on success.



117
118
119
120
121
# File 'lib/ocean-dynamo/associations/association.rb', line 117

def reload
  reset
  load_target
  self unless target.nil?
end

#resetObject

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



43
44
45
46
47
# File 'lib/ocean-dynamo/associations/association.rb', line 43

def reset
  @loaded = false
  @target = nil
  @stale_state = nil
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 stale_state method if relevant.

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

Returns:

  • (Boolean)


80
81
82
# File 'lib/ocean-dynamo/associations/association.rb', line 80

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