mongo_mapper 0.14.0 → 0.15.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/{README.rdoc → README.md} +26 -21
- data/examples/keys.rb +1 -1
- data/examples/modifiers/set.rb +1 -1
- data/examples/querying.rb +1 -1
- data/examples/safe.rb +2 -2
- data/examples/scopes.rb +1 -1
- data/lib/mongo_mapper/connection.rb +16 -38
- data/lib/mongo_mapper/extensions/object_id.rb +5 -1
- data/lib/mongo_mapper/plugins/accessible.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/base.rb +10 -2
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +36 -6
- data/lib/mongo_mapper/plugins/associations/in_foreign_array_proxy.rb +136 -0
- data/lib/mongo_mapper/plugins/associations/many_association.rb +4 -2
- data/lib/mongo_mapper/plugins/associations/proxy.rb +5 -1
- data/lib/mongo_mapper/plugins/associations/single_association.rb +5 -4
- data/lib/mongo_mapper/plugins/dirty.rb +29 -37
- data/lib/mongo_mapper/plugins/document.rb +1 -1
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +1 -1
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +1 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +1 -1
- data/lib/mongo_mapper/plugins/identity_map.rb +3 -1
- data/lib/mongo_mapper/plugins/indexes.rb +13 -6
- data/lib/mongo_mapper/plugins/keys/key.rb +13 -8
- data/lib/mongo_mapper/plugins/keys.rb +9 -2
- data/lib/mongo_mapper/plugins/modifiers.rb +39 -14
- data/lib/mongo_mapper/plugins/persistence.rb +6 -2
- data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +6 -6
- data/lib/mongo_mapper/plugins/querying.rb +9 -3
- data/lib/mongo_mapper/plugins/safe.rb +10 -4
- data/lib/mongo_mapper/plugins/stats.rb +1 -3
- data/lib/mongo_mapper/plugins/strong_parameters.rb +26 -0
- data/lib/mongo_mapper/railtie.rb +1 -0
- data/lib/mongo_mapper/utils.rb +2 -2
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/mongo_mapper.rb +3 -0
- data/spec/examples.txt +1717 -0
- data/spec/functional/accessible_spec.rb +7 -1
- data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +2 -2
- data/spec/functional/associations/belongs_to_proxy_spec.rb +22 -5
- data/spec/functional/associations/in_array_proxy_spec.rb +149 -14
- data/spec/functional/associations/in_foreign_array_proxy_spec.rb +321 -0
- data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
- data/spec/functional/associations/many_documents_proxy_spec.rb +22 -22
- data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +2 -2
- data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
- data/spec/functional/associations/one_as_proxy_spec.rb +8 -8
- data/spec/functional/associations/one_proxy_spec.rb +8 -8
- data/spec/functional/associations_spec.rb +3 -3
- data/spec/functional/binary_spec.rb +2 -2
- data/spec/functional/caching_spec.rb +15 -22
- data/spec/functional/callbacks_spec.rb +2 -2
- data/spec/functional/counter_cache_spec.rb +10 -10
- data/spec/functional/dirty_spec.rb +27 -10
- data/spec/functional/document_spec.rb +5 -8
- data/spec/functional/dumpable_spec.rb +1 -1
- data/spec/functional/embedded_document_spec.rb +5 -5
- data/spec/functional/identity_map_spec.rb +8 -8
- data/spec/functional/indexes_spec.rb +19 -18
- data/spec/functional/keys_spec.rb +51 -33
- data/spec/functional/logger_spec.rb +2 -2
- data/spec/functional/modifiers_spec.rb +81 -19
- data/spec/functional/partial_updates_spec.rb +8 -8
- data/spec/functional/protected_spec.rb +1 -1
- data/spec/functional/querying_spec.rb +70 -22
- data/spec/functional/safe_spec.rb +23 -27
- data/spec/functional/sci_spec.rb +7 -7
- data/spec/functional/scopes_spec.rb +1 -1
- data/spec/functional/static_keys_spec.rb +2 -2
- data/spec/functional/stats_spec.rb +28 -12
- data/spec/functional/strong_parameters_spec.rb +49 -0
- data/spec/functional/validations_spec.rb +8 -16
- data/spec/quality_spec.rb +1 -1
- data/spec/spec_helper.rb +32 -8
- data/spec/support/matchers.rb +1 -1
- data/spec/unit/associations/proxy_spec.rb +1 -5
- data/spec/unit/clone_spec.rb +1 -1
- data/spec/unit/document_spec.rb +3 -3
- data/spec/unit/embedded_document_spec.rb +4 -5
- data/spec/unit/extensions_spec.rb +3 -4
- data/spec/unit/identity_map_middleware_spec.rb +65 -96
- data/spec/unit/key_spec.rb +16 -17
- data/spec/unit/keys_spec.rb +17 -8
- data/spec/unit/mongo_mapper_spec.rb +41 -88
- data/spec/unit/rails_spec.rb +2 -2
- data/spec/unit/validations_spec.rb +18 -18
- metadata +41 -24
- data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 77d669d6c4286995fb2afbed7cf23475c6dc496cb3faf97a57f3b8f115e263b3
|
4
|
+
data.tar.gz: d11040bb05eeb6cb57465c4585c0667b16d750b16d7fd5fedca5e03734b769ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56a665e63c65da52238cc18348333a2a9c6b6d1c5acf564308f60f8fcec8a1be759c5b7546a0259092a7f6acc9170659d55c462ac59fffe6520f3339a650df15
|
7
|
+
data.tar.gz: 8dceac60d1ef1159585453450d2960e4d9d50e472ec7728f3b703da8e40e80af625b01bb6c4022327e16631960faa698da93c7d37c563cd1c839eb3ebfc28ddb
|
data/LICENSE
CHANGED
data/{README.rdoc → README.md}
RENAMED
@@ -1,39 +1,35 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
{<img src="https://badge.fury.io/rb/mongo_mapper.png" alt="RubyGem" />}[https://rubygems.org/gems/mongo_mapper]
|
4
|
-
{<img src="https://travis-ci.org/mongomapper/mongomapper.png?branch=master" alt="Build Status" />}[https://travis-ci.org/mongomapper/mongomapper]
|
5
|
-
{<img src="https://coveralls.io/repos/mongomapper/mongomapper/badge.png" alt="Coverage Status" />}[https://coveralls.io/r/mongomapper/mongomapper]
|
1
|
+
# MongoMapper
|
6
2
|
|
7
3
|
A Ruby Object Mapper for Mongo.
|
8
4
|
|
9
|
-
|
5
|
+
[<img src="https://badge.fury.io/rb/mongo_mapper.svg" alt="RubyGems">](https://rubygems.org/gems/mongo_mapper)
|
6
|
+
|
7
|
+
[<img src="https://travis-ci.org/mongomapper/mongomapper.svg?branch=master" alt="Build Status" />](https://travis-ci.org/mongomapper/mongomapper)
|
8
|
+
|
9
|
+
[<img src="https://coveralls.io/repos/mongomapper/mongomapper/badge.svg" alt="Coverage Status" />](https://coveralls.io/r/mongomapper/mongomapper)
|
10
|
+
|
11
|
+
## Install
|
10
12
|
|
11
13
|
$ gem install mongo_mapper
|
12
14
|
|
13
|
-
|
15
|
+
## Documentation
|
14
16
|
|
15
17
|
http://mongomapper.com/documentation/
|
16
18
|
|
17
19
|
http://rdoc.info/github/mongomapper/mongomapper
|
18
20
|
|
19
|
-
|
21
|
+
## Compatibility
|
20
22
|
|
21
23
|
MongoMapper is tested against:
|
22
24
|
|
23
|
-
* MRI
|
24
|
-
* MRI 1.9.3
|
25
|
-
* MRI 2.0.0
|
26
|
-
* MRI 2.1.1
|
25
|
+
* MRI 2.4 - 2.7
|
27
26
|
* JRuby (Versions with 1.9 compatibility)
|
28
27
|
|
29
28
|
Additionally, MongoMapper is tested against:
|
30
29
|
|
31
|
-
* Rails
|
32
|
-
* Rails 3.1.x
|
33
|
-
* Rails 3.2.x
|
34
|
-
* Rails 4.0.x
|
30
|
+
* Rails 5.0+ - 6.0
|
35
31
|
|
36
|
-
|
32
|
+
## Contributing & Development
|
37
33
|
|
38
34
|
$ git clone https://github.com/mongomapper/mongomapper && cd mongomapper
|
39
35
|
$ bundle install
|
@@ -45,12 +41,21 @@ Additionally, MongoMapper is tested against:
|
|
45
41
|
* Commit, do not mess with Rakefile, version, or history. If you want to have your own version, that is fine but bump version in a commit by itself in another branch so a maintainer ignore it when your pull request is merged.
|
46
42
|
* Send a pull request. Bonus points for topic branches.
|
47
43
|
|
48
|
-
|
44
|
+
## Problems or Questions?
|
49
45
|
|
50
46
|
Hit up the Google group: http://groups.google.com/group/mongomapper
|
51
47
|
|
52
|
-
|
48
|
+
## Copyright
|
49
|
+
|
50
|
+
Copyright (c) 2009-2020 MongoMapper. See LICENSE for details.
|
51
|
+
|
52
|
+
## Contributors
|
53
|
+
|
54
|
+
MongoMapper/Plucky is:
|
53
55
|
|
54
|
-
|
56
|
+
* John Nunemaker
|
57
|
+
* Chris Heald
|
58
|
+
* Scott Taylor
|
55
59
|
|
56
|
-
|
60
|
+
But all open source projects are a team effort and could not happen without
|
61
|
+
everyone who has contributed. See `CONTRIBUTORS` for the full list. Thank you!
|
data/examples/keys.rb
CHANGED
data/examples/modifiers/set.rb
CHANGED
@@ -10,7 +10,7 @@ class User
|
|
10
10
|
key :name, String
|
11
11
|
key :tags, Array
|
12
12
|
end
|
13
|
-
User.collection.
|
13
|
+
User.collection.drop # empties collection
|
14
14
|
|
15
15
|
john = User.create(:name => 'John', :tags => %w[ruby mongo], :age => 28)
|
16
16
|
bill = User.create(:name => 'Bill', :tags => %w[ruby mongo], :age => 30)
|
data/examples/querying.rb
CHANGED
@@ -10,7 +10,7 @@ class User
|
|
10
10
|
key :name, String
|
11
11
|
key :tags, Array
|
12
12
|
end
|
13
|
-
User.collection.
|
13
|
+
User.collection.drop # empties collection
|
14
14
|
|
15
15
|
User.create(:name => 'John', :tags => %w[ruby mongo], :age => 28)
|
16
16
|
User.create(:name => 'Bill', :tags => %w[ruby mongo], :age => 30)
|
data/examples/safe.rb
CHANGED
@@ -22,7 +22,7 @@ puts
|
|
22
22
|
begin
|
23
23
|
user = User.new(:email => 'nunemaker@gmail.com')
|
24
24
|
user.save(:safe => true)
|
25
|
-
rescue Mongo::OperationFailure => e
|
25
|
+
rescue Mongo::Error::OperationFailure => e
|
26
26
|
puts 'Mongo Operation failure raised because duplicate email was entered'
|
27
27
|
puts e.inspect
|
28
28
|
puts
|
@@ -37,7 +37,7 @@ User.safe
|
|
37
37
|
|
38
38
|
begin
|
39
39
|
User.create(:email => 'nunemaker@gmail.com')
|
40
|
-
rescue Mongo::OperationFailure => e
|
40
|
+
rescue Mongo::Error::OperationFailure => e
|
41
41
|
puts 'Mongo Operation failure raised because duplicate email was entered'
|
42
42
|
puts e.inspect
|
43
43
|
end
|
data/examples/scopes.rb
CHANGED
@@ -28,7 +28,7 @@ class User
|
|
28
28
|
key :name, String
|
29
29
|
key :tags, Array
|
30
30
|
end
|
31
|
-
User.collection.
|
31
|
+
User.collection.drop # empties collection
|
32
32
|
|
33
33
|
User.create(:name => 'John', :tags => %w[ruby mongo], :age => 28)
|
34
34
|
User.create(:name => 'Bill', :tags => %w[ruby mongo], :age => 30)
|
@@ -4,13 +4,12 @@ require 'uri'
|
|
4
4
|
module MongoMapper
|
5
5
|
module Connection
|
6
6
|
@@connection = nil
|
7
|
-
@@database = nil
|
8
|
-
@@database_name = nil
|
9
7
|
@@config = nil
|
8
|
+
@@database = nil
|
10
9
|
|
11
10
|
# @api public
|
12
11
|
def connection
|
13
|
-
@@connection ||= Mongo::
|
12
|
+
@@connection ||= Mongo::Client.new ['127.0.0.1:27017']
|
14
13
|
end
|
15
14
|
|
16
15
|
def connection?
|
@@ -29,15 +28,12 @@ module MongoMapper
|
|
29
28
|
|
30
29
|
# @api public
|
31
30
|
def database=(name)
|
32
|
-
@@database =
|
33
|
-
@@database_name = name
|
31
|
+
@@database = connection.use(name).database
|
34
32
|
end
|
35
33
|
|
36
34
|
# @api public
|
37
35
|
def database
|
38
|
-
|
39
|
-
|
40
|
-
@@database ||= MongoMapper.connection.db(@@database_name)
|
36
|
+
@@database ||= connection.database
|
41
37
|
end
|
42
38
|
|
43
39
|
def config=(hash)
|
@@ -51,46 +47,28 @@ module MongoMapper
|
|
51
47
|
|
52
48
|
# @api private
|
53
49
|
def config_for_environment(environment)
|
54
|
-
|
55
|
-
return env if env['uri'].blank?
|
56
|
-
|
57
|
-
uri = URI.parse(env['uri'])
|
58
|
-
raise InvalidScheme.new('must be mongodb') unless uri.scheme == 'mongodb'
|
59
|
-
{
|
60
|
-
'host' => uri.host,
|
61
|
-
'port' => uri.port,
|
62
|
-
'database' => uri.path.gsub(/^\//, ''),
|
63
|
-
'username' => uri.user,
|
64
|
-
'password' => uri.password,
|
65
|
-
}
|
50
|
+
config[environment.to_s]
|
66
51
|
end
|
67
52
|
|
68
53
|
def connect(environment, options={})
|
69
54
|
raise 'Set config before connecting. MongoMapper.config = {...}' if config.blank?
|
70
|
-
env = config_for_environment(environment)
|
55
|
+
env = config_for_environment(environment).dup
|
56
|
+
addresses_or_uri = env.delete('hosts') ||
|
57
|
+
env.delete('uri') ||
|
58
|
+
[env.delete('host')].compact
|
71
59
|
|
72
60
|
if env['options'].is_a?(Hash)
|
73
|
-
options = env
|
74
|
-
end
|
75
|
-
options[:read] = options[:read].to_sym if options[:read].is_a? String
|
76
|
-
|
77
|
-
if env.key?('ssl')
|
78
|
-
options[:ssl] = env['ssl']
|
61
|
+
options = env.delete('options').symbolize_keys.merge(options)
|
79
62
|
end
|
63
|
+
#database etc are all options to Mongo::Client.new now
|
64
|
+
options = env.symbolize_keys.merge(options)
|
80
65
|
|
81
|
-
|
82
|
-
|
83
|
-
if env['hosts'].first.is_a?(String)
|
84
|
-
klass.new( env['hosts'], options )
|
85
|
-
else
|
86
|
-
klass.new( *env['hosts'].push(options) )
|
87
|
-
end
|
88
|
-
else
|
89
|
-
Mongo::MongoClient.new(env['host'], env['port'], options)
|
66
|
+
if options[:port]
|
67
|
+
raise "port should be specified as part of the host or uri"
|
90
68
|
end
|
91
69
|
|
92
|
-
|
93
|
-
MongoMapper.
|
70
|
+
options[:read] = options[:read].to_sym if options[:read].is_a? String
|
71
|
+
MongoMapper.connection = Mongo::Client.new(addresses_or_uri, options)
|
94
72
|
end
|
95
73
|
|
96
74
|
def setup(config, environment, options={})
|
@@ -18,7 +18,7 @@ class ObjectId
|
|
18
18
|
end
|
19
19
|
|
20
20
|
class BSON::ObjectId
|
21
|
-
alias_method :
|
21
|
+
alias_method :original_as_json, :as_json
|
22
22
|
|
23
23
|
def as_json(options=nil)
|
24
24
|
to_s
|
@@ -29,4 +29,8 @@ class BSON::ObjectId
|
|
29
29
|
end
|
30
30
|
|
31
31
|
alias to_str to_s
|
32
|
+
|
33
|
+
def original_to_json(*args)
|
34
|
+
original_as_json.to_json(*args)
|
35
|
+
end
|
32
36
|
end
|
@@ -6,7 +6,7 @@ module MongoMapper
|
|
6
6
|
attr_reader :name, :options, :query_options
|
7
7
|
|
8
8
|
# Options that should not be considered MongoDB query options/criteria
|
9
|
-
AssociationOptions = [:as, :class, :class_name, :dependent, :extend, :foreign_key, :in, :polymorphic, :autosave, :touch, :counter_cache]
|
9
|
+
AssociationOptions = [:as, :class, :class_name, :dependent, :extend, :foreign_key, :in, :from, :polymorphic, :autosave, :touch, :counter_cache, :ordered]
|
10
10
|
|
11
11
|
def initialize(name, options={}, &extension)
|
12
12
|
@name, @options, @query_options, @original_options = name.to_sym, {}, {}, options
|
@@ -28,13 +28,17 @@ module MongoMapper
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def as?
|
31
|
-
!!@options[:as]
|
31
|
+
!in_foreign_array? && !!@options[:as]
|
32
32
|
end
|
33
33
|
|
34
34
|
def in_array?
|
35
35
|
!!@options[:in]
|
36
36
|
end
|
37
37
|
|
38
|
+
def in_foreign_array?
|
39
|
+
!!@options[:from]
|
40
|
+
end
|
41
|
+
|
38
42
|
def embeddable?
|
39
43
|
klass.embeddable?
|
40
44
|
end
|
@@ -47,6 +51,10 @@ module MongoMapper
|
|
47
51
|
!!@options[:counter_cache]
|
48
52
|
end
|
49
53
|
|
54
|
+
def ordered?
|
55
|
+
!!@options[:ordered]
|
56
|
+
end
|
57
|
+
|
50
58
|
def type_key_name
|
51
59
|
"_type"
|
52
60
|
end
|
@@ -29,7 +29,7 @@ module MongoMapper
|
|
29
29
|
|
30
30
|
def add_touch_callbacks
|
31
31
|
name = self.name
|
32
|
-
method_name = "belongs_to_touch_after_save_or_destroy_for_#{name}"
|
32
|
+
method_name = :"belongs_to_touch_after_save_or_destroy_for_#{name}"
|
33
33
|
touch = options.fetch(:touch)
|
34
34
|
|
35
35
|
@model.send(:define_method, method_name) do
|
@@ -6,11 +6,11 @@ module MongoMapper
|
|
6
6
|
include DynamicQuerying::ClassMethods
|
7
7
|
|
8
8
|
def find(*args)
|
9
|
-
query.find(*scoped_ids(args))
|
9
|
+
order_results(query.find(*scoped_ids(args)))
|
10
10
|
end
|
11
11
|
|
12
12
|
def find!(*args)
|
13
|
-
query.find!(*scoped_ids(args))
|
13
|
+
order_results(query.find!(*scoped_ids(args)))
|
14
14
|
end
|
15
15
|
|
16
16
|
def paginate(options)
|
@@ -20,17 +20,29 @@ module MongoMapper
|
|
20
20
|
|
21
21
|
def all(options={})
|
22
22
|
return [] if ids.blank?
|
23
|
-
query(options).all
|
23
|
+
order_results(query(options).all)
|
24
24
|
end
|
25
25
|
|
26
26
|
def first(options={})
|
27
27
|
return nil if ids.blank?
|
28
|
-
|
28
|
+
|
29
|
+
if ordered?
|
30
|
+
ids = find_ordered_ids(options)
|
31
|
+
find!(ids.first) if ids.any?
|
32
|
+
else
|
33
|
+
query(options).first
|
34
|
+
end
|
29
35
|
end
|
30
36
|
|
31
37
|
def last(options={})
|
32
38
|
return nil if ids.blank?
|
33
|
-
|
39
|
+
|
40
|
+
if ordered?
|
41
|
+
ids = find_ordered_ids(options)
|
42
|
+
find!(ids.last) if ids.any?
|
43
|
+
else
|
44
|
+
query(options).last
|
45
|
+
end
|
34
46
|
end
|
35
47
|
|
36
48
|
def count(options={})
|
@@ -120,6 +132,13 @@ module MongoMapper
|
|
120
132
|
valid.empty? ? nil : valid
|
121
133
|
end
|
122
134
|
|
135
|
+
def find_ordered_ids(options={})
|
136
|
+
return ids if options.empty?
|
137
|
+
|
138
|
+
matched_ids = klass.collection.distinct(:_id, query(options).criteria.to_hash)
|
139
|
+
matched_ids.sort_by! { |matched_id| ids.index(matched_id) }
|
140
|
+
end
|
141
|
+
|
123
142
|
def find_target
|
124
143
|
return [] if ids.blank?
|
125
144
|
all
|
@@ -128,7 +147,18 @@ module MongoMapper
|
|
128
147
|
def ids
|
129
148
|
proxy_owner[options[:in]]
|
130
149
|
end
|
150
|
+
|
151
|
+
def order_results(objects)
|
152
|
+
return objects if !ordered?
|
153
|
+
return objects unless objects.respond_to?(:to_a) && objects.respond_to?(:sort_by)
|
154
|
+
objects.sort_by { |obj| ids.index(obj.id) }
|
155
|
+
end
|
156
|
+
|
157
|
+
def ordered?
|
158
|
+
association.ordered?
|
159
|
+
end
|
160
|
+
|
131
161
|
end
|
132
162
|
end
|
133
163
|
end
|
134
|
-
end
|
164
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class InForeignArrayProxy < Collection
|
6
|
+
include DynamicQuerying::ClassMethods
|
7
|
+
|
8
|
+
def find(*args)
|
9
|
+
query.find(*scoped_ids(args))
|
10
|
+
end
|
11
|
+
|
12
|
+
def find!(*args)
|
13
|
+
query.find!(*scoped_ids(args))
|
14
|
+
end
|
15
|
+
|
16
|
+
def paginate(options)
|
17
|
+
query.paginate(options)
|
18
|
+
end
|
19
|
+
|
20
|
+
def all(options={})
|
21
|
+
query(options).all
|
22
|
+
end
|
23
|
+
|
24
|
+
def first(options={})
|
25
|
+
query(options).first
|
26
|
+
end
|
27
|
+
|
28
|
+
def last(options={})
|
29
|
+
query(options).last
|
30
|
+
end
|
31
|
+
|
32
|
+
def count(options={})
|
33
|
+
query(options).count
|
34
|
+
end
|
35
|
+
|
36
|
+
def destroy_all(options={})
|
37
|
+
all(options).each do |doc|
|
38
|
+
doc.destroy
|
39
|
+
end
|
40
|
+
reset
|
41
|
+
end
|
42
|
+
|
43
|
+
def delete_all(options={})
|
44
|
+
docs = query(options).fields(:_id).all
|
45
|
+
klass.delete(docs.map { |d| d.id })
|
46
|
+
reset
|
47
|
+
end
|
48
|
+
|
49
|
+
def nullify
|
50
|
+
replace([])
|
51
|
+
reset
|
52
|
+
end
|
53
|
+
|
54
|
+
def create(attrs={})
|
55
|
+
doc = klass.create(attrs)
|
56
|
+
if doc.persisted?
|
57
|
+
inverse_association(doc) << proxy_owner
|
58
|
+
doc.save
|
59
|
+
reset
|
60
|
+
end
|
61
|
+
doc
|
62
|
+
end
|
63
|
+
|
64
|
+
def create!(attrs={})
|
65
|
+
doc = klass.create!(attrs)
|
66
|
+
|
67
|
+
if doc.persisted?
|
68
|
+
inverse_association(doc) << proxy_owner
|
69
|
+
doc.save
|
70
|
+
reset
|
71
|
+
end
|
72
|
+
doc
|
73
|
+
end
|
74
|
+
|
75
|
+
def <<(*docs)
|
76
|
+
flatten_deeper(docs).each do |doc|
|
77
|
+
inverse_association(doc) << proxy_owner
|
78
|
+
doc.save
|
79
|
+
end
|
80
|
+
reset
|
81
|
+
end
|
82
|
+
alias_method :push, :<<
|
83
|
+
alias_method :concat, :<<
|
84
|
+
|
85
|
+
def replace(docs)
|
86
|
+
doc_ids = docs.map do |doc|
|
87
|
+
doc.save unless doc.persisted?
|
88
|
+
inverse_association(doc) << proxy_owner
|
89
|
+
doc.save
|
90
|
+
doc.id
|
91
|
+
end
|
92
|
+
|
93
|
+
replace_selector = { options[:from] => proxy_owner.id }
|
94
|
+
unless doc_ids.empty?
|
95
|
+
replace_selector[:_id] = {"$not" => {"$in" => doc_ids}}
|
96
|
+
end
|
97
|
+
|
98
|
+
klass.collection.update_many(replace_selector, {
|
99
|
+
"$pull" => { options[:from] => proxy_owner.id }
|
100
|
+
})
|
101
|
+
|
102
|
+
reset
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
def query(options={})
|
108
|
+
klass.query({}.
|
109
|
+
merge(association.query_options).
|
110
|
+
merge(options).
|
111
|
+
merge(criteria))
|
112
|
+
end
|
113
|
+
|
114
|
+
def criteria
|
115
|
+
{options[:from] => proxy_owner.id}
|
116
|
+
end
|
117
|
+
|
118
|
+
def scoped_ids(args)
|
119
|
+
valid = args.flatten.map do |id|
|
120
|
+
id = ObjectId.to_mongo(id) if klass.using_object_id?
|
121
|
+
id
|
122
|
+
end
|
123
|
+
valid.empty? ? nil : valid
|
124
|
+
end
|
125
|
+
|
126
|
+
def find_target
|
127
|
+
all
|
128
|
+
end
|
129
|
+
|
130
|
+
def inverse_association(doc)
|
131
|
+
doc.send(options[:as].to_s.pluralize)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -17,6 +17,8 @@ module MongoMapper
|
|
17
17
|
ManyPolymorphicProxy
|
18
18
|
elsif as?
|
19
19
|
ManyDocumentsAsProxy
|
20
|
+
elsif in_foreign_array?
|
21
|
+
InForeignArrayProxy
|
20
22
|
elsif in_array?
|
21
23
|
InArrayProxy
|
22
24
|
else
|
@@ -26,7 +28,7 @@ module MongoMapper
|
|
26
28
|
end
|
27
29
|
|
28
30
|
def setup(model)
|
29
|
-
model.associations_module.module_eval
|
31
|
+
model.associations_module.module_eval(<<-end_eval, __FILE__, __LINE__ + 1)
|
30
32
|
def #{name}
|
31
33
|
get_proxy(associations[#{name.inspect}])
|
32
34
|
end
|
@@ -60,4 +62,4 @@ module MongoMapper
|
|
60
62
|
end
|
61
63
|
end
|
62
64
|
end
|
63
|
-
end
|
65
|
+
end
|
@@ -13,7 +13,6 @@ module MongoMapper
|
|
13
13
|
|
14
14
|
attr_reader :proxy_owner, :association, :target
|
15
15
|
|
16
|
-
alias :proxy_target :target
|
17
16
|
alias :proxy_association :association
|
18
17
|
|
19
18
|
def_delegators :proxy_association, :klass, :options
|
@@ -100,6 +99,11 @@ module MongoMapper
|
|
100
99
|
end
|
101
100
|
end
|
102
101
|
|
102
|
+
def read
|
103
|
+
load_target
|
104
|
+
@target
|
105
|
+
end
|
106
|
+
|
103
107
|
protected
|
104
108
|
|
105
109
|
def load_target
|
@@ -5,10 +5,11 @@ module MongoMapper
|
|
5
5
|
class SingleAssociation < Base
|
6
6
|
def setup(model)
|
7
7
|
@model = model
|
8
|
-
|
8
|
+
|
9
|
+
model.associations_module.module_eval(<<-end_eval, __FILE__, __LINE__ + 1)
|
9
10
|
def #{name}
|
10
11
|
proxy = get_proxy(associations[#{name.inspect}])
|
11
|
-
proxy.nil? ? nil : proxy
|
12
|
+
proxy.nil? ? nil : proxy.read
|
12
13
|
end
|
13
14
|
|
14
15
|
def #{name}=(value)
|
@@ -20,7 +21,7 @@ module MongoMapper
|
|
20
21
|
end
|
21
22
|
|
22
23
|
proxy.replace(value)
|
23
|
-
|
24
|
+
proxy.read
|
24
25
|
end
|
25
26
|
|
26
27
|
def #{name}?
|
@@ -43,4 +44,4 @@ module MongoMapper
|
|
43
44
|
end
|
44
45
|
end
|
45
46
|
end
|
46
|
-
end
|
47
|
+
end
|