Class: DatastaxRails::Associations::Association

Inherits:
Object
  • Object
show all
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

CollectionAssociation, SingularAssociation

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#loadedObject (readonly) Also known as: loaded?

:nodoc:



21
22
23
# File 'lib/datastax_rails/associations/association.rb', line 21

def loaded
  @loaded
end

#ownerObject (readonly)

:nodoc:



21
22
23
# File 'lib/datastax_rails/associations/association.rb', line 21

def owner
  @owner
end

#reflectionObject (readonly)

:nodoc:



21
22
23
# File 'lib/datastax_rails/associations/association.rb', line 21

def reflection
  @reflection
end

#targetObject

:nodoc:



21
22
23
# File 'lib/datastax_rails/associations/association.rb', line 21

def target
  @target
end

Instance Method Details

#aliased_column_familyObject

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_scopeObject

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

#klassObject

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_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.

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

#reloadObject

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

#resetObject

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_scopeObject



95
96
97
# File 'lib/datastax_rails/associations/association.rb', line 95

def reset_scope
  @association_scope = nil
end

#scopedObject



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.

Returns:

  • (Boolean)


71
72
73
# File 'lib/datastax_rails/associations/association.rb', line 71

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)



114
115
116
# File 'lib/datastax_rails/associations/association.rb', line 114

def target_scope
  klass.scoped
end