Class: MarkMapper::Plugins::Associations::ManyAssociation

Inherits:
Base
  • Object
show all
Defined in:
lib/mark_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?, #initialize, #ivar, #klass, #polymorphic?, #touch?, #type_key_name

Constructor Details

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

Instance Method Details

#autosave?Boolean

Returns:


57
58
59
# File 'lib/mark_mapper/plugins/associations/many_association.rb', line 57

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

#class_nameObject


7
8
9
# File 'lib/mark_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
# File 'lib/mark_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_array?
      InArrayProxy
    else
      ManyDocumentsProxy
    end
  end
end

#setup(model) ⇒ Object


28
29
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
# File 'lib/mark_mapper/plugins/associations/many_association.rb', line 28

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

  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