Sha256: 36c3bd65def0d7c0e0be98d6a8f4c11d5ff7fd9d98ec70d0bbd15322882f63fb

Contents?: true

Size: 868 Bytes

Versions: 8

Compression:

Stored size: 868 Bytes

Contents

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

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

        reload_target
      end

      protected
      def find_target
        ref_id = @owner.__send__(:read_attribute, "#{@association.name}_id")
        ref_type = @owner.__send__(:read_attribute, "#{@association.name}_type")
        if ref_id && ref_type
          ref_type.constantize.find(ref_id)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 4 rubygems

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