Sha256: ab9c953cdc946723a99bba65c4a884d364564da91fe8f24a5d0eac75318c8f68

Contents?: true

Size: 882 Bytes

Versions: 2

Compression:

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

2 entries across 2 versions & 1 rubygems

Version Path
fcoury-mongomapper-0.2.0 lib/mongomapper/associations/polymorphic_belongs_to_proxy.rb
fcoury-mongomapper-0.4.0 lib/mongomapper/associations/polymorphic_belongs_to_proxy.rb