Sha256: 429149fe25175956e0bede3304f64c9b3ec779f61e3f339a3ca4d405808fffe5

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module Associations
      class ManyAssociation < Base

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

        def type_key_name
          "_type"
        end

        # hate this, need to revisit
        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

        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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongo_mapper-0.10.1 lib/mongo_mapper/plugins/associations/many_association.rb
mongo_mapper-0.10.0 lib/mongo_mapper/plugins/associations/many_association.rb