Class: MongoMapper::Plugins::Associations::InForeignArrayProxy

Inherits:
Collection show all
Includes:
DynamicQuerying::ClassMethods
Defined in:
lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb

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

#[], #each, #empty?, #length, #read, #reset, #size, #to_a, #write

Methods inherited from Proxy

define_proxy_method, #initialize, #inspect, #loaded, #loaded?, #proxy_method, #proxy_respond_to?, #read, #reload, #reset, #respond_to?, #write

Constructor Details

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MongoMapper::Plugins::DynamicQuerying::ClassMethods

Instance Method Details

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



75
76
77
78
79
80
81
# File 'lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb', line 75

def <<(*docs)
  flatten_deeper(docs).each do |doc|
    inverse_association(doc) << proxy_owner
    doc.save
  end
  reset
end

#all(options = {}) ⇒ Object



20
21
22
# File 'lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb', line 20

def all(options={})
  query(options).all
end

#count(options = {}) ⇒ Object



32
33
34
# File 'lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb', line 32

def count(options={})
  query(options).count
end

#create(attrs = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb', line 54

def create(attrs={})
  doc = klass.create(attrs)
  if doc.persisted?
    inverse_association(doc) << proxy_owner
    doc.save
    reset
  end
  doc
end

#create!(attrs = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb', line 64

def create!(attrs={})
  doc = klass.create!(attrs)

  if doc.persisted?
    inverse_association(doc) << proxy_owner
    doc.save
    reset
  end
  doc
end

#delete_all(options = {}) ⇒ Object



43
44
45
46
47
# File 'lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb', line 43

def delete_all(options={})
  docs = query(options).fields(:_id).all
  klass.delete(docs.map { |d| d.id })
  reset
end

#destroy_all(options = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb', line 36

def destroy_all(options={})
  all(options).each do |doc|
    doc.destroy
  end
  reset
end

#find(*args) ⇒ Object



8
9
10
# File 'lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb', line 8

def find(*args)
  query.find(*scoped_ids(args))
end

#find!(*args) ⇒ Object



12
13
14
# File 'lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb', line 12

def find!(*args)
  query.find!(*scoped_ids(args))
end

#first(options = {}) ⇒ Object



24
25
26
# File 'lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb', line 24

def first(options={})
  query(options).first
end

#last(options = {}) ⇒ Object



28
29
30
# File 'lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb', line 28

def last(options={})
  query(options).last
end

#nullifyObject



49
50
51
52
# File 'lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb', line 49

def nullify
  replace([])
  reset
end

#paginate(options) ⇒ Object



16
17
18
# File 'lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb', line 16

def paginate(options)
  query.paginate(options)
end

#replace(docs) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb', line 85

def replace(docs)
  doc_ids = docs.map do |doc|
    doc.save unless doc.persisted?
    inverse_association(doc) << proxy_owner
    doc.save
    doc.id
  end

  replace_selector =  { options[:from] => proxy_owner.id }
  unless doc_ids.empty?
    replace_selector[:_id] = {"$not" => {"$in" => doc_ids}}
  end

  klass.collection.update_many(replace_selector, {
    "$pull" => { options[:from] => proxy_owner.id }
  })

  reset
end