Class: DatastaxRails::Associations::Association
- Defined in:
- lib/datastax_rails/associations/association.rb
Overview
DatastaxRails Associations
This is the root class of all associations (‘+ Foo’ signifies an included module Foo):
Association
SingularAssociation
HasOneAssociation
HasOneThroughAssociation + ThroughAssociation (Not implemented)
BelongsToAssociation
BelongsToPolymorphicAssociation (Not implemented)
CollectionAssociation
HasAndBelongsToManyAssociation (Not implemented)
HasManyAssociation
HasManyThroughAssociation + ThroughAssociation (Not implemented)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#loaded ⇒ Object
(also: #loaded?)
readonly
:nodoc:.
-
#owner ⇒ Object
readonly
:nodoc:.
-
#reflection ⇒ Object
readonly
:nodoc:.
-
#target ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#aliased_column_family ⇒ Object
Returns the name of the column family name of the related class:.
-
#association_scope ⇒ Object
The scope for this association.
-
#initialize(owner, reflection) ⇒ Association
constructor
A new instance of Association.
-
#klass ⇒ Object
This 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
. -
#reload ⇒ Object
Reloads the target and returns
self
on success. -
#reset ⇒ Object
Resets the loaded flag to
false
and sets the target tonil
. - #reset_scope ⇒ Object
- #scoped ⇒ 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.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/datastax_rails/associations/association.rb', line 26 def initialize(owner, reflection) reflection.check_validity! @target = nil @owner, @reflection = owner, reflection @updated = false reset reset_scope end |
Instance Attribute Details
#loaded ⇒ Object (readonly) Also known as: loaded?
:nodoc:
21 22 23 |
# File 'lib/datastax_rails/associations/association.rb', line 21 def loaded @loaded end |
#owner ⇒ Object (readonly)
:nodoc:
21 22 23 |
# File 'lib/datastax_rails/associations/association.rb', line 21 def owner @owner end |
#reflection ⇒ Object (readonly)
:nodoc:
21 22 23 |
# File 'lib/datastax_rails/associations/association.rb', line 21 def reflection @reflection end |
#target ⇒ Object
:nodoc:
21 22 23 |
# File 'lib/datastax_rails/associations/association.rb', line 21 def target @target end |
Instance Method Details
#aliased_column_family ⇒ Object
Returns the name of the column family name of the related class:
post.comments.aliased_column_family # => "comments"
41 42 43 |
# File 'lib/datastax_rails/associations/association.rb', line 41 def aliased_column_family reflection.klass.column_family end |
#association_scope ⇒ Object
The scope for this association.
Note that the association_scope is merged into the target_scope only when the scoped 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.
91 92 93 |
# File 'lib/datastax_rails/associations/association.rb', line 91 def association_scope @association_scope ||= AssociationScope.new(self).scope if klass end |
#klass ⇒ Object
This class of the target. belongs_to polymorphic overrides this to look at the polymorphic_type field on the owner.
108 109 110 |
# File 'lib/datastax_rails/associations/association.rb', line 108 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.
DatastaxRails::RecordNotFound is rescued within the method, and it is not reraised. The proxy is reset and nil
is the return value.
128 129 130 131 132 133 134 |
# File 'lib/datastax_rails/associations/association.rb', line 128 def load_target @target ||= find_target if find_target? loaded! unless loaded? target rescue DatastaxRails::RecordNotFound reset end |
#loaded! ⇒ Object
Asserts the target has been loaded setting the loaded flag to true
.
60 61 62 63 |
# File 'lib/datastax_rails/associations/association.rb', line 60 def loaded! @loaded = true @stale_state = stale_state end |
#reload ⇒ Object
Reloads the target and returns self
on success.
52 53 54 55 56 57 |
# File 'lib/datastax_rails/associations/association.rb', line 52 def reload reset reset_scope load_target self unless target.nil? end |
#reset ⇒ Object
Resets the loaded flag to false
and sets the target to nil
.
46 47 48 49 |
# File 'lib/datastax_rails/associations/association.rb', line 46 def reset @loaded = false @target = nil end |
#reset_scope ⇒ Object
95 96 97 |
# File 'lib/datastax_rails/associations/association.rb', line 95 def reset_scope @association_scope = nil end |
#scoped ⇒ Object
81 82 83 |
# File 'lib/datastax_rails/associations/association.rb', line 81 def scoped target_scope.merge(association_scope) end |
#set_inverse_instance(record) ⇒ Object
Set the inverse association, if possible
100 101 102 103 104 |
# File 'lib/datastax_rails/associations/association.rb', line 100 def set_inverse_instance(record) # rubocop:disable Style/AccessorMethodName return unless record && invertible_for?(record) inverse = record.association(inverse_reflection_for(record).name) inverse.target = owner 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.
71 72 73 |
# File 'lib/datastax_rails/associations/association.rb', line 71 def stale_target? 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)
114 115 116 |
# File 'lib/datastax_rails/associations/association.rb', line 114 def target_scope klass.scoped end |