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?, #counter_cache?, #embeddable?, #foreign_key, #in_array?, #in_foreign_array?, #initialize, #ivar, #klass, #ordered?, #polymorphic?, #touch?, #type_key_name

Constructor Details

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

Instance Method Details

#autosave?Boolean

Returns:



58
59
60
# File 'lib/mongo_mapper/plugins/associations/many_association.rb', line 58

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



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

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

#setup(model) ⇒ Object



30
31
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
# File 'lib/mongo_mapper/plugins/associations/many_association.rb', line 30

def setup(model)
  model.associations_module.module_eval(<<-end_eval, __FILE__, __LINE__ + 1)
    def #{name}
      get_proxy(associations[#{name.inspect}]).read
    end

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

  association = self
  options = self.options

  model.before_destroy do
    if !association.embeddable?
      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