mongo_mapper 0.12.0 → 0.13.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +7 -0
  2. data/README.rdoc +35 -13
  3. data/bin/mmconsole +1 -1
  4. data/lib/mongo_mapper.rb +4 -0
  5. data/lib/mongo_mapper/connection.rb +17 -6
  6. data/lib/mongo_mapper/document.rb +1 -0
  7. data/lib/mongo_mapper/exceptions.rb +4 -1
  8. data/lib/mongo_mapper/extensions/binary.rb +1 -1
  9. data/lib/mongo_mapper/extensions/boolean.rb +20 -23
  10. data/lib/mongo_mapper/extensions/date.rb +3 -3
  11. data/lib/mongo_mapper/extensions/integer.rb +5 -1
  12. data/lib/mongo_mapper/extensions/kernel.rb +2 -0
  13. data/lib/mongo_mapper/extensions/ordered_hash.rb +23 -0
  14. data/lib/mongo_mapper/extensions/string.rb +2 -2
  15. data/lib/mongo_mapper/extensions/time.rb +7 -5
  16. data/lib/mongo_mapper/middleware/identity_map.rb +3 -4
  17. data/lib/mongo_mapper/plugins.rb +1 -1
  18. data/lib/mongo_mapper/plugins/associations.rb +11 -5
  19. data/lib/mongo_mapper/plugins/associations/base.rb +5 -3
  20. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +0 -0
  21. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +8 -8
  22. data/lib/mongo_mapper/plugins/associations/collection.rb +2 -0
  23. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +32 -7
  24. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +2 -2
  25. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +12 -12
  26. data/lib/mongo_mapper/plugins/associations/proxy.rb +5 -1
  27. data/lib/mongo_mapper/plugins/associations/single_association.rb +6 -6
  28. data/lib/mongo_mapper/plugins/clone.rb +4 -2
  29. data/lib/mongo_mapper/plugins/dirty.rb +22 -21
  30. data/lib/mongo_mapper/plugins/document.rb +4 -4
  31. data/lib/mongo_mapper/plugins/dumpable.rb +22 -0
  32. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +58 -9
  33. data/lib/mongo_mapper/plugins/identity_map.rb +42 -32
  34. data/lib/mongo_mapper/plugins/keys.rb +133 -54
  35. data/lib/mongo_mapper/plugins/keys/key.rb +68 -22
  36. data/lib/mongo_mapper/plugins/modifiers.rb +26 -19
  37. data/lib/mongo_mapper/plugins/persistence.rb +15 -5
  38. data/lib/mongo_mapper/plugins/querying.rb +15 -40
  39. data/lib/mongo_mapper/plugins/querying/{decorator.rb → decorated_plucky_query.rb} +24 -4
  40. data/lib/mongo_mapper/plugins/rails.rb +22 -2
  41. data/lib/mongo_mapper/plugins/safe.rb +8 -5
  42. data/lib/mongo_mapper/plugins/sci.rb +26 -4
  43. data/lib/mongo_mapper/plugins/scopes.rb +5 -4
  44. data/lib/mongo_mapper/plugins/timestamps.rb +11 -4
  45. data/lib/mongo_mapper/plugins/validations.rb +1 -1
  46. data/lib/mongo_mapper/utils.rb +12 -0
  47. data/lib/mongo_mapper/version.rb +1 -1
  48. data/lib/rails/generators/mongo_mapper/config/config_generator.rb +20 -7
  49. data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +6 -0
  50. data/lib/rails/generators/mongo_mapper/model/model_generator.rb +18 -1
  51. data/lib/rails/generators/mongo_mapper/model/templates/model.rb +9 -5
  52. data/{test/functional/test_accessible.rb → spec/functional/accessible_spec.rb} +29 -29
  53. data/{test/functional/associations/test_belongs_to_polymorphic_proxy.rb → spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb} +10 -10
  54. data/{test/functional/associations/test_belongs_to_proxy.rb → spec/functional/associations/belongs_to_proxy_spec.rb} +82 -64
  55. data/{test/functional/associations/test_in_array_proxy.rb → spec/functional/associations/in_array_proxy_spec.rb} +68 -68
  56. data/{test/functional/associations/test_many_documents_as_proxy.rb → spec/functional/associations/many_documents_as_proxy_spec.rb} +37 -38
  57. data/{test/functional/associations/test_many_documents_proxy.rb → spec/functional/associations/many_documents_proxy_spec.rb} +233 -146
  58. data/{test/functional/associations/test_many_embedded_polymorphic_proxy.rb → spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb} +19 -20
  59. data/{test/functional/associations/test_many_embedded_proxy.rb → spec/functional/associations/many_embedded_proxy_spec.rb} +23 -24
  60. data/{test/functional/associations/test_many_polymorphic_proxy.rb → spec/functional/associations/many_polymorphic_proxy_spec.rb} +45 -46
  61. data/{test/functional/associations/test_one_as_proxy.rb → spec/functional/associations/one_as_proxy_spec.rb} +75 -77
  62. data/{test/functional/associations/test_one_embedded_polymorphic_proxy.rb → spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb} +31 -32
  63. data/{test/functional/associations/test_one_embedded_proxy.rb → spec/functional/associations/one_embedded_proxy_spec.rb} +10 -10
  64. data/{test/functional/associations/test_one_proxy.rb → spec/functional/associations/one_proxy_spec.rb} +125 -102
  65. data/spec/functional/associations_spec.rb +48 -0
  66. data/{test/functional/test_binary.rb → spec/functional/binary_spec.rb} +6 -6
  67. data/spec/functional/caching_spec.rb +75 -0
  68. data/{test/functional/test_callbacks.rb → spec/functional/callbacks_spec.rb} +84 -26
  69. data/{test/functional/test_dirty.rb → spec/functional/dirty_spec.rb} +57 -42
  70. data/{test/functional/test_document.rb → spec/functional/document_spec.rb} +52 -52
  71. data/spec/functional/dumpable_spec.rb +24 -0
  72. data/{test/functional/test_dynamic_querying.rb → spec/functional/dynamic_querying_spec.rb} +14 -14
  73. data/{test/functional/test_embedded_document.rb → spec/functional/embedded_document_spec.rb} +51 -42
  74. data/{test/functional/test_equality.rb → spec/functional/equality_spec.rb} +4 -4
  75. data/spec/functional/extensions_spec.rb +16 -0
  76. data/{test/functional/test_identity_map.rb → spec/functional/identity_map_spec.rb} +73 -61
  77. data/spec/functional/indexes_spec.rb +48 -0
  78. data/spec/functional/keys_spec.rb +224 -0
  79. data/{test/functional/test_logger.rb → spec/functional/logger_spec.rb} +6 -6
  80. data/spec/functional/modifiers_spec.rb +550 -0
  81. data/spec/functional/pagination_spec.rb +89 -0
  82. data/spec/functional/protected_spec.rb +199 -0
  83. data/spec/functional/querying_spec.rb +1003 -0
  84. data/spec/functional/rails_spec.rb +55 -0
  85. data/spec/functional/safe_spec.rb +163 -0
  86. data/{test/functional/test_sci.rb → spec/functional/sci_spec.rb} +123 -34
  87. data/{test/functional/test_scopes.rb → spec/functional/scopes_spec.rb} +59 -26
  88. data/spec/functional/timestamps_spec.rb +97 -0
  89. data/{test/functional/test_touch.rb → spec/functional/touch_spec.rb} +13 -13
  90. data/spec/functional/userstamps_spec.rb +46 -0
  91. data/{test/functional/test_validations.rb → spec/functional/validations_spec.rb} +64 -64
  92. data/spec/spec_helper.rb +81 -0
  93. data/spec/support/matchers.rb +24 -0
  94. data/{test → spec/support}/models.rb +1 -6
  95. data/spec/unit/associations/base_spec.rb +146 -0
  96. data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
  97. data/spec/unit/associations/many_association_spec.rb +64 -0
  98. data/spec/unit/associations/one_association_spec.rb +48 -0
  99. data/{test/unit/associations/test_proxy.rb → spec/unit/associations/proxy_spec.rb} +21 -21
  100. data/{test/unit/test_clone.rb → spec/unit/clone_spec.rb} +21 -11
  101. data/spec/unit/config_generator_spec.rb +24 -0
  102. data/{test/unit/test_document.rb → spec/unit/document_spec.rb} +42 -42
  103. data/{test/unit/test_dynamic_finder.rb → spec/unit/dynamic_finder_spec.rb} +28 -28
  104. data/{test/unit/test_embedded_document.rb → spec/unit/embedded_document_spec.rb} +102 -108
  105. data/{test/unit/test_equality.rb → spec/unit/equality_spec.rb} +7 -7
  106. data/{test/unit/test_exceptions.rb → spec/unit/exceptions_spec.rb} +3 -3
  107. data/{test/unit/test_extensions.rb → spec/unit/extensions_spec.rb} +85 -71
  108. data/spec/unit/identity_map_middleware_spec.rb +134 -0
  109. data/{test/unit/test_inspect.rb → spec/unit/inspect_spec.rb} +8 -8
  110. data/{test/unit/test_key.rb → spec/unit/key_spec.rb} +82 -52
  111. data/spec/unit/keys_spec.rb +155 -0
  112. data/spec/unit/model_generator_spec.rb +47 -0
  113. data/spec/unit/mongo_mapper_spec.rb +184 -0
  114. data/spec/unit/pagination_spec.rb +11 -0
  115. data/{test/unit/test_plugins.rb → spec/unit/plugins_spec.rb} +14 -14
  116. data/spec/unit/rails_compatibility_spec.rb +40 -0
  117. data/{test/unit/test_rails_reflect_on_association.rb → spec/unit/rails_reflect_on_association_spec.rb} +9 -9
  118. data/{test/unit/test_rails.rb → spec/unit/rails_spec.rb} +31 -31
  119. data/spec/unit/serialization_spec.rb +169 -0
  120. data/spec/unit/serializers/json_serializer_spec.rb +218 -0
  121. data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
  122. data/{test/unit/test_time_zones.rb → spec/unit/time_zones_spec.rb} +8 -8
  123. data/{test/unit/test_translation.rb → spec/unit/translation_spec.rb} +6 -6
  124. data/{test/unit/test_validations.rb → spec/unit/validations_spec.rb} +72 -59
  125. metadata +199 -179
  126. data/test/_NOTE_ON_TESTING +0 -1
  127. data/test/functional/test_associations.rb +0 -46
  128. data/test/functional/test_caching.rb +0 -77
  129. data/test/functional/test_indexes.rb +0 -50
  130. data/test/functional/test_modifiers.rb +0 -537
  131. data/test/functional/test_pagination.rb +0 -91
  132. data/test/functional/test_protected.rb +0 -201
  133. data/test/functional/test_querying.rb +0 -935
  134. data/test/functional/test_safe.rb +0 -76
  135. data/test/functional/test_timestamps.rb +0 -62
  136. data/test/functional/test_userstamps.rb +0 -44
  137. data/test/support/railtie.rb +0 -4
  138. data/test/support/railtie/autoloaded.rb +0 -2
  139. data/test/support/railtie/not_autoloaded.rb +0 -3
  140. data/test/support/railtie/parent.rb +0 -3
  141. data/test/test_active_model_lint.rb +0 -18
  142. data/test/test_helper.rb +0 -93
  143. data/test/unit/associations/test_base.rb +0 -146
  144. data/test/unit/associations/test_belongs_to_association.rb +0 -29
  145. data/test/unit/associations/test_many_association.rb +0 -63
  146. data/test/unit/associations/test_one_association.rb +0 -47
  147. data/test/unit/serializers/test_json_serializer.rb +0 -216
  148. data/test/unit/serializers/test_xml_serializer.rb +0 -196
  149. data/test/unit/test_identity_map_middleware.rb +0 -132
  150. data/test/unit/test_keys.rb +0 -65
  151. data/test/unit/test_mongo_mapper.rb +0 -157
  152. data/test/unit/test_pagination.rb +0 -11
  153. data/test/unit/test_rails_compatibility.rb +0 -38
  154. data/test/unit/test_serialization.rb +0 -166
@@ -4,6 +4,11 @@ module MongoMapper
4
4
  module Timestamps
5
5
  extend ActiveSupport::Concern
6
6
 
7
+ included do
8
+ class_attribute :record_timestamps
9
+ self.record_timestamps = true
10
+ end
11
+
7
12
  module ClassMethods
8
13
  def timestamps!
9
14
  key :created_at, Time
@@ -13,10 +18,12 @@ module MongoMapper
13
18
  end
14
19
 
15
20
  def update_timestamps
16
- now = Time.now.utc
17
- self[:created_at] = now if !persisted? && !created_at?
18
- self[:updated_at] = now
21
+ if self.record_timestamps
22
+ now = Time.now.utc
23
+ self[:created_at] = now if !persisted? && !created_at?
24
+ self[:updated_at] = now
25
+ end
19
26
  end
20
27
  end
21
28
  end
22
- end
29
+ end
@@ -18,7 +18,7 @@ module MongoMapper
18
18
  end
19
19
 
20
20
  def save(options = {})
21
- options.reverse_merge!(:validate => true)
21
+ options = options.reverse_merge(:validate => true)
22
22
  !options[:validate] || valid? ? super : false
23
23
  end
24
24
 
@@ -0,0 +1,12 @@
1
+ module MongoMapper
2
+ module Utils
3
+ def self.get_safe_options(options)
4
+ return {} unless options and options.key? :safe
5
+ safe = options[:safe]
6
+ safe = {:w => 1} if safe == true
7
+ safe = {:w => 0} if safe == false
8
+ safe = {:w => safe} if safe.is_a? Fixnum
9
+ safe
10
+ end
11
+ end
12
+ end
@@ -1,4 +1,4 @@
1
1
  # encoding: UTF-8
2
2
  module MongoMapper
3
- Version = '0.12.0'
3
+ Version = '0.13.0.beta1'
4
4
  end
@@ -3,22 +3,35 @@
3
3
  module MongoMapper
4
4
  module Generators
5
5
  class ConfigGenerator < Rails::Generators::Base
6
- desc "creates the MongoMapper configuration at config/mongo.yml"
6
+ desc 'creates the MongoMapper configuration at config/mongo.yml'
7
7
 
8
8
  argument :database_name, :type => :string, :optional => true
9
9
 
10
10
  def self.source_root
11
- @source_root ||= File.expand_path("../templates", __FILE__)
12
- end
13
-
14
- def app_name
15
- Rails::Application.subclasses.first.parent.to_s.underscore
11
+ @source_root ||= File.expand_path('../templates', __FILE__)
16
12
  end
17
13
 
18
14
  def create_config_file
19
- template 'mongo.yml', File.join('config', "mongo.yml")
15
+ template 'mongo.yml', File.join('config', 'mongo.yml')
20
16
  end
21
17
 
18
+ protected
19
+
20
+ def app_name
21
+ @app_name ||= defined_app_const_base? ? defined_app_name : File.basename(destination_root)
22
+ end
23
+
24
+ def defined_app_name
25
+ defined_app_const_base.underscore
26
+ end
27
+
28
+ def defined_app_const_base
29
+ Rails.respond_to?(:application) && defined?(Rails::Application) &&
30
+ Rails.application.is_a?(Rails::Application) && Rails.application.class.name.sub(/::Application$/, '')
31
+ end
32
+
33
+ alias :defined_app_const_base? :defined_app_const_base
34
+
22
35
  end
23
36
  end
24
37
  end
@@ -1,6 +1,11 @@
1
1
  defaults: &defaults
2
2
  host: 127.0.0.1
3
3
  port: 27017
4
+ options:
5
+ w: 1
6
+ pool_size: 1
7
+ slave_ok: false
8
+ ssl: false
4
9
 
5
10
  development:
6
11
  <<: *defaults
@@ -9,6 +14,7 @@ development:
9
14
  test:
10
15
  <<: *defaults
11
16
  database: <%= database_name || app_name %>_test
17
+ w: 0
12
18
 
13
19
  # set these environment variables on your prod server
14
20
  production:
@@ -4,11 +4,15 @@ module MongoMapper
4
4
  module Generators
5
5
  class ModelGenerator < Rails::Generators::NamedBase
6
6
  desc 'Creates a mongomapper model'
7
+
7
8
  argument :name, :type => :string
8
9
  argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
9
- class_option :timestamps, :type => :boolean
10
+
10
11
  check_class_collision
11
12
 
13
+ class_option :timestamps, :type => :boolean
14
+ class_option :parent, :type => :string, :desc => "The parent class for the generated model"
15
+
12
16
  def self.source_root
13
17
  @source_root ||= File.expand_path("../templates", __FILE__)
14
18
  end
@@ -18,6 +22,19 @@ module MongoMapper
18
22
  end
19
23
 
20
24
  hook_for :test_framework
25
+
26
+ protected
27
+
28
+ def parent_class_name
29
+ options[:parent]
30
+ end
31
+
32
+ # Rails 3.0.X compatibility
33
+ unless methods.include?(:module_namespacing)
34
+ def module_namespacing(&block)
35
+ yield if block
36
+ end
37
+ end
21
38
  end
22
39
  end
23
40
  end
@@ -1,13 +1,17 @@
1
- <% module_namespacing do -%>
1
+ <%- module_namespacing do -%>
2
+ <%- if parent_class_name.present? -%>
3
+ class <%= class_name %> < <%= parent_class_name.classify %>
4
+ <%- else -%>
2
5
  class <%= class_name %>
3
6
  include MongoMapper::Document
7
+ <%- end -%>
4
8
 
5
- <% attributes.each do |attribute| -%>
9
+ <%- attributes.each do |attribute| -%>
6
10
  key :<%= attribute.name %>, <%= attribute.type.to_s.camelcase %>
7
11
  <% end -%>
8
- <% if options[:timestamps] %>
12
+ <%- if options[:timestamps] -%>
9
13
  timestamps!
10
- <% end -%>
14
+ <%- end -%>
11
15
 
12
16
  end
13
- <% end -%>
17
+ <%- end -%>
@@ -1,8 +1,8 @@
1
- require 'test_helper'
1
+ require 'spec_helper'
2
2
 
3
- class AccessibleTest < Test::Unit::TestCase
3
+ describe "Accessible" do
4
4
  context 'A document with accessible attributes' do
5
- setup do
5
+ before do
6
6
  @doc_class = Doc do
7
7
  key :name, String
8
8
  key :admin, Boolean, :default => false
@@ -13,42 +13,42 @@ class AccessibleTest < Test::Unit::TestCase
13
13
  @doc = @doc_class.create(:name => 'Steve Sloan')
14
14
  end
15
15
 
16
- should 'have accessible attributes class method' do
16
+ it 'should have accessible attributes class method' do
17
17
  @doc_class.accessible_attributes.should == [:name].to_set
18
18
  end
19
19
 
20
- should "default accessible attributes to nil" do
20
+ it "should default accessible attributes to nil" do
21
21
  Doc().accessible_attributes.should be_nil
22
22
  end
23
23
 
24
- should "have accessible_attributes instance method" do
24
+ it "should have accessible_attributes instance method" do
25
25
  @doc.accessible_attributes.should equal(@doc_class.accessible_attributes)
26
26
  end
27
27
 
28
- should "raise error if there are protected attributes" do
28
+ it "should raise error if there are protected attributes" do
29
29
  doc = Doc('Post')
30
30
  doc.attr_protected :admin
31
31
  lambda { doc.attr_accessible :name }.
32
32
  should raise_error(/Declare either attr_protected or attr_accessible for Post/)
33
33
  end
34
34
 
35
- should "know if using accessible attributes" do
35
+ it "should know if using accessible attributes" do
36
36
  @doc_class.accessible_attributes?.should be(true)
37
37
  Doc().accessible_attributes?.should be(false)
38
38
  end
39
39
 
40
- should "assign inaccessible attribute through accessor" do
40
+ it "should assign inaccessible attribute through accessor" do
41
41
  @doc.admin = true
42
42
  @doc.admin.should be_true
43
43
  end
44
44
 
45
- should "ignore inaccessible attribute on #initialize" do
45
+ it "should ignore inaccessible attribute on #initialize" do
46
46
  doc = @doc_class.new(:name => 'John', :admin => true)
47
47
  doc.admin.should be_false
48
48
  doc.name.should == 'John'
49
49
  end
50
50
 
51
- should "not ignore inaccessible attributes on #initialize from the database" do
51
+ it "should not ignore inaccessible attributes on #initialize from the database" do
52
52
  doc = @doc_class.new(:name => 'John')
53
53
  doc.admin = true
54
54
  doc.save!
@@ -58,7 +58,7 @@ class AccessibleTest < Test::Unit::TestCase
58
58
  doc.name.should == 'John'
59
59
  end
60
60
 
61
- should "not ignore inaccessible attributes on #reload" do
61
+ it "should not ignore inaccessible attributes on #reload" do
62
62
  doc = @doc_class.new(:name => 'John')
63
63
  doc.admin = true
64
64
  doc.save!
@@ -68,40 +68,40 @@ class AccessibleTest < Test::Unit::TestCase
68
68
  doc.name.should == 'John'
69
69
  end
70
70
 
71
- should "not ignore inaccessible attribute on #update_attribute" do
71
+ it "should not ignore inaccessible attribute on #update_attribute" do
72
72
  @doc.update_attribute('admin', true)
73
73
  @doc.admin.should be_true
74
74
  end
75
75
 
76
- should "ignore inaccessible attribute on #update_attributes" do
76
+ it "should ignore inaccessible attribute on #update_attributes" do
77
77
  @doc.update_attributes(:name => 'Ren Hoek', :admin => true)
78
78
  @doc.name.should == 'Ren Hoek'
79
79
  @doc.admin.should be_false
80
80
  end
81
81
 
82
- should "ignore inaccessible attribute on #update_attributes!" do
82
+ it "should ignore inaccessible attribute on #update_attributes!" do
83
83
  @doc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
84
84
  @doc.name.should == 'Stimpson J. Cat'
85
85
  @doc.admin.should be_false
86
86
  end
87
87
 
88
- should "ignore inaccessible attribute on #attributes=" do
88
+ it "should ignore inaccessible attribute on #attributes=" do
89
89
  @doc.attributes = {:name => 'Ren Hoek', :admin => true}
90
90
  @doc.name.should == 'Ren Hoek'
91
91
  @doc.admin.should be_false
92
92
  end
93
93
 
94
- should "be indifferent to whether the accessible keys are strings or symbols" do
94
+ it "should be indifferent to whether the accessible keys are strings or symbols" do
95
95
  @doc.update_attributes!("name" => 'Stimpson J. Cat', "admin" => true)
96
96
  @doc.name.should == 'Stimpson J. Cat'
97
97
  @doc.admin.should be_false
98
98
  end
99
99
 
100
- should "accept nil as constructor's argument without raising exception" do
100
+ it "should accept nil as constructor's argument without raising exception" do
101
101
  lambda { @doc_class.new(nil) }.should_not raise_error
102
102
  end
103
103
 
104
- should "ignore all attributes if called with no args" do
104
+ it "should ignore all attributes if called with no args" do
105
105
  @doc_class = Doc do
106
106
  key :name
107
107
  attr_accessible
@@ -112,7 +112,7 @@ class AccessibleTest < Test::Unit::TestCase
112
112
  end
113
113
 
114
114
  context "Single collection inherited accessible attributes" do
115
- setup do
115
+ before do
116
116
  class ::GrandParent
117
117
  include MongoMapper::Document
118
118
  attr_accessible :name
@@ -135,14 +135,14 @@ class AccessibleTest < Test::Unit::TestCase
135
135
  end
136
136
  end
137
137
 
138
- teardown do
138
+ after do
139
139
  Object.send :remove_const, 'GrandParent' if defined?(::GrandParent)
140
140
  Object.send :remove_const, 'Child' if defined?(::Child)
141
141
  Object.send :remove_const, 'GrandChild' if defined?(::GrandChild)
142
142
  Object.send :remove_const, 'OtherChild' if defined?(::OtherChild)
143
143
  end
144
144
 
145
- should "share keys down the inheritance trail" do
145
+ it "should share keys down the inheritance trail" do
146
146
  GrandParent.accessible_attributes.should == [:name].to_set
147
147
  Child.accessible_attributes.should == [:name, :position].to_set
148
148
  GrandChild.accessible_attributes.should == [:name, :position].to_set
@@ -151,7 +151,7 @@ class AccessibleTest < Test::Unit::TestCase
151
151
  end
152
152
 
153
153
  context "An embedded document with accessible attributes" do
154
- setup do
154
+ before do
155
155
  @doc_class = Doc('Project')
156
156
  @edoc_class = EDoc('Person') do
157
157
  key :name, String
@@ -166,30 +166,30 @@ class AccessibleTest < Test::Unit::TestCase
166
166
  @doc.people << @edoc
167
167
  end
168
168
 
169
- should "have accessible attributes class method" do
169
+ it "should have accessible attributes class method" do
170
170
  @edoc_class.accessible_attributes.should == [:name].to_set
171
171
  end
172
172
 
173
- should "default accessible attributes to nil" do
173
+ it "should default accessible attributes to nil" do
174
174
  EDoc().accessible_attributes.should be_nil
175
175
  end
176
176
 
177
- should "have accessible attributes instance method" do
177
+ it "should have accessible attributes instance method" do
178
178
  @edoc.accessible_attributes.should equal(@edoc_class.accessible_attributes)
179
179
  end
180
180
 
181
- should "assign inaccessible attribute through accessor" do
181
+ it "should assign inaccessible attribute through accessor" do
182
182
  @edoc.admin = true
183
183
  @edoc.admin.should be_true
184
184
  end
185
185
 
186
- should "ignore inaccessible attribute on #update_attributes" do
186
+ it "should ignore inaccessible attribute on #update_attributes" do
187
187
  @edoc.update_attributes(:name => 'Ren Hoek', :admin => true)
188
188
  @edoc.name.should == 'Ren Hoek'
189
189
  @edoc.admin.should be_false
190
190
  end
191
191
 
192
- should "ignore inaccessible attribute on #update_attributes!" do
192
+ it "should ignore inaccessible attribute on #update_attributes!" do
193
193
  @edoc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
194
194
  @edoc.name.should == 'Stimpson J. Cat'
195
195
  @edoc.admin.should be_false
@@ -1,19 +1,19 @@
1
- require 'test_helper'
2
- require 'models'
1
+ require 'spec_helper'
3
2
 
4
- class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
5
- def setup
3
+
4
+ describe "BelongsToPolymorphicProxy" do
5
+ before do
6
6
  Status.collection.remove
7
7
  Project.collection.remove
8
8
  end
9
9
 
10
- should "default to nil" do
10
+ it "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
- should "have boolean presence method" do
16
+ it "should have boolean presence method" do
17
17
  status = Status.new
18
18
  status.target?.should be_false
19
19
 
@@ -21,7 +21,7 @@ class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
21
21
  status.target?.should be_true
22
22
  end
23
23
 
24
- should "be able to replace the association" do
24
+ it "should be able to replace the association" do
25
25
  status = Status.new(:name => 'Foo!')
26
26
  project = Project.new(:name => "mongomapper")
27
27
  status.target = project
@@ -34,7 +34,7 @@ class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
34
34
  status.target.name.should == "mongomapper"
35
35
  end
36
36
 
37
- should "unset the association" do
37
+ it "should unset the association" do
38
38
  status = Status.new(:name => 'Foo!')
39
39
  project = Project.new(:name => "mongomapper")
40
40
  status.target = project
@@ -48,7 +48,7 @@ class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
48
48
  end
49
49
 
50
50
  context "association id set but document not found" do
51
- setup do
51
+ before do
52
52
  @status = Status.new(:name => 'Foo!')
53
53
  project = Project.new(:name => "mongomapper")
54
54
  @status.target = project
@@ -57,7 +57,7 @@ class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
57
57
  @status.reload
58
58
  end
59
59
 
60
- should "return nil instead of raising error" do
60
+ it "should return nil instead of raising error" do
61
61
  @status.target.nil?.should be_true
62
62
  end
63
63
  end
@@ -1,25 +1,24 @@
1
- require 'test_helper'
2
- require 'models'
1
+ require 'spec_helper'
3
2
 
4
- class BelongsToProxyTest < Test::Unit::TestCase
5
- def setup
3
+
4
+ describe "BelongsToProxy" do
5
+ before do
6
6
  @post_class = Doc()
7
7
  @comment_class = Doc do
8
8
  key :post_id, ObjectId
9
9
  end
10
-
11
10
  @comment_class.belongs_to :post, :class => @post_class
12
11
  end
13
12
 
14
- should "default to nil" do
13
+ it "should default to nil" do
15
14
  @comment_class.new.post.nil?.should be_true
16
15
  end
17
16
 
18
- should "return nil instead of a proxy" do
17
+ it "should return nil instead of a proxy" do
19
18
  nil.should === @comment_class.new.post
20
19
  end
21
20
 
22
- should "have boolean presence method" do
21
+ it "should have boolean presence method" do
23
22
  comment = @comment_class.new(:name => 'Foo!')
24
23
  comment.post?.should be_false
25
24
 
@@ -27,7 +26,7 @@ class BelongsToProxyTest < Test::Unit::TestCase
27
26
  comment.post?.should be_true
28
27
  end
29
28
 
30
- should "allow overriding association methods" do
29
+ it "should allow overriding association methods" do
31
30
  @comment_class.class_eval do
32
31
  def post?
33
32
  super
@@ -40,7 +39,7 @@ class BelongsToProxyTest < Test::Unit::TestCase
40
39
  instance.post?.should be_true
41
40
  end
42
41
 
43
- should "be able to replace the association" do
42
+ it "should be able to replace the association" do
44
43
  post = @post_class.new(:name => 'mongomapper')
45
44
  comment = @comment_class.new(:name => 'Foo!', :post => post)
46
45
  comment.save.should be_true
@@ -49,25 +48,25 @@ class BelongsToProxyTest < Test::Unit::TestCase
49
48
  comment.post.should == post
50
49
  comment.post.nil?.should be_false
51
50
  end
52
-
53
- should "not reload the association when replacing" do
51
+
52
+ it "should not reload the association when replacing" do
54
53
  post = @post_class.new(:name => 'mongomapper')
55
54
  comment = @comment_class.new(:name => 'Foo!', :post => post)
56
55
  comment.post.proxy_target.object_id.should == post.object_id
57
56
  end
58
57
 
59
- should "properly assign the associated object when assigning the association with create" do
58
+ it "should properly assign the associated object when assigning the association with create" do
60
59
  child_class = Doc('Child')
61
60
  parent_class = Doc('Parent')
62
-
61
+
63
62
  parent_class.one :child, :class => child_class
64
63
  child_class.belongs_to :parent, :class => parent_class
65
-
64
+
66
65
  child = child_class.create(:parent => parent_class.create)
67
66
  child.parent.child.should == child
68
67
  end
69
68
 
70
- should "generate a new proxy when replacing the association" do
69
+ it "should generate a new proxy when replacing the association" do
71
70
  post1 = @post_class.create(:name => 'post1')
72
71
  post2 = @post_class.create(:name => 'post2')
73
72
 
@@ -87,7 +86,7 @@ class BelongsToProxyTest < Test::Unit::TestCase
87
86
  original_post.name.should == 'post1'
88
87
  end
89
88
 
90
- should "unset the association" do
89
+ it "should unset the association" do
91
90
  post = @post_class.new(:name => 'mongomapper')
92
91
  comment = @comment_class.new(:name => 'Foo!', :post => post)
93
92
  comment.save.should be_true
@@ -97,19 +96,19 @@ class BelongsToProxyTest < Test::Unit::TestCase
97
96
  comment.post.nil?.should be_true
98
97
  end
99
98
 
100
- should "return nil if id set but document not found" do
99
+ it "should return nil if id set but document not found" do
101
100
  id = BSON::ObjectId.new
102
101
  @comment_class.new(:name => 'Foo', :post_id => id).post.nil?.should be_true
103
102
  end
104
103
 
105
- should "define foreign key if it doesn't exist" do
104
+ it "should define foreign key if it doesn't exist" do
106
105
  @category_class = Doc()
107
106
  @post_class.belongs_to :category, :class => @category_class
108
107
 
109
108
  @post_class.key?(:category_id).should be_true
110
109
  end
111
110
 
112
- should "not define foreign key if it already exists" do
111
+ it "should not define foreign key if it already exists" do
113
112
  @category_class = Doc()
114
113
  @post_class.key :category_id, String
115
114
  @post_class.belongs_to :category, :class => @category_class
@@ -118,7 +117,7 @@ class BelongsToProxyTest < Test::Unit::TestCase
118
117
  end
119
118
 
120
119
  context ":dependent" do
121
- setup do
120
+ before do
122
121
  # FIXME: make use of already defined models
123
122
  class ::Property
124
123
  include MongoMapper::Document
@@ -132,13 +131,13 @@ class BelongsToProxyTest < Test::Unit::TestCase
132
131
  Thing.collection.remove
133
132
  end
134
133
 
135
- teardown do
134
+ after do
136
135
  Object.send :remove_const, 'Property' if defined?(::Property)
137
136
  Object.send :remove_const, 'Thing' if defined?(::Thing)
138
137
  end
139
138
 
140
139
  context "=> destroy" do
141
- setup do
140
+ before do
142
141
  Property.key :thing_id, ObjectId
143
142
  Property.belongs_to :thing, :dependent => :destroy
144
143
  Thing.many :properties
@@ -152,7 +151,7 @@ class BelongsToProxyTest < Test::Unit::TestCase
152
151
  @thing.properties << @property3
153
152
  end
154
153
 
155
- should "not execute on a belongs_to association" do
154
+ it "should not execute on a belongs_to association" do
156
155
  Thing.count.should == 1
157
156
  @property1.destroy
158
157
  Thing.count.should == 1
@@ -160,60 +159,79 @@ class BelongsToProxyTest < Test::Unit::TestCase
160
159
  end
161
160
  end
162
161
 
163
- should "be able to build" do
164
- @comment_class.belongs_to :post, :class => @post_class
165
-
166
- comment = @comment_class.create
167
- post = comment.build_post(:title => 'Hello, world!')
168
- comment.post.should be_instance_of(@post_class)
169
- comment.post.should be_new
170
- comment.post.title.should == 'Hello, world!'
171
- comment.post.should == post
172
- comment.post_id.should == post.id
173
- end
174
-
175
- should "be able to create" do
176
- @comment_class.belongs_to :post, :class => @post_class
177
-
178
- comment = @comment_class.create
179
- post = comment.create_post(:title => 'Hello, world!')
180
- comment.post.should be_instance_of(@post_class)
181
- comment.post.should_not be_new
182
- comment.post.title.should == 'Hello, world!'
183
- comment.post.should == post
184
- comment.post_id.should == post.id
185
- end
162
+ context "when creating documents" do
163
+ let(:comment) { @comment_class.create }
186
164
 
187
- context "#create!" do
188
- setup do
165
+ before do
189
166
  @post_class.key :title, String, :required => true
190
167
  @comment_class.belongs_to :post, :class => @post_class
191
168
  end
192
169
 
193
- should "raise exception if invalid" do
194
- comment = @comment_class.create
195
- assert_raises(MongoMapper::DocumentNotValid) do
196
- comment.create_post!
170
+ context "#build" do
171
+ it "should work" do
172
+ post = comment.build_post(:title => 'Hello, world!')
173
+ comment.post.should be_instance_of(@post_class)
174
+ comment.post.should be_new
175
+ comment.post.title.should == 'Hello, world!'
176
+ comment.post.should == post
177
+ comment.post_id.should == post.id
178
+ end
179
+
180
+ it "should accept a block" do
181
+ comment.build_post(:title => 'Hello, world!') do |post|
182
+ post.title = "Hello world!"
183
+ end
184
+ comment.post.title.should == "Hello world!"
197
185
  end
198
186
  end
199
187
 
200
- should "work if valid" do
201
- comment = @comment_class.create
202
- post = comment.create_post!(:title => 'Hello, world!')
203
- comment.post.should be_instance_of(@post_class)
204
- comment.post.should_not be_new
205
- comment.post.title.should == 'Hello, world!'
206
- comment.post.should == post
207
- comment.post_id.should == post.id
188
+ context "#create" do
189
+ it "should work" do
190
+ post = comment.create_post(:title => 'Hello, world!')
191
+ comment.post.should be_instance_of(@post_class)
192
+ comment.post.should_not be_new
193
+ comment.post.title.should == 'Hello, world!'
194
+ comment.post.should == post
195
+ comment.post_id.should == post.id
196
+ end
197
+
198
+ it "should accept a block" do
199
+ comment.create_post(:title => 'Hello, world!') do |post|
200
+ post.title = "Hello world!"
201
+ end
202
+ comment.post.title.should == "Hello world!"
203
+ end
204
+ end
205
+
206
+ context "#create!" do
207
+ it "should accept a block" do
208
+ comment.create_post! do |post|
209
+ post.title = "Hello world!"
210
+ end
211
+ comment.post.title.should == "Hello world!"
212
+ end
213
+
214
+ it "should raise exception if invalid" do
215
+ expect { comment.create_post! }.to raise_error(MongoMapper::DocumentNotValid)
216
+ end
217
+
218
+ it "should work if valid" do
219
+ post = comment.create_post!(:title => 'Hello, world!')
220
+ comment.post.should be_instance_of(@post_class)
221
+ comment.post.should_not be_new
222
+ comment.post.title.should == 'Hello, world!'
223
+ comment.post.should == post
224
+ comment.post_id.should == post.id
225
+ end
208
226
  end
209
227
  end
210
228
 
211
229
  context 'autosave' do
212
- should 'not be true by default' do
230
+ it 'should not be true by default' do
213
231
  @comment_class.associations[:post].options[:autosave].should_not be_true
214
232
  end
215
233
 
216
- should 'save parent changes when true' do
234
+ it 'should save parent changes when true' do
217
235
  @comment_class.associations[:post].options[:autosave] = true
218
236
 
219
237
  comment = @comment_class.create
@@ -225,7 +243,7 @@ class BelongsToProxyTest < Test::Unit::TestCase
225
243
  post.reload.title.should == 'Hi, world.'
226
244
  end
227
245
 
228
- should 'not save parent changes when false' do
246
+ it 'should not save parent changes when false' do
229
247
  comment = @comment_class.create
230
248
  post = comment.create_post(:title => 'Hello, world!')
231
249