Sha256: ff37ba1d96ad3b9d7f81cc69088cdbf59872a51df81f010c3ae7a339c405ee20

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

module MongoMapper
  module Associations
    class ManyDocumentsProxy < Proxy
      delegate :klass, :to => :@association
      
      def find(*args)
        options = args.extract_options!
        klass.find(*args << scoped_options(options))
      end
      
      def paginate(options)
        klass.paginate(scoped_options(options))
      end
      
      def all(options={})
        find(:all, scoped_options(options))
      end
      
      def first(options={})
        find(:first, scoped_options(options))
      end
      
      def last(options={})
        find(:last, scoped_options(options))
      end
      
      def count(conditions={})
        klass.count(conditions.deep_merge(scoped_conditions))
      end
      
      def replace(docs)
        @target.map(&:destroy) if load_target
        docs.each { |doc| apply_scope(doc).save }
        reset
      end
      
      def <<(*docs)
        ensure_owner_saved
        flatten_deeper(docs).each { |doc| apply_scope(doc).save }
        reset
      end
      alias_method :push, :<<
      alias_method :concat, :<<
      
      def build(attrs={})
        doc = klass.new(attrs)
        apply_scope(doc)
        doc
      end
      
      def create(attrs={})
        doc = klass.new(attrs)
        apply_scope(doc).save
        doc
      end
      
      protected
        def scoped_conditions
          {self.foreign_key => @owner.id}
        end
        
        def scoped_options(options)
          options.deep_merge({:conditions => scoped_conditions})
        end
        
        def find_target
          find(:all)
        end
        
        def ensure_owner_saved
          @owner.save if @owner.new?
        end
        
        def apply_scope(doc)
          ensure_owner_saved
          doc.send("#{self.foreign_key}=", @owner.id)
          doc
        end

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

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
djsun-mongomapper-0.3.1.1 lib/mongomapper/associations/many_documents_proxy.rb
djsun-mongomapper-0.3.1 lib/mongomapper/associations/many_documents_proxy.rb
jnunemaker-mongomapper-0.3.2 lib/mongomapper/associations/many_documents_proxy.rb