Class: MongoMapper::Plugins::Associations::ManyAssociation

Inherits:
Base
  • Object
show all
Defined in:
lib/mongo_mapper/plugins/associations/many_association.rb

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?, #embeddable?, #foreign_key, #in_array?, #initialize, #ivar, #klass, #polymorphic?

Constructor Details

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

Instance Method Details

#autosave?Boolean

Returns:



61
62
63
# File 'lib/mongo_mapper/plugins/associations/many_association.rb', line 61

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

#class_nameObject



7
8
9
# File 'lib/mongo_mapper/plugins/associations/many_association.rb', line 7

def class_name
  @class_name ||= options[:class_name] || name.to_s.singularize.camelize
end

#proxy_classObject

hate this, need to revisit



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mongo_mapper/plugins/associations/many_association.rb', line 16

def proxy_class
  @proxy_class ||= if klass.embeddable?
    polymorphic? ? ManyEmbeddedPolymorphicProxy : ManyEmbeddedProxy
  else
    if polymorphic?
      ManyPolymorphicProxy
    elsif as?
      ManyDocumentsAsProxy
    elsif in_array?
      InArrayProxy
    else
      ManyDocumentsProxy
    end
  end
end

#setup(model) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mongo_mapper/plugins/associations/many_association.rb', line 32

def setup(model)
  model.associations_module.module_eval <<-end_eval
    def #{name}
      get_proxy(associations[#{name.inspect}])
    end

    def #{name}=(value)
      get_proxy(associations[#{name.inspect}]).replace(value)
      value
    end
  end_eval

  if options[:dependent] && !embeddable?
    association = self
    options = self.options

    model.after_destroy do
      case options[:dependent]
        when :destroy
          self.get_proxy(association).destroy_all
        when :delete_all
          self.get_proxy(association).delete_all
        when :nullify
          self.get_proxy(association).nullify
      end
    end
  end
end

#type_key_nameObject



11
12
13
# File 'lib/mongo_mapper/plugins/associations/many_association.rb', line 11

def type_key_name
  "_type"
end