Class: Mongoid::Association::Referenced::HasOneThrough

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Association::Relatable
Defined in:
lib/mongoid/association/referenced/has_one_through.rb,
lib/mongoid/association/referenced/has_one_through/eager.rb,
lib/mongoid/association/referenced/has_one_through/proxy.rb

Overview

Metadata class for has_one :through associations.

Defined Under Namespace

Classes: Eager, Proxy

Constant Summary collapse

ASSOCIATION_OPTIONS =

The options available for this type of association, in addition to the common ones.

Returns:

  • (Array<Symbol>)

    The extra valid options.

i[source through].freeze
VALID_OPTIONS =

The complete list of valid options for this association, including the shared ones.

Returns:

  • (Array<Symbol>)

    The valid options.

(ASSOCIATION_OPTIONS + SHARED_OPTIONS).freeze

Constants included from Mongoid::Association::Relatable

Mongoid::Association::Relatable::PRIMARY_KEY_DEFAULT, Mongoid::Association::Relatable::SHARED_OPTIONS

Instance Attribute Summary

Attributes included from Mongoid::Association::Relatable

#name, #options, #owner_class, #parent_inclusions

Instance Method Summary collapse

Methods included from Mongoid::Association::Relatable

#==, #bindable?, #counter_cache_column_name, #create_relation, #destructive?, #extension, #foreign_key_check, #foreign_key_setter, #get_callbacks, #in_to?, #initialize, #inverse, #inverse_association, #inverse_class, #inverse_class_name, #inverse_setter, #inverse_type, #inverse_type_setter, #inverses, #key, #many?, #many_to_many?, #one?, #path, #relation_class, #relation_class_name, #setter, #try_relation_class, #type_setter, #validate?

Methods included from Options

#as, #autobuilding?, #autosave, #cascading_callbacks?, #counter_cached?, #cyclic?, #dependent, #fallback, #fallback?, #forced_nil_inverse?, #indexed?, #inverse_of, #order, #polymorphic?, #primary_key, #store_as, #touch_field, #touchable?, #type

Methods included from Constrainable

#convert_to_foreign_key

Instance Method Details

#criteria(base) ⇒ Document | nil

Resolve the target by delegating through the intermediate proxy. Unlike other association types, this returns a document (or nil) directly rather than a Mongoid::Criteria, because the two-hop traversal is performed eagerly via the existing association proxy.

Parameters:

  • base (Document)

    The owner document.

Returns:



118
119
120
121
122
123
# File 'lib/mongoid/association/referenced/has_one_through.rb', line 118

def criteria(base)
  through_target = base.public_send(through_association.name)
  return nil if through_target.nil?

  through_target.public_send(source_association.name)
end

#embedded?false

Is this association embedded?

Returns:

  • (false)


43
44
45
# File 'lib/mongoid/association/referenced/has_one_through.rb', line 43

def embedded?
  false
end

#relationClass

The proxy class for this association type.

Returns:

  • (Class)


50
51
52
# File 'lib/mongoid/association/referenced/has_one_through.rb', line 50

def relation
  Proxy
end

#relation_complementsArray

The list of association complements.

Returns:

  • (Array)


28
29
30
# File 'lib/mongoid/association/referenced/has_one_through.rb', line 28

def relation_complements
  [].freeze
end

#setup!self

Setup instance methods on the owner class.

Returns:

  • (self)


35
36
37
38
# File 'lib/mongoid/association/referenced/has_one_through.rb', line 35

def setup!
  setup_instance_methods!
  self
end

#source_associationMongoid::Association::Relatable

The source association metadata on the intermediate class. Resolved lazily to allow forward references.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mongoid/association/referenced/has_one_through.rb', line 82

def source_association
  @source_association ||= begin
    source_name = (@options[:source] || name).to_s
    assoc = through_association.klass.relations[source_name] ||
            raise(
              Errors::InvalidRelationOption.new(
                @owner_class, name, :source, source_name
              )
            )
    if assoc.is_a?(Referenced::HasAndBelongsToMany)
      raise(
        Errors::InvalidRelationOption.new(
          @owner_class, name, :source,
          'has_and_belongs_to_many is not supported as a :through source'
        )
      )
    end
    assoc
  end
end

#stores_foreign_key?false

Through associations never store a foreign key on the owner document.

Returns:

  • (false)


106
107
108
# File 'lib/mongoid/association/referenced/has_one_through.rb', line 106

def stores_foreign_key?
  false
end

#through_associationMongoid::Association::Relatable

The intermediate association metadata on the owner class. Resolved lazily to allow forward references.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mongoid/association/referenced/has_one_through.rb', line 58

def through_association
  @through_association ||= begin
    assoc = @owner_class.relations[@options[:through].to_s] ||
            raise(
              Errors::InvalidRelationOption.new(
                @owner_class, name, :through, @options[:through]
              )
            )
    if assoc.embedded?
      raise(
        Errors::InvalidRelationOption.new(
          @owner_class, name, :through,
          'through association must be a referenced association, not embedded'
        )
      )
    end
    assoc
  end
end

#validation_defaultfalse

The default for validating the association object.

Returns:

  • (false)


128
129
130
# File 'lib/mongoid/association/referenced/has_one_through.rb', line 128

def validation_default
  false
end