Sha256: d9817f6415c55a94739f53f3222d5c81f03625f0e5ed7f3cb4eac0df7bc826d3

Contents?: true

Size: 653 Bytes

Versions: 8

Compression:

Stored size: 653 Bytes

Contents

module MongoMapper
  module Associations
    class HasManyProxy < ArrayProxy
      def replace(v)
        if load_target
          @target.map(&:destroy)
        end

        v.each do |o|
          @owner.save if @owner.new?
          o.__send__(:write_attribute, self.foreign_key, @owner.id)
          o.save
          o
        end
        reload_target
      end

      protected
      def find_target
        @association.klass.find(:all, {:conditions => {self.foreign_key => @owner.id}})
      end

      def foreign_key
        @association.options[:foreign_key] || @owner.class.name.underscore.gsub("/", "_") + "_id"
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 4 rubygems

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