Sha256: 52796644ded4190db6903ebbc2e789e630ccbe77452234dc51a954cdff00754b

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 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

          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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongo_mapper-0.9.2 lib/mongo_mapper/plugins/associations/many_association.rb
mongo_mapper-0.9.1 lib/mongo_mapper/plugins/associations/many_association.rb
mongo_mapper-0.9.0 lib/mongo_mapper/plugins/associations/many_association.rb