mongo_mapper 0.10.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/UPGRADES +3 -0
  2. data/examples/plugins.rb +2 -5
  3. data/lib/mongo_mapper.rb +1 -0
  4. data/lib/mongo_mapper/connection.rb +4 -0
  5. data/lib/mongo_mapper/embedded_document.rb +1 -0
  6. data/lib/mongo_mapper/extensions/object.rb +5 -6
  7. data/lib/mongo_mapper/plugins/accessible.rb +14 -16
  8. data/lib/mongo_mapper/plugins/associations.rb +21 -23
  9. data/lib/mongo_mapper/plugins/associations/base.rb +1 -1
  10. data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +4 -0
  11. data/lib/mongo_mapper/plugins/associations/many_association.rb +0 -4
  12. data/lib/mongo_mapper/plugins/associations/one_association.rb +1 -1
  13. data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +30 -0
  14. data/lib/mongo_mapper/plugins/caching.rb +11 -13
  15. data/lib/mongo_mapper/plugins/callbacks.rb +12 -14
  16. data/lib/mongo_mapper/plugins/clone.rb +11 -13
  17. data/lib/mongo_mapper/plugins/dirty.rb +36 -38
  18. data/lib/mongo_mapper/plugins/document.rb +20 -22
  19. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +11 -13
  20. data/lib/mongo_mapper/plugins/embedded_document.rb +22 -24
  21. data/lib/mongo_mapper/plugins/equality.rb +6 -8
  22. data/lib/mongo_mapper/plugins/identity_map.rb +11 -13
  23. data/lib/mongo_mapper/plugins/inspect.rb +6 -8
  24. data/lib/mongo_mapper/plugins/keys.rb +109 -110
  25. data/lib/mongo_mapper/plugins/logger.rb +2 -4
  26. data/lib/mongo_mapper/plugins/modifiers.rb +32 -34
  27. data/lib/mongo_mapper/plugins/persistence.rb +5 -7
  28. data/lib/mongo_mapper/plugins/protected.rb +14 -16
  29. data/lib/mongo_mapper/plugins/querying.rb +29 -31
  30. data/lib/mongo_mapper/plugins/rails.rb +28 -22
  31. data/lib/mongo_mapper/plugins/rails/active_record_association_adapter.rb +33 -0
  32. data/lib/mongo_mapper/plugins/safe.rb +3 -5
  33. data/lib/mongo_mapper/plugins/sci.rb +3 -5
  34. data/lib/mongo_mapper/plugins/serialization.rb +49 -52
  35. data/lib/mongo_mapper/plugins/timestamps.rb +4 -6
  36. data/lib/mongo_mapper/plugins/validations.rb +8 -10
  37. data/lib/mongo_mapper/railtie/database.rake +1 -1
  38. data/lib/mongo_mapper/version.rb +1 -1
  39. data/lib/rails/generators/mongo_mapper/model/model_generator.rb +1 -1
  40. data/lib/rails/generators/mongo_mapper/model/templates/model.rb +2 -0
  41. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +63 -0
  42. data/test/functional/associations/test_one_embedded_polymorphic_proxy.rb +208 -0
  43. data/test/functional/test_accessible.rb +5 -0
  44. data/test/functional/test_embedded_document.rb +12 -0
  45. data/test/functional/test_modifiers.rb +19 -19
  46. data/test/functional/test_protected.rb +10 -0
  47. data/test/functional/test_querying.rb +15 -1
  48. data/test/functional/test_validations.rb +33 -0
  49. data/test/models.rb +14 -0
  50. data/test/unit/associations/test_one_association.rb +11 -0
  51. data/test/unit/test_mongo_mapper.rb +9 -0
  52. data/test/unit/test_plugins.rb +2 -4
  53. data/test/unit/test_rails_reflect_on_association.rb +118 -0
  54. metadata +15 -11
@@ -74,6 +74,11 @@ class ProtectedTest < Test::Unit::TestCase
74
74
  doc.name.should == 'John'
75
75
  end
76
76
 
77
+ should "ignore protected attribute on #update_attribute" do
78
+ @doc.update_attribute('admin', true)
79
+ @doc.admin.should be_true
80
+ end
81
+
77
82
  should "ignore protected attribute on #update_attributes" do
78
83
  @doc.update_attributes(:name => 'Ren Hoek', :admin => true)
79
84
  @doc.name.should == 'Ren Hoek'
@@ -176,6 +181,11 @@ class ProtectedTest < Test::Unit::TestCase
176
181
  @edoc.admin.should be_true
177
182
  end
178
183
 
184
+ should "not ignore protected attribute on #update_attribute" do
185
+ @edoc.update_attribute('admin', true)
186
+ @edoc.admin.should be_true
187
+ end
188
+
179
189
  should "ignore protected attribute on #update_attributes" do
180
190
  @edoc.update_attributes(:name => 'Ren Hoek', :admin => true)
181
191
  @edoc.name.should == 'Ren Hoek'
@@ -682,10 +682,24 @@ class QueryingTesting < Test::Unit::TestCase
682
682
  @doc = @document.create(:first_name => 'John', :age => '27')
683
683
  end
684
684
 
685
- should "update the attribute" do
685
+ should "accept symbols as keys" do
686
686
  @doc.update_attribute(:first_name, 'Chris').should be_true
687
687
  @doc.reload.first_name.should == 'Chris'
688
688
  end
689
+
690
+ should "update the attribute" do
691
+ @doc.update_attribute('first_name', 'Chris').should be_true
692
+ @doc.reload.first_name.should == 'Chris'
693
+ end
694
+
695
+ should "update the attribute without invoking validations" do
696
+ @document.key :name, String, :required => true
697
+
698
+ @doc.expects(:valid?).never
699
+ @doc.update_attribute('name', '').should be_true
700
+ @doc.reload.name.should == ''
701
+ @document.count.should == 1
702
+ end
689
703
  end
690
704
 
691
705
  context "#save (new document)" do
@@ -350,6 +350,39 @@ class ValidationsTest < Test::Unit::TestCase
350
350
  doc.children.build
351
351
  doc.should have_error_on(:children, 'are invalid')
352
352
  end
353
+
354
+ end
355
+
356
+ context "validating associated docs with custom context" do
357
+ setup do
358
+ @child_class = EDoc do
359
+ key :name
360
+
361
+ validates_length_of :name, :minimum => 5, :on => :custom_context
362
+ end
363
+
364
+ @root_class = Doc { }
365
+ @root_class.many :children, :class => @child_class
366
+ @root_class.validates_associated :children, :context => :custom_context
367
+ end
368
+
369
+ should "pass if there are no associated docs" do
370
+ doc = @root_class.new
371
+ doc.valid?(:custom_context).should be_true
372
+ end
373
+
374
+ should "pass if the associated doc is valid" do
375
+ doc = @root_class.new
376
+ doc.children.build(:name => 'George')
377
+ doc.valid?(:custom_context).should be_true
378
+ end
379
+
380
+ should "fail if the associated doc is invalid" do
381
+ doc = @root_class.new
382
+ doc.children.build(:name => 'Bob')
383
+ doc.valid?(:custom_context).should_not be_true
384
+ end
385
+
353
386
  end
354
387
  # context "validates uniqueness of with :unique shortcut" do
355
388
  # should "work" do
@@ -245,3 +245,17 @@ end
245
245
  class AltUser
246
246
  include MongoMapper::Document
247
247
  end
248
+
249
+ class Human
250
+ include MongoMapper::EmbeddedDocument
251
+
252
+ key :name, String
253
+ embedded_in :post
254
+ end
255
+
256
+ class Robot
257
+ include MongoMapper::EmbeddedDocument
258
+
259
+ key :serial_number, String
260
+ embedded_in :post
261
+ end
@@ -4,6 +4,12 @@ require 'models'
4
4
  class OneAssociationTest < Test::Unit::TestCase
5
5
  include MongoMapper::Plugins::Associations
6
6
 
7
+ context "type_key_name" do
8
+ should "be _type" do
9
+ OneAssociation.new(:foo).type_key_name.should == '_type'
10
+ end
11
+ end
12
+
7
13
  context "embeddable?" do
8
14
  should "be true if class is embeddable" do
9
15
  base = OneAssociation.new(:media)
@@ -31,6 +37,11 @@ class OneAssociationTest < Test::Unit::TestCase
31
37
  base = OneAssociation.new(:media)
32
38
  base.proxy_class.should == OneEmbeddedProxy
33
39
  end
40
+
41
+ should "be OneEmbeddedPolymorphicProxy for polymorphic one embedded" do
42
+ base = OneAssociation.new(:media, :polymorphic => true)
43
+ base.proxy_class.should == OneEmbeddedPolymorphicProxy
44
+ end
34
45
  end
35
46
 
36
47
  end
@@ -65,6 +65,15 @@ class MongoMapperTest < Test::Unit::TestCase
65
65
  MongoMapper.connect('development', :logger => logger)
66
66
  end
67
67
 
68
+ should "work with options from config" do
69
+ MongoMapper.config = {
70
+ 'development' => {'host' => '192.168.1.1', 'port' => 2222, 'database' => 'test', 'options' => {'safe' => true}}
71
+ }
72
+ connection, logger = mock('connection'), mock('logger')
73
+ Mongo::Connection.expects(:new).with('192.168.1.1', 2222, :logger => logger, :safe => true)
74
+ MongoMapper.connect('development', :logger => logger)
75
+ end
76
+
68
77
  should "work with options using uri" do
69
78
  MongoMapper.config = {
70
79
  'development' => {'uri' => 'mongodb://127.0.0.1:27017/test'}
@@ -19,10 +19,8 @@ class PluginsTest < Test::Unit::TestCase
19
19
  end
20
20
  end
21
21
 
22
- module InstanceMethods
23
- def instance_foo
24
- 'instance_foo'
25
- end
22
+ def instance_foo
23
+ 'instance_foo'
26
24
  end
27
25
  end
28
26
 
@@ -0,0 +1,118 @@
1
+ require 'test_helper'
2
+
3
+ module ReflectOnAssociationTestModels
4
+ class Tree
5
+ include MongoMapper::Document
6
+ many :birds, :class_name => "ReflectOnAssociationTestModels::Bird"
7
+ end
8
+
9
+ class Bird
10
+ include MongoMapper::Document
11
+ belongs_to :tree, :class_name => "ReflectOnAssociationTestModels::Tree"
12
+ end
13
+
14
+ class Book
15
+ include MongoMapper::Document
16
+ many :authors, :class_name => "ReflectOnAssociationTestModels::Author", :in => :author_ids
17
+ end
18
+
19
+ class Author
20
+ include MongoMapper::Document
21
+ end
22
+
23
+ class Employee
24
+ include MongoMapper::Document
25
+ one :desk, :class_name => "ReflectOnAssociationTestModels::Desk"
26
+ end
27
+
28
+ class Desk
29
+ include MongoMapper::Document
30
+ belongs_to :employee, :class_name => "ReflectOnAssociationTestModels::Employee"
31
+ end
32
+
33
+ class Order
34
+ include MongoMapper::Document
35
+ many :line_items, :class_name => "ReflectOnAssociationTestModels::LineItem"
36
+ end
37
+
38
+ class LineItem
39
+ include MongoMapper::EmbeddedDocument
40
+ end
41
+
42
+ class Body
43
+ include MongoMapper::Document
44
+ one :heart, :class_name => "ReflectOnAssociationTestModels::Heart"
45
+ end
46
+
47
+ class Heart
48
+ include MongoMapper::EmbeddedDocument
49
+ end
50
+ end
51
+
52
+ class ReflectOnAssociationTest < Test::Unit::TestCase
53
+ context "one-to-many association" do
54
+ should "return :has_many association for Tree#birds" do
55
+ association = ReflectOnAssociationTestModels::Tree.reflect_on_association(:birds)
56
+ association.klass.should == ReflectOnAssociationTestModels::Bird
57
+ association.macro.should == :has_many
58
+ association.name.should == :birds
59
+ association.options.should == {}
60
+ end
61
+
62
+ should "return :belongs_to association for Bird#tree" do
63
+ association = ReflectOnAssociationTestModels::Bird.reflect_on_association(:tree)
64
+ association.klass.should == ReflectOnAssociationTestModels::Tree
65
+ association.macro.should == :belongs_to
66
+ association.name.should == :tree
67
+ association.options.should == {}
68
+ end
69
+ end
70
+
71
+ context "many-to-many association" do
72
+ should "return :has_many association for Book#authors" do
73
+ association = ReflectOnAssociationTestModels::Book.reflect_on_association(:authors)
74
+ association.klass.should == ReflectOnAssociationTestModels::Author
75
+ association.macro.should == :has_many
76
+ association.name.should == :authors
77
+ association.options.should == {}
78
+ end
79
+ end
80
+
81
+ context "one-to-one association" do
82
+ should "return :has_one association for Employee#desk" do
83
+ association = ReflectOnAssociationTestModels::Employee.reflect_on_association(:desk)
84
+ association.klass.should == ReflectOnAssociationTestModels::Desk
85
+ association.macro.should == :has_one
86
+ association.name.should == :desk
87
+ association.options.should == {}
88
+ end
89
+
90
+ should "return :belongs_to association for Desk#employee" do
91
+ association = ReflectOnAssociationTestModels::Desk.reflect_on_association(:employee)
92
+ association.klass.should == ReflectOnAssociationTestModels::Employee
93
+ association.macro.should == :belongs_to
94
+ association.name.should == :employee
95
+ association.options.should == {}
96
+ end
97
+ end
98
+
99
+ context "embeds one" do
100
+ should "return :has_one association for Body#heart" do
101
+ association = ReflectOnAssociationTestModels::Body.reflect_on_association(:heart)
102
+ association.klass.should == ReflectOnAssociationTestModels::Heart
103
+ association.macro.should == :has_one
104
+ association.name.should == :heart
105
+ association.options.should == {}
106
+ end
107
+ end
108
+
109
+ context "embeds many" do
110
+ should "return :has_many association for Order#line_items" do
111
+ association = ReflectOnAssociationTestModels::Order.reflect_on_association(:line_items)
112
+ association.klass.should == ReflectOnAssociationTestModels::LineItem
113
+ association.macro.should == :has_many
114
+ association.name.should == :line_items
115
+ association.options.should == {}
116
+ end
117
+ end
118
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 53
4
+ hash: 51
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 10
9
- - 1
10
- version: 0.10.1
8
+ - 11
9
+ - 0
10
+ version: 0.11.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Nunemaker
@@ -15,11 +15,13 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-07 00:00:00 -05:00
18
+ date: 2012-01-26 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
+ type: :runtime
22
23
  name: activemodel
24
+ prerelease: false
23
25
  version_requirements: &id001 !ruby/object:Gem::Requirement
24
26
  none: false
25
27
  requirements:
@@ -30,11 +32,11 @@ dependencies:
30
32
  - 3
31
33
  - 0
32
34
  version: "3.0"
33
- prerelease: false
34
- type: :runtime
35
35
  requirement: *id001
36
36
  - !ruby/object:Gem::Dependency
37
+ type: :runtime
37
38
  name: activesupport
39
+ prerelease: false
38
40
  version_requirements: &id002 !ruby/object:Gem::Requirement
39
41
  none: false
40
42
  requirements:
@@ -45,11 +47,11 @@ dependencies:
45
47
  - 3
46
48
  - 0
47
49
  version: "3.0"
48
- prerelease: false
49
- type: :runtime
50
50
  requirement: *id002
51
51
  - !ruby/object:Gem::Dependency
52
+ type: :runtime
52
53
  name: plucky
54
+ prerelease: false
53
55
  version_requirements: &id003 !ruby/object:Gem::Requirement
54
56
  none: false
55
57
  requirements:
@@ -61,8 +63,6 @@ dependencies:
61
63
  - 4
62
64
  - 0
63
65
  version: 0.4.0
64
- prerelease: false
65
- type: :runtime
66
66
  requirement: *id003
67
67
  description:
68
68
  email:
@@ -125,6 +125,7 @@ files:
125
125
  - lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb
126
126
  - lib/mongo_mapper/plugins/associations/one_as_proxy.rb
127
127
  - lib/mongo_mapper/plugins/associations/one_association.rb
128
+ - lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb
128
129
  - lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb
129
130
  - lib/mongo_mapper/plugins/associations/one_proxy.rb
130
131
  - lib/mongo_mapper/plugins/associations/proxy.rb
@@ -153,6 +154,7 @@ files:
153
154
  - lib/mongo_mapper/plugins/querying/decorator.rb
154
155
  - lib/mongo_mapper/plugins/querying/plucky_methods.rb
155
156
  - lib/mongo_mapper/plugins/querying.rb
157
+ - lib/mongo_mapper/plugins/rails/active_record_association_adapter.rb
156
158
  - lib/mongo_mapper/plugins/rails.rb
157
159
  - lib/mongo_mapper/plugins/safe.rb
158
160
  - lib/mongo_mapper/plugins/sci.rb
@@ -181,6 +183,7 @@ files:
181
183
  - test/functional/associations/test_many_embedded_proxy.rb
182
184
  - test/functional/associations/test_many_polymorphic_proxy.rb
183
185
  - test/functional/associations/test_one_as_proxy.rb
186
+ - test/functional/associations/test_one_embedded_polymorphic_proxy.rb
184
187
  - test/functional/associations/test_one_embedded_proxy.rb
185
188
  - test/functional/associations/test_one_proxy.rb
186
189
  - test/functional/test_accessible.rb
@@ -235,6 +238,7 @@ files:
235
238
  - test/unit/test_plugins.rb
236
239
  - test/unit/test_rails.rb
237
240
  - test/unit/test_rails_compatibility.rb
241
+ - test/unit/test_rails_reflect_on_association.rb
238
242
  - test/unit/test_railtie.rb
239
243
  - test/unit/test_serialization.rb
240
244
  - test/unit/test_time_zones.rb