class MarkMapper::Plugins::Associations::ManyDocumentsProxy

Public Instance Methods

<<(*docs) click to toggle source
# File lib/mark_mapper/plugins/associations/many_documents_proxy.rb, line 24
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/mark_mapper/plugins/associations/many_documents_proxy.rb, line 32
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/mark_mapper/plugins/associations/many_documents_proxy.rb, line 41
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/mark_mapper/plugins/associations/many_documents_proxy.rb, line 50
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/mark_mapper/plugins/associations/many_documents_proxy.rb, line 64
def delete_all(options={})
  query(options).remove
  reset
end
destroy_all(options={}) click to toggle source
# File lib/mark_mapper/plugins/associations/many_documents_proxy.rb, line 59
def destroy_all(options={})
  all(options).each { |doc| doc.destroy }
  reset
end
each(&block) click to toggle source
# File lib/mark_mapper/plugins/associations/many_documents_proxy.rb, line 78
def each(&block)
  load_target.each(&block)
end
nullify() click to toggle source
# File lib/mark_mapper/plugins/associations/many_documents_proxy.rb, line 69
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/mark_mapper/plugins/associations/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/mark_mapper/plugins/associations/many_documents_proxy.rb, line 74
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/mark_mapper/plugins/associations/many_documents_proxy.rb, line 131
def apply_scope(doc)
  criteria.each { |key, value| doc[key] = value }
  doc
end
criteria() click to toggle source
# File lib/mark_mapper/plugins/associations/many_documents_proxy.rb, line 115
def criteria
  {self.foreign_key => proxy_owner.id}
end
ensure_owner_saved() click to toggle source
# File lib/mark_mapper/plugins/associations/many_documents_proxy.rb, line 123
def ensure_owner_saved
  proxy_owner.save unless proxy_owner.persisted?
end
find_target() click to toggle source
# File lib/mark_mapper/plugins/associations/many_documents_proxy.rb, line 119
def find_target
  all
end
foreign_key() click to toggle source
# File lib/mark_mapper/plugins/associations/many_documents_proxy.rb, line 136
def foreign_key
  options[:foreign_key] || proxy_owner.class.name.foreign_key
end
method_missing(method, *args, &block) click to toggle source
# File lib/mark_mapper/plugins/associations/many_documents_proxy.rb, line 89
def method_missing(method, *args, &block)
  if klass.respond_to?(method)
    result = klass.send(method, *args, &block)
    case result
    when MarkMapper::Query
      query.merge result

    # If we got a single record of this classas 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
  else
    super
  end
end
prepare(doc) click to toggle source
# File lib/mark_mapper/plugins/associations/many_documents_proxy.rb, line 127
def prepare(doc)
  klass === doc ? apply_scope(doc) : build(doc)
end
query(options={}) click to toggle source
# File lib/mark_mapper/plugins/associations/many_documents_proxy.rb, line 83
def query(options={})
  klass.
    query(association.query_options).
    amend(options).amend(criteria)
end