Class: MongoMapper::Plugins::Associations::OneAssociation

Inherits:
SingleAssociation show all
Defined in:
lib/mongo_mapper/plugins/associations/one_association.rb

Constant Summary

Constant Summary

Constants inherited from Base

Base::AssociationOptions

Instance Attribute Summary

Attributes inherited from Base

#name, #options, #query_options

Instance Method Summary (collapse)

Methods inherited from Base

#as, #as?, #class_name, #foreign_key, #in_array?, #initialize, #ivar, #klass, #polymorphic?, #touch?, #type_key_name

Constructor Details

This class inherits a constructor from MongoMapper::Plugins::Associations::Base

Instance Method Details

- (Boolean) autosave?

Returns:



42
43
44
# File 'lib/mongo_mapper/plugins/associations/one_association.rb', line 42

def autosave?
  options.fetch(:autosave, embeddable?)
end

- (Boolean) embeddable?

Returns:



6
7
8
# File 'lib/mongo_mapper/plugins/associations/one_association.rb', line 6

def embeddable?
  klass.embeddable?
end

- (Object) proxy_class



10
11
12
13
14
15
16
17
18
19
# File 'lib/mongo_mapper/plugins/associations/one_association.rb', line 10

def proxy_class
  @proxy_class ||=
    if klass.embeddable?
      polymorphic? ? OneEmbeddedPolymorphicProxy : OneEmbeddedProxy
    elsif as?
      OneAsProxy
    else
      OneProxy
    end
end

- (Object) setup(model)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mongo_mapper/plugins/associations/one_association.rb', line 21

def setup(model)
  super

  association = self
  options = self.options

  model.before_destroy do
    if !association.embeddable?
      proxy = self.get_proxy(association)

      unless proxy.nil?
        case options[:dependent]
          when :destroy then proxy.destroy
          when :delete  then proxy.delete
          else proxy.nullify
        end
      end
    end
  end
end