Sha256: 01c1ce8a104aa4962436cbe1f2a2244979173fc4a97a5be9db4cf0edcc7e890f

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module Associations
      class OneAssociation < SingleAssociation
        def embeddable?
          klass.embeddable?
        end

        def proxy_class
          @proxy_class ||=
            if klass.embeddable?
              OneEmbeddedProxy
            elsif as?
              OneAsProxy
            else
              OneProxy
            end
        end

        def setup(model)
          super

          association = self
          options = self.options

          model.before_destroy do
            if !association.embeddable?
              proxy = self.get_proxy(association)

              unless proxy.nil?
                case options[:dependent]
                  when :destroy then proxy.destroy
                  when :delete  then proxy.delete
                  else proxy.nullify
                end
              end
            end
          end
        end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongo_mapper-0.10.1 lib/mongo_mapper/plugins/associations/one_association.rb