Class: MarkMapper::Plugins::Associations::ManyDocumentsProxy

Inherits:
Collection show all
Includes:
DynamicQuerying::ClassMethods
Defined in:
lib/mark_mapper/plugins/associations/many_documents_proxy.rb

Direct Known Subclasses

ManyDocumentsAsProxy, ManyPolymorphicProxy

Instance Attribute Summary

Attributes inherited from Proxy

#association, #proxy_owner, #target

Instance Method Summary collapse

Methods included from DynamicQuerying::ClassMethods

#dynamic_find

Methods inherited from Collection

#include?, #reset, #to_ary

Methods inherited from Proxy

#as_json, #blank?, #initialize, #inspect, #loaded, #loaded?, #nil?, #present?, #proxy_respond_to?, #reload, #reset, #respond_to?, #send, #to_json

Constructor Details

This class inherits a constructor from MarkMapper::Plugins::Associations::Proxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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

Instance Method Details

#<<(*docs) ⇒ Object Also known as: push, concat


24
25
26
27
28
# 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

#build(attrs = {}) {|doc| ... } ⇒ Object

Yields:

  • (doc)

32
33
34
35
36
37
38
39
# 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

#create(attrs = {}) {|doc| ... } ⇒ Object

Yields:

  • (doc)

41
42
43
44
45
46
47
48
# 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| ... } ⇒ Object

Yields:

  • (doc)

50
51
52
53
54
55
56
57
# 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 = {}) ⇒ Object


64
65
66
67
# File 'lib/mark_mapper/plugins/associations/many_documents_proxy.rb', line 64

def delete_all(options={})
  query(options).remove
  reset
end

#destroy_all(options = {}) ⇒ Object


59
60
61
62
# 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) ⇒ Object


78
79
80
# File 'lib/mark_mapper/plugins/associations/many_documents_proxy.rb', line 78

def each(&block)
  load_target.each(&block)
end

#nullifyObject


69
70
71
72
# 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

#replace(docs) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
# 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 = {}) ⇒ Object


74
75
76
# 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