Class: OceanDynamo::Associations::Association
- Inherits:
-
Object
- Object
- OceanDynamo::Associations::Association
- 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
Instance Attribute Summary collapse
-
#owner ⇒ Object
readonly
:nodoc:.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
-
#target ⇒ Object
Returns the value of attribute target.
Instance Method Summary collapse
-
#initialize(owner, reflection) ⇒ Association
constructor
A new instance of Association.
-
#klass ⇒ Object
Returns the class of the target.
-
#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
. -
#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.
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
#owner ⇒ Object (readonly)
:nodoc:
27 28 29 |
# File 'lib/ocean-dynamo/associations/association.rb', line 27 def owner @owner end |
#reflection ⇒ Object (readonly)
Returns the value of attribute reflection.
28 29 30 |
# File 'lib/ocean-dynamo/associations/association.rb', line 28 def reflection @reflection end |
#target ⇒ Object
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
#klass ⇒ Object
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_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.
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?
52 53 54 |
# File 'lib/ocean-dynamo/associations/association.rb', line 52 def loaded? @loaded end |
#reload ⇒ Object
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 |
#reset ⇒ Object
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.
80 81 82 |
# File 'lib/ocean-dynamo/associations/association.rb', line 80 def stale_target? loaded? && @stale_state != stale_state end |