class MongoMapper::Plugins::Associations::ManyDocumentsProxy

Public Instance Methods

<<(*docs) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 25
def <<(*docs)
  ensure_owner_saved
  flatten_deeper(docs).each { |doc| prepare(doc).save }
  reset
end
Also aliased as: push, concat
build(attrs={}) { |doc| ... } click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 33
def build(attrs={})
  doc = klass.new(attrs)
  apply_scope(doc)
  yield doc if block_given?
  @target ||= [] unless loaded?
  @target << doc
  doc
end
concat(*docs)
Alias for: <<
create(attrs={}) { |doc| ... } click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 42
def create(attrs={})
  doc = klass.new(attrs)
  apply_scope(doc)
  yield doc if block_given?
  doc.save
  reset
  doc
end
create!(attrs={}) { |doc| ... } click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 51
def create!(attrs={})
  doc = klass.new(attrs)
  apply_scope(doc)
  yield doc if block_given?
  doc.save!
  reset
  doc
end
delete_all(options={}) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 65
def delete_all(options={})
  query(options).remove
  reset
end
destroy_all(options={}) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 60
def destroy_all(options={})
  all(options).each { |doc| doc.destroy }
  reset
end
nullify() click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 70
def nullify
  all.each { |doc| doc.update_attributes(self.foreign_key => nil) }
  reset
end
push(*docs)
Alias for: <<
replace(docs) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 10
def replace(docs)
  load_target

  (target - docs).each do |t|
    case options[:dependent]
      when :destroy    then t.destroy
      when :delete_all then t.delete
      else t.update_attributes(self.foreign_key => nil)
    end
  end

  docs.each { |doc| prepare(doc).save }
  reset
end
save_to_collection(options={}) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 75
def save_to_collection(options={})
  @target.each { |doc| doc.save(options) } if @target
end

Protected Instance Methods

apply_scope(doc) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 104
def apply_scope(doc)
  criteria.each { |key, value| doc[key] = value }
  doc
end
criteria() click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 88
def criteria
  {self.foreign_key => proxy_owner.id}
end
ensure_owner_saved() click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 96
def ensure_owner_saved
  proxy_owner.save unless proxy_owner.persisted?
end
find_target() click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 92
def find_target
  all
end
foreign_key() click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 109
def foreign_key
  options[:foreign_key] || proxy_owner.class.name.foreign_key
end
prepare(doc) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 100
def prepare(doc)
  klass === doc ? apply_scope(doc) : build(doc)
end
query(options={}) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 81
def query(options={})
  klass.
    query(association.query_options).
    amend(options).
    amend(criteria)
end

Private Instance Methods

method_missing(method, *args, &block) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/many_documents_proxy.rb, line 115
def method_missing(method, *args, &block)
  return super unless klass.respond_to?(method)

  result = nil

  query.with_scope(query.criteria_hash) do
    result = klass.send(method, *args, &block)
  end

  case result
  when Plucky::Query
    query.merge result

  # If we got a single record of this class as a result, return it
  when klass
    result

  # If we got an array of this class as a result, return it
  when Array
    if result[0].is_a? klass
      result
    else
      super
    end
  else
    super
  end
end