mongo_mapper-unstable 2010.3.8 → 2010.06.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. data/README.rdoc +4 -8
  2. data/bin/mmconsole +1 -1
  3. data/examples/keys.rb +37 -0
  4. data/examples/plugins.rb +41 -0
  5. data/examples/querying.rb +35 -0
  6. data/examples/scopes.rb +52 -0
  7. data/lib/mongo_mapper/connection.rb +83 -0
  8. data/lib/mongo_mapper/document.rb +11 -329
  9. data/lib/mongo_mapper/embedded_document.rb +9 -38
  10. data/lib/mongo_mapper/exceptions.rb +30 -0
  11. data/lib/mongo_mapper/extensions/array.rb +19 -0
  12. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  13. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  14. data/lib/mongo_mapper/extensions/date.rb +25 -0
  15. data/lib/mongo_mapper/extensions/float.rb +14 -0
  16. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  17. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  18. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  19. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  20. data/lib/mongo_mapper/extensions/object.rb +27 -0
  21. data/lib/mongo_mapper/extensions/object_id.rb +30 -0
  22. data/lib/mongo_mapper/extensions/set.rb +20 -0
  23. data/lib/mongo_mapper/extensions/string.rb +18 -0
  24. data/lib/mongo_mapper/extensions/time.rb +29 -0
  25. data/lib/mongo_mapper/plugins/accessible.rb +44 -0
  26. data/lib/mongo_mapper/plugins/associations/base.rb +7 -6
  27. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +5 -6
  28. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +5 -6
  29. data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
  30. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +2 -1
  31. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +25 -39
  32. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +4 -4
  33. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +36 -46
  34. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
  35. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +5 -4
  36. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
  37. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
  38. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +7 -7
  39. data/lib/mongo_mapper/plugins/associations/proxy.rb +16 -8
  40. data/lib/mongo_mapper/plugins/associations.rb +14 -22
  41. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  42. data/lib/mongo_mapper/plugins/callbacks.rb +17 -5
  43. data/lib/mongo_mapper/plugins/clone.rb +10 -4
  44. data/lib/mongo_mapper/plugins/descendants.rb +3 -2
  45. data/lib/mongo_mapper/plugins/dirty.rb +1 -0
  46. data/lib/mongo_mapper/plugins/document.rb +41 -0
  47. data/lib/mongo_mapper/{support/find.rb → plugins/dynamic_querying/dynamic_finder.rb} +3 -36
  48. data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
  49. data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
  50. data/lib/mongo_mapper/plugins/equality.rb +4 -10
  51. data/lib/mongo_mapper/plugins/identity_map.rb +29 -23
  52. data/lib/mongo_mapper/plugins/indexes.rb +12 -0
  53. data/lib/mongo_mapper/plugins/inspect.rb +1 -0
  54. data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
  55. data/lib/mongo_mapper/plugins/keys.rb +85 -110
  56. data/lib/mongo_mapper/plugins/logger.rb +1 -0
  57. data/lib/mongo_mapper/plugins/modifiers.rb +41 -16
  58. data/lib/mongo_mapper/plugins/pagination.rb +5 -15
  59. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  60. data/lib/mongo_mapper/plugins/protected.rb +9 -1
  61. data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
  62. data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
  63. data/lib/mongo_mapper/plugins/querying.rb +176 -0
  64. data/lib/mongo_mapper/plugins/rails.rb +6 -1
  65. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  66. data/lib/mongo_mapper/plugins/sci.rb +32 -0
  67. data/lib/mongo_mapper/plugins/scopes.rb +21 -0
  68. data/lib/mongo_mapper/plugins/serialization.rb +5 -4
  69. data/lib/mongo_mapper/plugins/timestamps.rb +2 -1
  70. data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
  71. data/lib/mongo_mapper/plugins/validations.rb +9 -5
  72. data/lib/mongo_mapper/plugins.rb +1 -20
  73. data/lib/mongo_mapper/support/descendant_appends.rb +5 -6
  74. data/lib/mongo_mapper/version.rb +4 -0
  75. data/lib/mongo_mapper.rb +71 -128
  76. data/test/{NOTE_ON_TESTING → _NOTE_ON_TESTING} +0 -0
  77. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +5 -5
  78. data/test/functional/associations/test_belongs_to_proxy.rb +13 -21
  79. data/test/functional/associations/test_in_array_proxy.rb +7 -9
  80. data/test/functional/associations/test_many_documents_as_proxy.rb +5 -5
  81. data/test/functional/associations/test_many_documents_proxy.rb +186 -64
  82. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +22 -22
  83. data/test/functional/associations/test_many_embedded_proxy.rb +32 -32
  84. data/test/functional/associations/test_many_polymorphic_proxy.rb +47 -47
  85. data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
  86. data/test/functional/associations/test_one_proxy.rb +70 -49
  87. data/test/functional/test_accessible.rb +168 -0
  88. data/test/functional/test_associations.rb +11 -11
  89. data/test/functional/test_binary.rb +5 -5
  90. data/test/functional/test_caching.rb +76 -0
  91. data/test/functional/test_callbacks.rb +104 -34
  92. data/test/functional/test_dirty.rb +16 -16
  93. data/test/functional/test_document.rb +12 -924
  94. data/test/functional/test_dynamic_querying.rb +75 -0
  95. data/test/functional/test_embedded_document.rb +88 -8
  96. data/test/functional/test_identity_map.rb +41 -43
  97. data/test/functional/{test_indexing.rb → test_indexes.rb} +3 -5
  98. data/test/functional/test_logger.rb +1 -1
  99. data/test/functional/test_modifiers.rb +275 -181
  100. data/test/functional/test_pagination.rb +13 -15
  101. data/test/functional/test_protected.rb +25 -11
  102. data/test/functional/test_querying.rb +873 -0
  103. data/test/functional/test_safe.rb +76 -0
  104. data/test/functional/test_sci.rb +230 -0
  105. data/test/functional/test_scopes.rb +171 -0
  106. data/test/functional/test_string_id_compatibility.rb +11 -11
  107. data/test/functional/test_timestamps.rb +0 -2
  108. data/test/functional/test_userstamps.rb +0 -1
  109. data/test/functional/test_validations.rb +44 -31
  110. data/test/models.rb +18 -17
  111. data/test/{active_model_lint_test.rb → test_active_model_lint.rb} +3 -1
  112. data/test/test_helper.rb +59 -16
  113. data/test/unit/associations/test_base.rb +47 -42
  114. data/test/unit/associations/test_proxy.rb +15 -15
  115. data/test/unit/serializers/test_json_serializer.rb +29 -29
  116. data/test/unit/test_clone.rb +69 -0
  117. data/test/unit/test_descendant_appends.rb +3 -3
  118. data/test/unit/test_document.rb +49 -67
  119. data/test/unit/test_dynamic_finder.rb +53 -51
  120. data/test/unit/test_embedded_document.rb +19 -38
  121. data/test/unit/{test_support.rb → test_extensions.rb} +136 -122
  122. data/test/unit/test_key.rb +185 -0
  123. data/test/unit/test_keys.rb +29 -147
  124. data/test/unit/test_mongo_mapper.rb +3 -48
  125. data/test/unit/test_pagination.rb +1 -150
  126. data/test/unit/test_rails.rb +77 -19
  127. data/test/unit/test_rails_compatibility.rb +12 -12
  128. data/test/unit/test_serialization.rb +5 -5
  129. data/test/unit/test_time_zones.rb +9 -9
  130. data/test/unit/test_validations.rb +46 -46
  131. metadata +157 -155
  132. data/.gitignore +0 -10
  133. data/Rakefile +0 -55
  134. data/VERSION +0 -1
  135. data/lib/mongo_mapper/plugins/pagination/proxy.rb +0 -72
  136. data/lib/mongo_mapper/query.rb +0 -130
  137. data/lib/mongo_mapper/support.rb +0 -215
  138. data/mongo_mapper.gemspec +0 -196
  139. data/performance/read_write.rb +0 -52
  140. data/specs.watchr +0 -51
  141. data/test/support/custom_matchers.rb +0 -55
  142. data/test/support/timing.rb +0 -16
  143. data/test/unit/test_query.rb +0 -340
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module MongoMapper
2
3
  module Plugins
3
4
  def plugins
@@ -10,25 +11,5 @@ module MongoMapper
10
11
  mod.configure(self) if mod.respond_to?(:configure)
11
12
  plugins << mod
12
13
  end
13
-
14
- autoload :Callbacks, 'mongo_mapper/plugins/callbacks'
15
- autoload :Clone, 'mongo_mapper/plugins/clone'
16
- autoload :Descendants, 'mongo_mapper/plugins/descendants'
17
- autoload :Dirty, 'mongo_mapper/plugins/dirty'
18
- autoload :Equality, 'mongo_mapper/plugins/equality'
19
- autoload :IdentityMap, 'mongo_mapper/plugins/identity_map'
20
- autoload :Inspect, 'mongo_mapper/plugins/inspect'
21
- autoload :Keys, 'mongo_mapper/plugins/keys'
22
- autoload :Logger, 'mongo_mapper/plugins/logger'
23
- autoload :Modifiers, 'mongo_mapper/plugins/modifiers'
24
- autoload :Protected, 'mongo_mapper/plugins/protected'
25
- autoload :Rails, 'mongo_mapper/plugins/rails'
26
- autoload :Serialization, 'mongo_mapper/plugins/serialization'
27
- autoload :Timestamps, 'mongo_mapper/plugins/timestamps'
28
- autoload :Userstamps, 'mongo_mapper/plugins/userstamps'
29
- autoload :Validations, 'mongo_mapper/plugins/validations'
30
14
  end
31
15
  end
32
-
33
- require 'mongo_mapper/plugins/associations'
34
- require 'mongo_mapper/plugins/pagination'
@@ -1,3 +1,6 @@
1
+ # encoding: UTF-8
2
+ require 'set'
3
+
1
4
  module MongoMapper
2
5
  module Support
3
6
  module DescendantAppends
@@ -14,9 +17,7 @@ module MongoMapper
14
17
 
15
18
  # @api public
16
19
  def append_extensions(*extensions)
17
- extra_extensions.concat extensions
18
-
19
- # Add the extension to existing descendants
20
+ extra_extensions.concat(extensions)
20
21
  descendants.each do |model|
21
22
  extensions.each { |extension| model.extend(extension) }
22
23
  end
@@ -24,9 +25,7 @@ module MongoMapper
24
25
 
25
26
  # @api public
26
27
  def append_inclusions(*inclusions)
27
- extra_inclusions.concat inclusions
28
-
29
- # Add the inclusion to existing descendants
28
+ extra_inclusions.concat(inclusions)
30
29
  descendants.each do |model|
31
30
  inclusions.each { |inclusion| model.send(:include, inclusion) }
32
31
  end
@@ -0,0 +1,4 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ Version = '0.8.2'
4
+ end
data/lib/mongo_mapper.rb CHANGED
@@ -1,137 +1,80 @@
1
- require 'set'
2
- require 'uri'
3
-
4
- # if Gem is defined i'll assume you are using rubygems and lock specific versions
5
- # call me crazy but a plain old require will just get the latest version you have installed
6
- # so i want to make sure that if you are using gems you do in fact have the correct versions
7
- # if there is a better way to do this, please enlighten me!
8
- if self.class.const_defined?(:Gem)
9
- gem 'activesupport', '>= 2.3'
10
- gem 'mongo', '0.19.1'
11
- gem 'jnunemaker-validatable', '1.8.3'
12
- end
13
-
14
- require 'active_support/all'
15
- require 'mongo'
1
+ # encoding: UTF-8
2
+ # Make sure you have the correct versions of the gems (see gemspec) in your load path.
3
+ require 'plucky'
16
4
  require 'validatable'
5
+ require 'active_support/all'
17
6
 
18
7
  module MongoMapper
19
- # generic MM error
20
- class MongoMapperError < StandardError; end
21
-
22
- # raised when key expected to exist but not found
23
- class KeyNotFound < MongoMapperError; end
24
-
25
- # raised when document expected but not found
26
- class DocumentNotFound < MongoMapperError; end
27
-
28
- # raised when trying to connect using uri with incorrect scheme
29
- class InvalidScheme < MongoMapperError; end
30
-
31
- # raised when document not valid and using !
32
- class DocumentNotValid < MongoMapperError
33
- def initialize(document)
34
- super("Validation failed: #{document.errors.full_messages.join(", ")}")
8
+ autoload :Connection, 'mongo_mapper/connection'
9
+
10
+ autoload :Error, 'mongo_mapper/exceptions'
11
+ autoload :KeyNotFound, 'mongo_mapper/exceptions'
12
+ autoload :DocumentNotFound, 'mongo_mapper/exceptions'
13
+ autoload :InvalidScheme, 'mongo_mapper/exceptions'
14
+ autoload :DocumentNotValid, 'mongo_mapper/exceptions'
15
+ autoload :AccessibleOrProtected, 'mongo_mapper/exceptions'
16
+
17
+ autoload :Document, 'mongo_mapper/document'
18
+ autoload :EmbeddedDocument, 'mongo_mapper/embedded_document'
19
+ autoload :Plugins, 'mongo_mapper/plugins'
20
+ autoload :Version, 'mongo_mapper/version'
21
+
22
+ module Plugins
23
+ autoload :Associations, 'mongo_mapper/plugins/associations'
24
+ autoload :Accessible, 'mongo_mapper/plugins/accessible'
25
+ autoload :Callbacks, 'mongo_mapper/plugins/callbacks'
26
+ autoload :Caching, 'mongo_mapper/plugins/caching'
27
+ autoload :Clone, 'mongo_mapper/plugins/clone'
28
+ autoload :Descendants, 'mongo_mapper/plugins/descendants'
29
+ autoload :Dirty, 'mongo_mapper/plugins/dirty'
30
+ autoload :Document, 'mongo_mapper/plugins/document'
31
+ autoload :DynamicQuerying, 'mongo_mapper/plugins/dynamic_querying'
32
+ autoload :EmbeddedDocument, 'mongo_mapper/plugins/embedded_document'
33
+ autoload :Equality, 'mongo_mapper/plugins/equality'
34
+ autoload :IdentityMap, 'mongo_mapper/plugins/identity_map'
35
+ autoload :Inspect, 'mongo_mapper/plugins/inspect'
36
+ autoload :Indexes, 'mongo_mapper/plugins/indexes'
37
+ autoload :Keys, 'mongo_mapper/plugins/keys'
38
+ autoload :Logger, 'mongo_mapper/plugins/logger'
39
+ autoload :Modifiers, 'mongo_mapper/plugins/modifiers'
40
+ autoload :Pagination, 'mongo_mapper/plugins/pagination'
41
+ autoload :Persistence, 'mongo_mapper/plugins/persistence'
42
+ autoload :Protected, 'mongo_mapper/plugins/protected'
43
+ autoload :Querying, 'mongo_mapper/plugins/querying'
44
+ autoload :Rails, 'mongo_mapper/plugins/rails'
45
+ autoload :Safe, 'mongo_mapper/plugins/safe'
46
+ autoload :Sci, 'mongo_mapper/plugins/sci'
47
+ autoload :Scopes, 'mongo_mapper/plugins/scopes'
48
+ autoload :Serialization, 'mongo_mapper/plugins/serialization'
49
+ autoload :Timestamps, 'mongo_mapper/plugins/timestamps'
50
+ autoload :Userstamps, 'mongo_mapper/plugins/userstamps'
51
+ autoload :Validations, 'mongo_mapper/plugins/validations'
52
+
53
+ module Associations
54
+ autoload :Base, 'mongo_mapper/plugins/associations/base'
55
+ autoload :Collection, 'mongo_mapper/plugins/associations/collection'
56
+ autoload :EmbeddedCollection, 'mongo_mapper/plugins/associations/embedded_collection'
57
+ autoload :ManyDocumentsProxy, 'mongo_mapper/plugins/associations/many_documents_proxy'
58
+ autoload :BelongsToProxy, 'mongo_mapper/plugins/associations/belongs_to_proxy'
59
+ autoload :BelongsToPolymorphicProxy, 'mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy'
60
+ autoload :ManyPolymorphicProxy, 'mongo_mapper/plugins/associations/many_polymorphic_proxy'
61
+ autoload :ManyEmbeddedProxy, 'mongo_mapper/plugins/associations/many_embedded_proxy'
62
+ autoload :ManyEmbeddedPolymorphicProxy, 'mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy'
63
+ autoload :ManyDocumentsAsProxy, 'mongo_mapper/plugins/associations/many_documents_as_proxy'
64
+ autoload :OneProxy, 'mongo_mapper/plugins/associations/one_proxy'
65
+ autoload :OneEmbeddedProxy, 'mongo_mapper/plugins/associations/one_embedded_proxy'
66
+ autoload :InArrayProxy, 'mongo_mapper/plugins/associations/in_array_proxy'
35
67
  end
36
68
  end
37
69
 
38
- # @api public
39
- def self.connection
40
- @@connection ||= Mongo::Connection.new
41
- end
42
-
43
- # @api public
44
- def self.connection=(new_connection)
45
- @@connection = new_connection
46
- end
47
-
48
- # @api public
49
- def self.logger
50
- connection.logger
51
- end
52
-
53
- # @api public
54
- def self.database=(name)
55
- @@database = nil
56
- @@database_name = name
57
- end
58
-
59
- # @api public
60
- def self.database
61
- if @@database_name.blank?
62
- raise 'You forgot to set the default database name: MongoMapper.database = "foobar"'
63
- end
64
-
65
- @@database ||= MongoMapper.connection.db(@@database_name)
66
- end
67
-
68
- def self.config=(hash)
69
- @@config = hash
70
- end
71
-
72
- def self.config
73
- raise 'Set config before connecting. MongoMapper.config = {...}' unless defined?(@@config)
74
- @@config
75
- end
76
-
77
- # @api private
78
- def self.config_for_environment(environment)
79
- env = config[environment]
80
- return env if env['uri'].blank?
81
-
82
- uri = URI.parse(env['uri'])
83
- raise InvalidScheme.new('must be mongodb') unless uri.scheme == 'mongodb'
84
- {
85
- 'host' => uri.host,
86
- 'port' => uri.port,
87
- 'database' => uri.path.gsub(/^\//, ''),
88
- 'username' => uri.user,
89
- 'password' => uri.password,
90
- }
91
- end
92
-
93
- def self.connect(environment, options={})
94
- raise 'Set config before connecting. MongoMapper.config = {...}' if config.blank?
95
- env = config_for_environment(environment)
96
- MongoMapper.connection = Mongo::Connection.new(env['host'], env['port'], options)
97
- MongoMapper.database = env['database']
98
- MongoMapper.database.authenticate(env['username'], env['password']) if env['username'] && env['password']
99
- end
100
-
101
- def self.setup(config, environment, options={})
102
- using_passenger = options.delete(:passenger)
103
- handle_passenger_forking if using_passenger
104
- self.config = config
105
- connect(environment, options)
106
- end
107
-
108
- def self.handle_passenger_forking
109
- if defined?(PhusionPassenger)
110
- PhusionPassenger.on_event(:starting_worker_process) do |forked|
111
- connection.connect_to_master if forked
112
- end
113
- end
114
- end
115
-
116
- # @api private
117
- def self.use_time_zone?
118
- Time.respond_to?(:zone) && Time.zone ? true : false
119
- end
120
-
121
- # @api private
122
- def self.time_class
123
- use_time_zone? ? Time.zone : Time
124
- end
125
-
126
- # @api private
127
- def self.normalize_object_id(value)
128
- value.is_a?(String) ? Mongo::ObjectID.from_string(value) : value
129
- end
70
+ extend Connection
71
+ end
130
72
 
131
- autoload :Query, 'mongo_mapper/query'
132
- autoload :Document, 'mongo_mapper/document'
133
- autoload :EmbeddedDocument, 'mongo_mapper/embedded_document'
73
+ Dir[File.join(File.dirname(__FILE__), 'mongo_mapper', 'extensions', '*.rb')].each do |extension|
74
+ require extension
134
75
  end
135
76
 
136
- require 'mongo_mapper/support'
137
- require 'mongo_mapper/plugins'
77
+ require 'mongo_mapper/support/descendant_appends'
78
+
79
+ # FIXME: autoload with proxy is failing, need to investigate
80
+ require 'mongo_mapper/plugins/associations/proxy'
File without changes
@@ -6,17 +6,17 @@ class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
6
6
  Status.collection.remove
7
7
  Project.collection.remove
8
8
  end
9
-
9
+
10
10
  should "default to nil" do
11
11
  status = Status.new
12
12
  status.target.nil?.should be_true
13
13
  status.target.inspect.should == "nil"
14
14
  end
15
-
15
+
16
16
  should "have boolean presence method" do
17
17
  status = Status.new
18
18
  status.target?.should be_false
19
-
19
+
20
20
  status.target = Project.new(:name => 'mongomapper')
21
21
  status.target?.should be_true
22
22
  end
@@ -46,7 +46,7 @@ class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
46
46
  status.target_id.nil?.should be_true
47
47
  status.target.nil?.should be_true
48
48
  end
49
-
49
+
50
50
  context "association id set but document not found" do
51
51
  setup do
52
52
  @status = Status.new(:name => 'Foo!')
@@ -60,4 +60,4 @@ class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
60
60
  @status.target.nil?.should be_true
61
61
  end
62
62
  end
63
- end
63
+ end
@@ -2,60 +2,52 @@ require 'test_helper'
2
2
  require 'models'
3
3
 
4
4
  class BelongsToProxyTest < Test::Unit::TestCase
5
- def setup
5
+ def setup
6
6
  @post_class = Doc()
7
7
  @comment_class = Doc do
8
8
  key :post_id, String
9
9
  end
10
-
10
+
11
11
  @comment_class.belongs_to :post, :class => @post_class
12
12
  end
13
-
13
+
14
14
  should "default to nil" do
15
15
  @comment_class.new.post.nil?.should be_true
16
16
  end
17
-
18
- should "send object id to target" do
19
- post = @post_class.new(:name => 'mongomapper')
20
- comment = @comment_class.new(:name => 'Foo!', :post => post)
21
- comment.save
22
-
23
- comment.post.object_id.should == comment.post.target.object_id
24
- end
25
-
17
+
26
18
  should "have boolean presence method" do
27
19
  comment = @comment_class.new(:name => 'Foo!')
28
20
  comment.post?.should be_false
29
-
21
+
30
22
  comment.post = @post_class.new(:name => 'mongomapper')
31
23
  comment.post?.should be_true
32
24
  end
33
-
25
+
34
26
  should "be able to replace the association" do
35
27
  post = @post_class.new(:name => 'mongomapper')
36
28
  comment = @comment_class.new(:name => 'Foo!', :post => post)
37
29
  comment.save.should be_true
38
-
30
+
39
31
  comment = comment.reload
40
32
  comment.post.should == post
41
33
  comment.post.nil?.should be_false
42
34
  end
43
-
35
+
44
36
  should "unset the association" do
45
37
  post = @post_class.new(:name => 'mongomapper')
46
38
  comment = @comment_class.new(:name => 'Foo!', :post => post)
47
39
  comment.save.should be_true
48
-
40
+
49
41
  comment = comment.reload
50
42
  comment.post = nil
51
43
  comment.post.nil?.should be_true
52
44
  end
53
-
45
+
54
46
  should "return nil if id set but document not found" do
55
- id = Mongo::ObjectID.new
47
+ id = BSON::ObjectID.new
56
48
  @comment_class.new(:name => 'Foo', :post_id => id).post.nil?.should be_true
57
49
  end
58
-
50
+
59
51
  context ":dependent" do
60
52
  setup do
61
53
  # FIXME: make use of already defined models
@@ -75,7 +67,7 @@ class BelongsToProxyTest < Test::Unit::TestCase
75
67
  Object.send :remove_const, 'Property' if defined?(::Property)
76
68
  Object.send :remove_const, 'Thing' if defined?(::Thing)
77
69
  end
78
-
70
+
79
71
  context "=> destroy" do
80
72
  setup do
81
73
  Property.key :thing_id, ObjectId
@@ -22,7 +22,7 @@ class InArrayProxyTest < Test::Unit::TestCase
22
22
  Object.send :remove_const, 'List' if defined?(::List)
23
23
  Object.send :remove_const, 'User' if defined?(::User)
24
24
  end
25
-
25
+
26
26
  should "default reader to empty array" do
27
27
  User.new.lists.should == []
28
28
  end
@@ -34,14 +34,14 @@ class InArrayProxyTest < Test::Unit::TestCase
34
34
  user.lists.concat List.new(:name => 'Foo3!')
35
35
  user.lists.size.should == 3
36
36
  end
37
-
37
+
38
38
  should "ignore adding duplicate ids" do
39
39
  user = User.create(:name => 'John')
40
40
  list = List.create(:name => 'Foo')
41
41
  user.lists << list
42
42
  user.lists << list
43
43
  user.lists << list
44
-
44
+
45
45
  user.list_ids.should == [list.id]
46
46
  user.lists.count.should == 1
47
47
  end
@@ -135,36 +135,30 @@ class InArrayProxyTest < Test::Unit::TestCase
135
135
 
136
136
  context "all" do
137
137
  should "work" do
138
- @user.lists.find(:all, :order => :position.asc).should == [@list1, @list2]
139
138
  @user.lists.all(:order => :position.asc).should == [@list1, @list2]
140
139
  end
141
140
 
142
141
  should "work with conditions" do
143
- @user.lists.find(:all, :name => 'Foo 1').should == [@list1]
144
142
  @user.lists.all(:name => 'Foo 1').should == [@list1]
145
143
  end
146
144
  end
147
145
 
148
146
  context "first" do
149
147
  should "work" do
150
- @user.lists.find(:first, :order => 'position').should == @list1
151
148
  @user.lists.first(:order => 'position').should == @list1
152
149
  end
153
150
 
154
151
  should "work with conditions" do
155
- @user.lists.find(:first, :position => 2).should == @list2
156
152
  @user.lists.first(:position => 2).should == @list2
157
153
  end
158
154
  end
159
155
 
160
156
  context "last" do
161
157
  should "work" do
162
- @user.lists.find(:last, :order => 'position').should == @list2
163
158
  @user.lists.last(:order => 'position').should == @list2
164
159
  end
165
160
 
166
161
  should "work with conditions" do
167
- @user.lists.find(:last, :position => 2, :order => 'position').should == @list2
168
162
  @user.lists.last(:position => 2, :order => 'position').should == @list2
169
163
  end
170
164
  end
@@ -174,6 +168,10 @@ class InArrayProxyTest < Test::Unit::TestCase
174
168
  @user.lists.find(@list1.id).should == @list1
175
169
  end
176
170
 
171
+ should "work with string ids" do
172
+ @user.lists.find(@list1.id.to_s).should == @list1
173
+ end
174
+
177
175
  should "not work for id not in association" do
178
176
  @user.lists.find(@list3.id).should be_nil
179
177
  end
@@ -35,7 +35,7 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
35
35
  PostComment.new(:body => 'baz')
36
36
  ]
37
37
  }.should change { PostComment.count }.by(3)
38
-
38
+
39
39
  post = post.reload
40
40
  post.comments.size.should == 3
41
41
  bodies = post.comments.collect(&:body)
@@ -172,24 +172,24 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
172
172
  end
173
173
  end
174
174
  end
175
-
175
+
176
176
  context "dynamic finders" do
177
177
  should "work with single key" do
178
178
  @post.comments.find_by_body('comment1').should == @comment1
179
179
  @post2.comments.find_by_body('comment1').should == @comment4
180
180
  end
181
-
181
+
182
182
  should "work with multiple keys" do
183
183
  @post.comments.find_by_body_and_name('comment1', 'John').should == @comment1
184
184
  @post.comments.find_by_body_and_name('comment1', 'Frank').should be_nil
185
185
  end
186
-
186
+
187
187
  should "raise error when using !" do
188
188
  lambda {
189
189
  @post.comments.find_by_body!('asdf')
190
190
  }.should raise_error(MongoMapper::DocumentNotFound)
191
191
  end
192
-
192
+
193
193
  context "find_or_create_by" do
194
194
  should "not create document if found" do
195
195
  lambda {