mongo_mapper 0.8.6 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/UPGRADES +10 -0
- data/bin/mmconsole +0 -1
- data/examples/identity_map/automatic.rb +1 -7
- data/examples/plugins.rb +9 -9
- data/examples/safe.rb +43 -0
- data/lib/mongo_mapper.rb +46 -33
- data/lib/mongo_mapper/document.rb +33 -32
- data/lib/mongo_mapper/embedded_document.rb +22 -22
- data/lib/mongo_mapper/locale/en.yml +5 -0
- data/lib/mongo_mapper/middleware/identity_map.rb +16 -0
- data/lib/mongo_mapper/plugins.rb +16 -3
- data/lib/mongo_mapper/plugins/accessible.rb +2 -0
- data/lib/mongo_mapper/plugins/active_model.rb +18 -0
- data/lib/mongo_mapper/plugins/associations.rb +37 -42
- data/lib/mongo_mapper/plugins/associations/base.rb +14 -50
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +58 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +6 -1
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +30 -2
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +4 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +12 -6
- data/lib/mongo_mapper/plugins/associations/many_association.rb +67 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +5 -5
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/one_association.rb +20 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +5 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +7 -7
- data/lib/mongo_mapper/plugins/associations/proxy.rb +2 -2
- data/lib/mongo_mapper/plugins/caching.rb +3 -1
- data/lib/mongo_mapper/plugins/callbacks.rb +12 -221
- data/lib/mongo_mapper/plugins/clone.rb +3 -1
- data/lib/mongo_mapper/plugins/dirty.rb +38 -91
- data/lib/mongo_mapper/plugins/document.rb +4 -2
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +2 -0
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +43 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +16 -9
- data/lib/mongo_mapper/plugins/equality.rb +2 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +4 -2
- data/lib/mongo_mapper/plugins/indexes.rb +2 -0
- data/lib/mongo_mapper/plugins/inspect.rb +3 -1
- data/lib/mongo_mapper/plugins/keys.rb +28 -22
- data/lib/mongo_mapper/plugins/keys/key.rb +12 -6
- data/lib/mongo_mapper/plugins/logger.rb +2 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +3 -1
- data/lib/mongo_mapper/plugins/pagination.rb +2 -0
- data/lib/mongo_mapper/plugins/persistence.rb +2 -0
- data/lib/mongo_mapper/plugins/protected.rb +2 -0
- data/lib/mongo_mapper/plugins/querying.rb +5 -4
- data/lib/mongo_mapper/plugins/rails.rb +3 -5
- data/lib/mongo_mapper/plugins/safe.rb +2 -0
- data/lib/mongo_mapper/plugins/sci.rb +2 -0
- data/lib/mongo_mapper/plugins/scopes.rb +2 -0
- data/lib/mongo_mapper/plugins/serialization.rb +67 -46
- data/lib/mongo_mapper/plugins/timestamps.rb +3 -1
- data/lib/mongo_mapper/plugins/userstamps.rb +2 -0
- data/lib/mongo_mapper/plugins/validations.rb +40 -24
- data/lib/mongo_mapper/railtie.rb +49 -0
- data/lib/mongo_mapper/railtie/database.rake +60 -0
- data/lib/mongo_mapper/support/descendant_appends.rb +11 -11
- data/lib/mongo_mapper/translation.rb +10 -0
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/rails/generators/mongo_mapper/config/config_generator.rb +24 -0
- data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +18 -0
- data/lib/rails/generators/mongo_mapper/model/model_generator.rb +23 -0
- data/lib/rails/generators/mongo_mapper/model/templates/model.rb +11 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +1 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +131 -1
- data/test/functional/associations/test_in_array_proxy.rb +30 -0
- data/test/functional/associations/test_many_documents_proxy.rb +30 -2
- data/test/functional/associations/test_many_embedded_proxy.rb +33 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +1 -0
- data/test/functional/associations/test_one_embedded_proxy.rb +21 -2
- data/test/functional/associations/test_one_proxy.rb +49 -9
- data/test/functional/test_associations.rb +2 -0
- data/test/functional/test_caching.rb +3 -2
- data/test/functional/test_callbacks.rb +25 -18
- data/test/functional/test_dirty.rb +123 -1
- data/test/functional/test_document.rb +26 -2
- data/test/functional/test_embedded_document.rb +68 -2
- data/test/functional/test_identity_map.rb +3 -4
- data/test/functional/test_querying.rb +11 -0
- data/test/functional/test_userstamps.rb +2 -2
- data/test/functional/test_validations.rb +31 -29
- data/test/models.rb +10 -0
- data/test/test_active_model_lint.rb +1 -1
- data/test/test_helper.rb +9 -10
- data/test/unit/associations/test_base.rb +24 -100
- data/test/unit/associations/test_belongs_to_association.rb +29 -0
- data/test/unit/associations/test_many_association.rb +63 -0
- data/test/unit/associations/test_one_association.rb +18 -0
- data/test/unit/serializers/test_json_serializer.rb +0 -1
- data/test/unit/test_descendant_appends.rb +8 -16
- data/test/unit/test_document.rb +4 -9
- data/test/unit/test_dynamic_finder.rb +1 -1
- data/test/unit/test_embedded_document.rb +51 -18
- data/test/unit/test_identity_map_middleware.rb +34 -0
- data/test/unit/test_inspect.rb +22 -0
- data/test/unit/test_key.rb +21 -1
- data/test/unit/test_keys.rb +0 -2
- data/test/unit/test_plugins.rb +106 -20
- data/test/unit/test_rails.rb +8 -8
- data/test/unit/test_serialization.rb +116 -1
- data/test/unit/test_translation.rb +27 -0
- data/test/unit/test_validations.rb +66 -81
- metadata +103 -43
- data/examples/identity_map/middleware.rb +0 -14
- data/lib/mongo_mapper/plugins/descendants.rb +0 -17
- data/rails/init.rb +0 -19
data/UPGRADES
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
0.8.6 => 0.9
|
2
|
+
* [attribute]_before_typecast should become [attribute]_before_type_cast (note the extra _)
|
3
|
+
* Change validates_exclusion_of :within option to :in
|
4
|
+
* Change validates_inclusion_of :within option to :in
|
5
|
+
* descendants now returns all descendants use direct_descendants for just children
|
6
|
+
* Switch validate_on_create and validate_on_update use validate :on => :create syntax
|
7
|
+
* Added #update_attribute
|
8
|
+
* belongs_to and one associations now return a true nil instead of a proxy to nil, so you must now call .build_thing instead of thing.build.
|
9
|
+
* Plugins should now extend ActiveSupport::Concern
|
10
|
+
|
1
11
|
0.7.6 => 0.8
|
2
12
|
* Proxy#owner has been removed in favor of Proxy#proxy_owner
|
3
13
|
* @new instance variable renamed to @_new (you shouldn't be using this anyway)
|
data/bin/mmconsole
CHANGED
@@ -1,8 +1,2 @@
|
|
1
1
|
# Using the following will turn on identity map for all models
|
2
|
-
|
3
|
-
def self.included(model)
|
4
|
-
model.plugin MongoMapper::Plugins::IdentityMap
|
5
|
-
end
|
6
|
-
end
|
7
|
-
|
8
|
-
MongoMapper::Document.append_inclusions(IdentityMapAddition)
|
2
|
+
MongoMapper::Document.plugin(MongoMapper::Plugins::IdentityMap)
|
data/examples/plugins.rb
CHANGED
@@ -4,9 +4,11 @@ require 'pp'
|
|
4
4
|
|
5
5
|
MongoMapper.database = 'testing'
|
6
6
|
|
7
|
-
# To create your own plugin, just create a module
|
7
|
+
# To create your own plugin, just create a module that
|
8
|
+
# extends ActiveSupport::Concern.
|
8
9
|
module FooPlugin
|
9
|
-
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
10
12
|
# ClassMethods module will automatically get extended
|
11
13
|
module ClassMethods
|
12
14
|
def foo
|
@@ -21,14 +23,12 @@ module FooPlugin
|
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
24
|
-
#
|
25
|
-
#
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
model.key :foo, String
|
26
|
+
# Any configuration can be done in the #included block, which gets
|
27
|
+
# class evaled. Feel free to add keys, validations, or anything else.
|
28
|
+
included do
|
29
|
+
puts "Configuring #{self}..."
|
30
|
+
key :foo, String
|
30
31
|
end
|
31
|
-
|
32
32
|
end
|
33
33
|
|
34
34
|
class User
|
data/examples/safe.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
require 'mongo_mapper'
|
3
|
+
MongoMapper.database = 'testing'
|
4
|
+
|
5
|
+
class User
|
6
|
+
include MongoMapper::Document
|
7
|
+
key :email, String
|
8
|
+
end
|
9
|
+
|
10
|
+
# Drop collection and ensure unique index on email
|
11
|
+
User.collection.drop
|
12
|
+
User.ensure_index(:email, :unique => true)
|
13
|
+
|
14
|
+
User.create(:email => 'nunemaker@gmail.com')
|
15
|
+
User.create(:email => 'nunemaker@gmail.com')
|
16
|
+
|
17
|
+
puts "Count should only be one since the second create failed, count is: #{User.count}" # 1 because second was not created, but no exception raised
|
18
|
+
puts
|
19
|
+
|
20
|
+
# save method also takes options, including :safe
|
21
|
+
# which will force raise when duplicate is hit
|
22
|
+
begin
|
23
|
+
user = User.new(:email => 'nunemaker@gmail.com')
|
24
|
+
user.save(:safe => true)
|
25
|
+
rescue Mongo::OperationFailure => e
|
26
|
+
puts 'Mongo Operation failure raised because duplicate email was entered'
|
27
|
+
puts e.inspect
|
28
|
+
puts
|
29
|
+
end
|
30
|
+
|
31
|
+
# Mark user model as safe, same as doing this...
|
32
|
+
# class User
|
33
|
+
# include MongoMapper::Document
|
34
|
+
# safe
|
35
|
+
# end
|
36
|
+
User.safe
|
37
|
+
|
38
|
+
begin
|
39
|
+
User.create(:email => 'nunemaker@gmail.com')
|
40
|
+
rescue Mongo::OperationFailure => e
|
41
|
+
puts 'Mongo Operation failure raised because duplicate email was entered'
|
42
|
+
puts e.inspect
|
43
|
+
end
|
data/lib/mongo_mapper.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
# Make sure you have the correct versions of the gems (see gemspec) in your load path.
|
3
2
|
require 'plucky'
|
4
|
-
require '
|
5
|
-
require '
|
3
|
+
require 'active_support/core_ext'
|
4
|
+
require 'active_model'
|
5
|
+
require "mongo_mapper/railtie" if defined?(Rails)
|
6
|
+
|
7
|
+
I18n.load_path << File.expand_path('../mongo_mapper/locale/en.yml', __FILE__)
|
6
8
|
|
7
9
|
module MongoMapper
|
8
10
|
autoload :Connection, 'mongo_mapper/connection'
|
@@ -16,43 +18,52 @@ module MongoMapper
|
|
16
18
|
autoload :Document, 'mongo_mapper/document'
|
17
19
|
autoload :EmbeddedDocument, 'mongo_mapper/embedded_document'
|
18
20
|
autoload :Plugins, 'mongo_mapper/plugins'
|
21
|
+
autoload :Translation, 'mongo_mapper/translation'
|
19
22
|
autoload :Version, 'mongo_mapper/version'
|
20
23
|
|
24
|
+
module Middleware
|
25
|
+
autoload :IdentityMap, 'mongo_mapper/middleware/identity_map'
|
26
|
+
end
|
27
|
+
|
21
28
|
module Plugins
|
22
|
-
autoload :
|
23
|
-
autoload :
|
24
|
-
autoload :
|
25
|
-
autoload :
|
26
|
-
autoload :
|
27
|
-
autoload :
|
28
|
-
autoload :Dirty,
|
29
|
-
autoload :Document,
|
30
|
-
autoload :DynamicQuerying,
|
31
|
-
autoload :
|
32
|
-
autoload :
|
33
|
-
autoload :
|
34
|
-
autoload :
|
35
|
-
autoload :
|
36
|
-
autoload :
|
37
|
-
autoload :
|
38
|
-
autoload :
|
39
|
-
autoload :
|
40
|
-
autoload :
|
41
|
-
autoload :
|
42
|
-
autoload :
|
43
|
-
autoload :
|
44
|
-
autoload :
|
45
|
-
autoload :
|
46
|
-
autoload :
|
47
|
-
autoload :
|
48
|
-
autoload :
|
49
|
-
autoload :
|
50
|
-
autoload :
|
29
|
+
autoload :ActiveModel, 'mongo_mapper/plugins/active_model'
|
30
|
+
autoload :Associations, 'mongo_mapper/plugins/associations'
|
31
|
+
autoload :Accessible, 'mongo_mapper/plugins/accessible'
|
32
|
+
autoload :Callbacks, 'mongo_mapper/plugins/callbacks'
|
33
|
+
autoload :Caching, 'mongo_mapper/plugins/caching'
|
34
|
+
autoload :Clone, 'mongo_mapper/plugins/clone'
|
35
|
+
autoload :Dirty, 'mongo_mapper/plugins/dirty'
|
36
|
+
autoload :Document, 'mongo_mapper/plugins/document'
|
37
|
+
autoload :DynamicQuerying, 'mongo_mapper/plugins/dynamic_querying'
|
38
|
+
autoload :EmbeddedCallbacks, 'mongo_mapper/plugins/embedded_callbacks'
|
39
|
+
autoload :EmbeddedDocument, 'mongo_mapper/plugins/embedded_document'
|
40
|
+
autoload :Equality, 'mongo_mapper/plugins/equality'
|
41
|
+
autoload :IdentityMap, 'mongo_mapper/plugins/identity_map'
|
42
|
+
autoload :Inspect, 'mongo_mapper/plugins/inspect'
|
43
|
+
autoload :Indexes, 'mongo_mapper/plugins/indexes'
|
44
|
+
autoload :Keys, 'mongo_mapper/plugins/keys'
|
45
|
+
autoload :Logger, 'mongo_mapper/plugins/logger'
|
46
|
+
autoload :Modifiers, 'mongo_mapper/plugins/modifiers'
|
47
|
+
autoload :Pagination, 'mongo_mapper/plugins/pagination'
|
48
|
+
autoload :Persistence, 'mongo_mapper/plugins/persistence'
|
49
|
+
autoload :Protected, 'mongo_mapper/plugins/protected'
|
50
|
+
autoload :Querying, 'mongo_mapper/plugins/querying'
|
51
|
+
autoload :Rails, 'mongo_mapper/plugins/rails'
|
52
|
+
autoload :Safe, 'mongo_mapper/plugins/safe'
|
53
|
+
autoload :Sci, 'mongo_mapper/plugins/sci'
|
54
|
+
autoload :Scopes, 'mongo_mapper/plugins/scopes'
|
55
|
+
autoload :Serialization, 'mongo_mapper/plugins/serialization'
|
56
|
+
autoload :Timestamps, 'mongo_mapper/plugins/timestamps'
|
57
|
+
autoload :Userstamps, 'mongo_mapper/plugins/userstamps'
|
58
|
+
autoload :Validations, 'mongo_mapper/plugins/validations'
|
51
59
|
|
52
60
|
module Associations
|
53
61
|
autoload :Base, 'mongo_mapper/plugins/associations/base'
|
54
62
|
autoload :Collection, 'mongo_mapper/plugins/associations/collection'
|
55
63
|
autoload :EmbeddedCollection, 'mongo_mapper/plugins/associations/embedded_collection'
|
64
|
+
autoload :ManyAssociation, 'mongo_mapper/plugins/associations/many_association'
|
65
|
+
autoload :BelongsToAssociation, 'mongo_mapper/plugins/associations/belongs_to_association'
|
66
|
+
autoload :OneAssociation, 'mongo_mapper/plugins/associations/one_association'
|
56
67
|
autoload :ManyDocumentsProxy, 'mongo_mapper/plugins/associations/many_documents_proxy'
|
57
68
|
autoload :BelongsToProxy, 'mongo_mapper/plugins/associations/belongs_to_proxy'
|
58
69
|
autoload :BelongsToPolymorphicProxy, 'mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy'
|
@@ -76,4 +87,6 @@ end
|
|
76
87
|
require 'mongo_mapper/support/descendant_appends'
|
77
88
|
|
78
89
|
# FIXME: autoload with proxy is failing, need to investigate
|
79
|
-
require 'mongo_mapper/plugins/associations/proxy'
|
90
|
+
require 'mongo_mapper/plugins/associations/proxy'
|
91
|
+
|
92
|
+
ActiveSupport.run_load_hooks(:mongo_mapper, MongoMapper)
|
@@ -1,41 +1,42 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
module MongoMapper
|
3
3
|
module Document
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
extend Plugins
|
4
6
|
extend Support::DescendantAppends
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
include Plugins::ActiveModel
|
9
|
+
include Plugins::Document
|
10
|
+
include Plugins::Querying # for now needs to be before associations (save_to_collection)
|
11
|
+
include Plugins::Associations
|
12
|
+
include Plugins::Caching
|
13
|
+
include Plugins::Clone
|
14
|
+
include Plugins::DynamicQuerying
|
15
|
+
include Plugins::Equality
|
16
|
+
include Plugins::Inspect
|
17
|
+
include Plugins::Indexes
|
18
|
+
include Plugins::Keys
|
19
|
+
include Plugins::Dirty # for now dirty needs to be after keys
|
20
|
+
include Plugins::Logger
|
21
|
+
include Plugins::Modifiers
|
22
|
+
include Plugins::Pagination
|
23
|
+
include Plugins::Persistence
|
24
|
+
include Plugins::Accessible
|
25
|
+
include Plugins::Protected
|
26
|
+
include Plugins::Rails
|
27
|
+
include Plugins::Safe # needs to be after querying (save_to_collection)
|
28
|
+
include Plugins::Sci
|
29
|
+
include Plugins::Scopes
|
30
|
+
include Plugins::Serialization
|
31
|
+
include Plugins::Timestamps
|
32
|
+
include Plugins::Userstamps
|
33
|
+
include Plugins::Validations
|
34
|
+
include Plugins::EmbeddedCallbacks
|
35
|
+
include Plugins::Callbacks # for now callbacks needs to be after validations
|
9
36
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
plugin Plugins::Caching
|
14
|
-
plugin Plugins::Clone
|
15
|
-
plugin Plugins::Descendants
|
16
|
-
plugin Plugins::DynamicQuerying
|
17
|
-
plugin Plugins::Equality
|
18
|
-
plugin Plugins::Inspect
|
19
|
-
plugin Plugins::Indexes
|
20
|
-
plugin Plugins::Keys
|
21
|
-
plugin Plugins::Dirty # for now dirty needs to be after keys
|
22
|
-
plugin Plugins::Logger
|
23
|
-
plugin Plugins::Modifiers
|
24
|
-
plugin Plugins::Pagination
|
25
|
-
plugin Plugins::Persistence
|
26
|
-
plugin Plugins::Accessible
|
27
|
-
plugin Plugins::Protected
|
28
|
-
plugin Plugins::Rails
|
29
|
-
plugin Plugins::Safe # needs to be after querying (save_to_collection)
|
30
|
-
plugin Plugins::Sci
|
31
|
-
plugin Plugins::Scopes
|
32
|
-
plugin Plugins::Serialization
|
33
|
-
plugin Plugins::Timestamps
|
34
|
-
plugin Plugins::Userstamps
|
35
|
-
plugin Plugins::Validations
|
36
|
-
plugin Plugins::Callbacks # for now callbacks needs to be after validations
|
37
|
-
end
|
38
|
-
super
|
37
|
+
included do
|
38
|
+
extend Plugins
|
39
|
+
extend Translation
|
39
40
|
end
|
40
41
|
end # Document
|
41
42
|
end # MongoMapper
|
@@ -2,30 +2,30 @@
|
|
2
2
|
module MongoMapper
|
3
3
|
module EmbeddedDocument
|
4
4
|
extend Support::DescendantAppends
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
extend Plugins
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
include Plugins::ActiveModel
|
9
|
+
include Plugins::EmbeddedDocument
|
10
|
+
include Plugins::Associations
|
11
|
+
include Plugins::Caching
|
12
|
+
include Plugins::Clone
|
13
|
+
include Plugins::Equality
|
14
|
+
include Plugins::Inspect
|
15
|
+
include Plugins::Keys
|
16
|
+
include Plugins::Logger
|
17
|
+
include Plugins::Persistence
|
18
|
+
include Plugins::Accessible
|
19
|
+
include Plugins::Protected
|
20
|
+
include Plugins::Rails
|
21
|
+
include Plugins::Sci
|
22
|
+
include Plugins::Serialization
|
23
|
+
include Plugins::Validations
|
24
|
+
include Plugins::EmbeddedCallbacks
|
9
25
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
plugin Plugins::Clone
|
14
|
-
plugin Plugins::Descendants
|
15
|
-
plugin Plugins::Equality
|
16
|
-
plugin Plugins::Inspect
|
17
|
-
plugin Plugins::Keys
|
18
|
-
plugin Plugins::Logger
|
19
|
-
plugin Plugins::Persistence
|
20
|
-
plugin Plugins::Accessible
|
21
|
-
plugin Plugins::Protected
|
22
|
-
plugin Plugins::Rails
|
23
|
-
plugin Plugins::Sci
|
24
|
-
plugin Plugins::Serialization
|
25
|
-
plugin Plugins::Validations
|
26
|
-
plugin Plugins::Callbacks
|
27
|
-
end
|
28
|
-
super
|
26
|
+
included do
|
27
|
+
extend Plugins
|
28
|
+
extend Translation
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Middleware
|
3
|
+
class IdentityMap
|
4
|
+
def initialize(app)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
MongoMapper::Plugins::IdentityMap.clear
|
10
|
+
@app.call(env)
|
11
|
+
ensure
|
12
|
+
MongoMapper::Plugins::IdentityMap.clear
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/mongo_mapper/plugins.rb
CHANGED
@@ -1,15 +1,28 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
module MongoMapper
|
3
3
|
module Plugins
|
4
|
+
include ActiveSupport::DescendantsTracker
|
5
|
+
|
4
6
|
def plugins
|
5
7
|
@plugins ||= []
|
6
8
|
end
|
7
9
|
|
8
10
|
def plugin(mod)
|
9
|
-
|
10
|
-
|
11
|
-
|
11
|
+
if ActiveSupport::Concern === mod
|
12
|
+
include mod
|
13
|
+
else
|
14
|
+
warn "[DEPRECATED] Plugins must extend ActiveSupport::Concern"
|
15
|
+
extend mod::ClassMethods if mod.const_defined?(:ClassMethods)
|
16
|
+
include mod::InstanceMethods if mod.const_defined?(:InstanceMethods)
|
17
|
+
mod.configure(self) if mod.respond_to?(:configure)
|
18
|
+
end
|
19
|
+
direct_descendants.each {|model| model.send(:include, mod) }
|
12
20
|
plugins << mod
|
13
21
|
end
|
22
|
+
|
23
|
+
def included(base = nil, &block)
|
24
|
+
direct_descendants << base if base
|
25
|
+
super
|
26
|
+
end
|
14
27
|
end
|
15
28
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module ActiveModel
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
include ::ActiveModel::Naming
|
8
|
+
include ::ActiveModel::Conversion
|
9
|
+
include ::ActiveModel::Serialization
|
10
|
+
include ::ActiveModel::Serializers::Xml
|
11
|
+
include ::ActiveModel::Serializers::JSON
|
12
|
+
|
13
|
+
included do
|
14
|
+
extend ::ActiveModel::Translation
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -2,6 +2,8 @@
|
|
2
2
|
module MongoMapper
|
3
3
|
module Plugins
|
4
4
|
module Associations
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
5
7
|
module ClassMethods
|
6
8
|
def inherited(subclass)
|
7
9
|
subclass.associations = associations.dup
|
@@ -9,57 +11,48 @@ module MongoMapper
|
|
9
11
|
end
|
10
12
|
|
11
13
|
def belongs_to(association_id, options={}, &extension)
|
12
|
-
create_association(
|
14
|
+
create_association(BelongsToAssociation.new(association_id, options, &extension))
|
13
15
|
end
|
14
16
|
|
15
17
|
def many(association_id, options={}, &extension)
|
16
|
-
create_association(
|
18
|
+
create_association(ManyAssociation.new(association_id, options, &extension))
|
17
19
|
end
|
18
20
|
|
19
21
|
def one(association_id, options={}, &extension)
|
20
|
-
create_association(
|
22
|
+
create_association(OneAssociation.new(association_id, options, &extension))
|
21
23
|
end
|
22
24
|
|
23
25
|
def associations
|
24
|
-
@associations ||=
|
26
|
+
@associations ||= {}
|
25
27
|
end
|
26
28
|
|
27
29
|
def associations=(hash)
|
28
30
|
@associations = hash
|
29
31
|
end
|
30
32
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
define_method("#{association.name}=") do |value|
|
41
|
-
get_proxy(association).replace(value)
|
42
|
-
value
|
43
|
-
end
|
33
|
+
def associations_module_defined?
|
34
|
+
if method(:const_defined?).arity == 1 # Ruby 1.9 compat check
|
35
|
+
const_defined?('MongoMapperAssociations')
|
36
|
+
else
|
37
|
+
const_defined?('MongoMapperAssociations', false)
|
38
|
+
end
|
39
|
+
end
|
44
40
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
41
|
+
def associations_module
|
42
|
+
if associations_module_defined?
|
43
|
+
const_get 'MongoMapperAssociations'
|
44
|
+
else
|
45
|
+
Module.new.tap do |m|
|
46
|
+
const_set 'MongoMapperAssociations', m
|
47
|
+
include m
|
49
48
|
end
|
49
|
+
end
|
50
|
+
end
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
doc.get_proxy(association).destroy_all
|
56
|
-
when :delete_all
|
57
|
-
doc.get_proxy(association).delete_all
|
58
|
-
when :nullify
|
59
|
-
doc.get_proxy(association).nullify
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
52
|
+
private
|
53
|
+
def create_association(association)
|
54
|
+
associations[association.name] = association
|
55
|
+
association.setup(self)
|
63
56
|
end
|
64
57
|
end
|
65
58
|
|
@@ -69,26 +62,28 @@ module MongoMapper
|
|
69
62
|
end
|
70
63
|
|
71
64
|
def embedded_associations
|
72
|
-
associations.select
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
65
|
+
associations.values.select { |assoc| assoc.embeddable? }
|
66
|
+
end
|
67
|
+
|
68
|
+
def build_proxy(association)
|
69
|
+
proxy = association.proxy_class.new(self, association)
|
70
|
+
self.instance_variable_set(association.ivar, proxy)
|
71
|
+
|
72
|
+
proxy
|
77
73
|
end
|
78
74
|
|
79
75
|
def get_proxy(association)
|
80
76
|
unless proxy = self.instance_variable_get(association.ivar)
|
81
|
-
proxy =
|
82
|
-
self.instance_variable_set(association.ivar, proxy)
|
77
|
+
proxy = build_proxy(association)
|
83
78
|
end
|
84
79
|
proxy
|
85
80
|
end
|
86
81
|
|
87
82
|
def save_to_collection(options={})
|
88
|
-
super
|
83
|
+
super if defined?(super)
|
89
84
|
associations.each do |association_name, association|
|
90
85
|
proxy = get_proxy(association)
|
91
|
-
proxy.save_to_collection(options) if proxy.proxy_respond_to?(:save_to_collection)
|
86
|
+
proxy.save_to_collection(options) if proxy.proxy_respond_to?(:save_to_collection) && association.autosave?
|
92
87
|
end
|
93
88
|
end
|
94
89
|
end
|