Sha256: 68539155101c895de50304899f0213aeeadec3b69a0c2e2279fd6df0dea3476e

Contents?: true

Size: 559 Bytes

Versions: 10

Compression:

Stored size: 559 Bytes

Contents

module MongoMapper
  module Associations
    class BelongsToProxy < Proxy
      def replace(v)
        ref_id = "#{@association.name}_id"

        if v
          v.save if v.new?
          @owner.__send__(:write_attribute, ref_id, v.id)
        else
          @owner.__send__(:write_attribute, ref_id, nil)
        end

        reload_target
      end

      protected
      def find_target
        ref = @owner.__send__(:read_attribute, "#{@association.name}_id")
        if ref
          @association.klass.find(ref)
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 4 rubygems

Version Path
crnixon-mongomapper-0.2.0 lib/mongomapper/associations/belongs_to_proxy.rb
fcoury-mongomapper-0.2.0 lib/mongomapper/associations/belongs_to_proxy.rb
fcoury-mongomapper-0.3.0 lib/mongomapper/associations/belongs_to_proxy.rb
fcoury-mongomapper-0.3.1 lib/mongomapper/associations/belongs_to_proxy.rb
fcoury-mongomapper-0.3.3 lib/mongomapper/associations/belongs_to_proxy.rb
fcoury-mongomapper-0.3.4 lib/mongomapper/associations/belongs_to_proxy.rb
fcoury-mongomapper-0.3.5 lib/mongomapper/associations/belongs_to_proxy.rb
fcoury-mongomapper-0.4.0 lib/mongomapper/associations/belongs_to_proxy.rb
jnunemaker-mongomapper-0.2.0 lib/mongomapper/associations/belongs_to_proxy.rb
ramsingla-mongomapper-0.2.1 lib/mongomapper/associations/belongs_to_proxy.rb