jamieorc-mongo_mapper 0.11.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +33 -0
- data/UPGRADES +26 -0
- data/bin/mmconsole +59 -0
- data/examples/attr_accessible.rb +22 -0
- data/examples/attr_protected.rb +22 -0
- data/examples/cache_key.rb +24 -0
- data/examples/custom_types.rb +24 -0
- data/examples/identity_map.rb +33 -0
- data/examples/identity_map/automatic.rb +2 -0
- data/examples/keys.rb +40 -0
- data/examples/modifiers/set.rb +25 -0
- data/examples/plugins.rb +38 -0
- data/examples/querying.rb +35 -0
- data/examples/safe.rb +43 -0
- data/examples/scopes.rb +52 -0
- data/examples/validating/embedded_docs.rb +29 -0
- data/lib/mongo_mapper.rb +94 -0
- data/lib/mongo_mapper/connection.rb +96 -0
- data/lib/mongo_mapper/document.rb +42 -0
- data/lib/mongo_mapper/embedded_document.rb +32 -0
- data/lib/mongo_mapper/exceptions.rb +27 -0
- data/lib/mongo_mapper/extensions/array.rb +19 -0
- data/lib/mongo_mapper/extensions/binary.rb +22 -0
- data/lib/mongo_mapper/extensions/boolean.rb +44 -0
- data/lib/mongo_mapper/extensions/date.rb +25 -0
- data/lib/mongo_mapper/extensions/float.rb +14 -0
- data/lib/mongo_mapper/extensions/hash.rb +14 -0
- data/lib/mongo_mapper/extensions/integer.rb +19 -0
- data/lib/mongo_mapper/extensions/kernel.rb +9 -0
- data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
- data/lib/mongo_mapper/extensions/object.rb +26 -0
- data/lib/mongo_mapper/extensions/object_id.rb +32 -0
- data/lib/mongo_mapper/extensions/set.rb +20 -0
- data/lib/mongo_mapper/extensions/string.rb +18 -0
- data/lib/mongo_mapper/extensions/time.rb +28 -0
- 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 +22 -0
- data/lib/mongo_mapper/plugins/accessible.rb +36 -0
- data/lib/mongo_mapper/plugins/active_model.rb +18 -0
- data/lib/mongo_mapper/plugins/associations.rb +90 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +92 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +53 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +34 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +52 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +27 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +44 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +133 -0
- data/lib/mongo_mapper/plugins/associations/many_association.rb +63 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +117 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
- data/lib/mongo_mapper/plugins/associations/one_as_proxy.rb +22 -0
- data/lib/mongo_mapper/plugins/associations/one_association.rb +48 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +30 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +44 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +95 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +134 -0
- data/lib/mongo_mapper/plugins/associations/single_association.rb +46 -0
- data/lib/mongo_mapper/plugins/caching.rb +21 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +29 -0
- data/lib/mongo_mapper/plugins/clone.rb +22 -0
- data/lib/mongo_mapper/plugins/dirty.rb +60 -0
- data/lib/mongo_mapper/plugins/document.rb +41 -0
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +45 -0
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +56 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +53 -0
- data/lib/mongo_mapper/plugins/equality.rb +23 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
- data/lib/mongo_mapper/plugins/indexes.rb +13 -0
- data/lib/mongo_mapper/plugins/inspect.rb +16 -0
- data/lib/mongo_mapper/plugins/keys.rb +313 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +61 -0
- data/lib/mongo_mapper/plugins/logger.rb +18 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +132 -0
- data/lib/mongo_mapper/plugins/pagination.rb +16 -0
- data/lib/mongo_mapper/plugins/persistence.rb +69 -0
- data/lib/mongo_mapper/plugins/protected.rb +45 -0
- data/lib/mongo_mapper/plugins/querying.rb +171 -0
- data/lib/mongo_mapper/plugins/querying/decorator.rb +34 -0
- data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +21 -0
- data/lib/mongo_mapper/plugins/rails.rb +58 -0
- data/lib/mongo_mapper/plugins/rails/active_record_association_adapter.rb +33 -0
- data/lib/mongo_mapper/plugins/safe.rb +28 -0
- data/lib/mongo_mapper/plugins/sci.rb +36 -0
- data/lib/mongo_mapper/plugins/scopes.rb +27 -0
- data/lib/mongo_mapper/plugins/serialization.rb +109 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
- data/lib/mongo_mapper/plugins/touch.rb +18 -0
- data/lib/mongo_mapper/plugins/userstamps.rb +18 -0
- data/lib/mongo_mapper/plugins/validations.rb +86 -0
- data/lib/mongo_mapper/railtie.rb +48 -0
- data/lib/mongo_mapper/railtie/database.rake +65 -0
- data/lib/mongo_mapper/translation.rb +10 -0
- data/lib/mongo_mapper/version.rb +4 -0
- 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 +13 -0
- data/test/_NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +64 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +238 -0
- data/test/functional/associations/test_in_array_proxy.rb +349 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
- data/test/functional/associations/test_many_documents_proxy.rb +866 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +239 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +289 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +303 -0
- data/test/functional/associations/test_one_as_proxy.rb +491 -0
- data/test/functional/associations/test_one_embedded_polymorphic_proxy.rb +208 -0
- data/test/functional/associations/test_one_embedded_proxy.rb +100 -0
- data/test/functional/associations/test_one_proxy.rb +383 -0
- data/test/functional/test_accessible.rb +198 -0
- data/test/functional/test_associations.rb +46 -0
- data/test/functional/test_binary.rb +27 -0
- data/test/functional/test_caching.rb +77 -0
- data/test/functional/test_callbacks.rb +232 -0
- data/test/functional/test_dirty.rb +301 -0
- data/test/functional/test_document.rb +282 -0
- data/test/functional/test_dynamic_querying.rb +75 -0
- data/test/functional/test_embedded_document.rb +288 -0
- data/test/functional/test_equality.rb +20 -0
- data/test/functional/test_identity_map.rb +513 -0
- data/test/functional/test_indexes.rb +50 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_modifiers.rb +483 -0
- data/test/functional/test_pagination.rb +91 -0
- data/test/functional/test_protected.rb +201 -0
- data/test/functional/test_querying.rb +935 -0
- data/test/functional/test_safe.rb +76 -0
- data/test/functional/test_sci.rb +240 -0
- data/test/functional/test_scopes.rb +171 -0
- data/test/functional/test_timestamps.rb +62 -0
- data/test/functional/test_touch.rb +125 -0
- data/test/functional/test_userstamps.rb +44 -0
- data/test/functional/test_validations.rb +405 -0
- data/test/models.rb +261 -0
- data/test/support/railtie.rb +4 -0
- data/test/support/railtie/autoloaded.rb +2 -0
- data/test/support/railtie/not_autoloaded.rb +3 -0
- data/test/support/railtie/parent.rb +3 -0
- data/test/test_active_model_lint.rb +18 -0
- data/test/test_helper.rb +94 -0
- data/test/unit/associations/test_base.rb +146 -0
- 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 +47 -0
- data/test/unit/associations/test_proxy.rb +100 -0
- data/test/unit/serializers/test_json_serializer.rb +216 -0
- data/test/unit/serializers/test_xml_serializer.rb +196 -0
- data/test/unit/test_clone.rb +69 -0
- data/test/unit/test_document.rb +249 -0
- data/test/unit/test_dynamic_finder.rb +125 -0
- data/test/unit/test_embedded_document.rb +682 -0
- data/test/unit/test_equality.rb +38 -0
- data/test/unit/test_extensions.rb +380 -0
- data/test/unit/test_identity_map_middleware.rb +34 -0
- data/test/unit/test_inspect.rb +47 -0
- data/test/unit/test_key.rb +205 -0
- data/test/unit/test_keys.rb +65 -0
- data/test/unit/test_mongo_mapper.rb +143 -0
- data/test/unit/test_pagination.rb +11 -0
- data/test/unit/test_plugins.rb +89 -0
- data/test/unit/test_rails.rb +183 -0
- data/test/unit/test_rails_compatibility.rb +38 -0
- data/test/unit/test_rails_reflect_on_association.rb +118 -0
- data/test/unit/test_railtie.rb +61 -0
- data/test/unit/test_serialization.rb +166 -0
- data/test/unit/test_time_zones.rb +44 -0
- data/test/unit/test_translation.rb +27 -0
- data/test/unit/test_validations.rb +562 -0
- metadata +262 -0
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TimestampsTest < Test::Unit::TestCase
|
4
|
+
context "timestamping" do
|
5
|
+
setup do
|
6
|
+
@klass = Doc do
|
7
|
+
key :first_name, String
|
8
|
+
key :last_name, String
|
9
|
+
key :age, Integer
|
10
|
+
key :date, Date
|
11
|
+
end
|
12
|
+
@klass.timestamps!
|
13
|
+
end
|
14
|
+
|
15
|
+
should "set created_at and updated_at on create" do
|
16
|
+
doc = @klass.new(:first_name => 'John', :age => 27)
|
17
|
+
doc.created_at.should be(nil)
|
18
|
+
doc.updated_at.should be(nil)
|
19
|
+
doc.save
|
20
|
+
doc.created_at.should_not be(nil)
|
21
|
+
doc.updated_at.should_not be(nil)
|
22
|
+
end
|
23
|
+
|
24
|
+
should "not overwrite created_at if it already exists" do
|
25
|
+
original_created_at = 1.month.ago
|
26
|
+
doc = @klass.new(:first_name => 'John', :age => 27, :created_at => original_created_at)
|
27
|
+
doc.created_at.to_i.should == original_created_at.to_i
|
28
|
+
doc.updated_at.should be_nil
|
29
|
+
doc.save
|
30
|
+
doc.created_at.to_i.should == original_created_at.to_i
|
31
|
+
doc.updated_at.should_not be_nil
|
32
|
+
end
|
33
|
+
|
34
|
+
should "set updated_at on field update but leave created_at alone" do
|
35
|
+
doc = @klass.create(:first_name => 'John', :age => 27)
|
36
|
+
old_created_at = doc.created_at
|
37
|
+
old_updated_at = doc.updated_at
|
38
|
+
doc.first_name = 'Johnny'
|
39
|
+
|
40
|
+
Timecop.freeze(Time.now + 5.seconds) do
|
41
|
+
doc.save
|
42
|
+
end
|
43
|
+
|
44
|
+
doc.created_at.should == old_created_at
|
45
|
+
doc.updated_at.should_not == old_updated_at
|
46
|
+
end
|
47
|
+
|
48
|
+
should "set updated_at on document update but leave created_at alone" do
|
49
|
+
doc = @klass.create(:first_name => 'John', :age => 27)
|
50
|
+
old_created_at = doc.created_at.to_f
|
51
|
+
|
52
|
+
new_updated_at = Time.now + 5.seconds
|
53
|
+
Timecop.freeze(new_updated_at) do
|
54
|
+
@klass.update(doc._id, { :first_name => 'Johnny' })
|
55
|
+
end
|
56
|
+
|
57
|
+
doc = doc.reload
|
58
|
+
doc.created_at.to_f.should be_close(old_created_at, 0.001)
|
59
|
+
doc.updated_at.to_f.should be_close(new_updated_at.to_f, 0.001)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TouchTest < Test::Unit::TestCase
|
4
|
+
context "touch" do
|
5
|
+
context "document" do
|
6
|
+
setup do
|
7
|
+
@document = Doc { timestamps! }
|
8
|
+
end
|
9
|
+
|
10
|
+
should "update the updated_at timestamp" do
|
11
|
+
doc = @document.create
|
12
|
+
old_updated_at = doc.updated_at
|
13
|
+
|
14
|
+
Timecop.freeze(Time.now + 1.day) do
|
15
|
+
doc.touch
|
16
|
+
end
|
17
|
+
|
18
|
+
doc.reload
|
19
|
+
doc.updated_at.should_not == old_updated_at
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "embedded document" do
|
24
|
+
should "update the updated_at timestamp" do
|
25
|
+
Doc = Doc("Document") { timestamps!}
|
26
|
+
Emdoc = EDoc("EmbeddedDocument") { timestamps! }
|
27
|
+
Doc.has_many :emdocs, :class => Emdoc
|
28
|
+
|
29
|
+
doc = Doc.create
|
30
|
+
emdoc = Emdoc.new
|
31
|
+
doc.emdocs << emdoc
|
32
|
+
doc.save
|
33
|
+
|
34
|
+
old_updated_at = emdoc.updated_at
|
35
|
+
document_old_updated_at = doc.updated_at
|
36
|
+
|
37
|
+
Timecop.freeze(Time.now + 1.day) do
|
38
|
+
emdoc.touch
|
39
|
+
end
|
40
|
+
|
41
|
+
doc.reload
|
42
|
+
emdoc.updated_at.should_not == old_updated_at
|
43
|
+
doc.updated_at.should_not == document_old_updated_at
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "association" do
|
48
|
+
setup do
|
49
|
+
@post_class = Doc("Post") do
|
50
|
+
key :touched_at, DateTime
|
51
|
+
timestamps!
|
52
|
+
end
|
53
|
+
@comment_class = Doc("Comment") do
|
54
|
+
key :post_id, ObjectId
|
55
|
+
key :text, String
|
56
|
+
timestamps!
|
57
|
+
end
|
58
|
+
|
59
|
+
@post_class.many :comments, :class => @comment_class
|
60
|
+
end
|
61
|
+
|
62
|
+
should 'not be true by default' do
|
63
|
+
@comment_class.belongs_to :post, :class => @post_class
|
64
|
+
@comment_class.associations[:post].touch?.should_not be_true
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'touch the parent when true' do
|
68
|
+
setup do
|
69
|
+
@comment_class.belongs_to :post, :class => @post_class, :touch => true
|
70
|
+
@post = @post_class.create(:title => 'Hello, world!')
|
71
|
+
@comment = @post.comments.build
|
72
|
+
end
|
73
|
+
|
74
|
+
should "when the child is created" do
|
75
|
+
orig_updated_at = @post.updated_at
|
76
|
+
Timecop.freeze(Time.now + 1.day) do
|
77
|
+
@comment.save
|
78
|
+
end
|
79
|
+
|
80
|
+
@post.reload.updated_at.should_not == orig_updated_at
|
81
|
+
end
|
82
|
+
|
83
|
+
should "when the child is updated" do
|
84
|
+
@comment.save
|
85
|
+
old_updated_at = @post.updated_at
|
86
|
+
Timecop.freeze(Time.now + 2.day) do
|
87
|
+
@comment.update_attributes(:text => "Something")
|
88
|
+
end
|
89
|
+
@post.reload.updated_at.should_not == old_updated_at
|
90
|
+
end
|
91
|
+
|
92
|
+
should "when the child is touched" do
|
93
|
+
@comment.save
|
94
|
+
old_updated_at = @post.updated_at
|
95
|
+
Timecop.freeze(Time.now + 3.day) do
|
96
|
+
@comment.touch
|
97
|
+
end
|
98
|
+
@post.reload.updated_at.should_not == old_updated_at
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context "when set to a symbol that is a key of parent" do
|
103
|
+
should "set that key on touch events" do
|
104
|
+
@comment_class.belongs_to :post, :class => @post_class, :touch => :touched_at
|
105
|
+
post = @post_class.create(:title => 'Hello, world!')
|
106
|
+
post.touched_at.should be_nil
|
107
|
+
|
108
|
+
comment = post.comments.build
|
109
|
+
comment.save
|
110
|
+
post.reload.touched_at.should_not be_nil
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
should 'not touch the parent when false' do
|
115
|
+
post = @post_class.create(:title => 'Hello, world!')
|
116
|
+
comment = post.comments.build
|
117
|
+
Timecop.freeze(Time.now + 1.day) do
|
118
|
+
comment.save
|
119
|
+
end
|
120
|
+
|
121
|
+
post.reload.updated_at.should == post.created_at
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class UserstampsTest < Test::Unit::TestCase
|
4
|
+
class AltUser
|
5
|
+
include MongoMapper::Document
|
6
|
+
end
|
7
|
+
|
8
|
+
context "userstamping" do
|
9
|
+
setup do
|
10
|
+
@document = Doc do
|
11
|
+
userstamps!
|
12
|
+
end
|
13
|
+
@document_alt_user = Doc do
|
14
|
+
userstamps! :class_name => 'AltUser'
|
15
|
+
end
|
16
|
+
@document_alt_user_class = Doc do
|
17
|
+
userstamps! :class => AltUser
|
18
|
+
end
|
19
|
+
@docs = [@document, @document_alt_user, @document_alt_user_class]
|
20
|
+
end
|
21
|
+
|
22
|
+
should "add creator_id key" do
|
23
|
+
@docs.each{ |d| d.keys.should include('creator_id') }
|
24
|
+
end
|
25
|
+
|
26
|
+
should "add updater_id key" do
|
27
|
+
@docs.each{ |d| d.keys.should include('updater_id') }
|
28
|
+
end
|
29
|
+
|
30
|
+
should "add belongs_to creator" do
|
31
|
+
@docs.each{ |d| d.associations.keys.should include(:creator) }
|
32
|
+
end
|
33
|
+
|
34
|
+
should "add belongs_to updater" do
|
35
|
+
@docs.each{ |d| d.associations.keys.should include(:updater) }
|
36
|
+
end
|
37
|
+
|
38
|
+
should "properly set class names" do
|
39
|
+
@document.associations[:creator].class_name.should == 'User'
|
40
|
+
@document_alt_user.associations[:creator].class_name.should == 'AltUser'
|
41
|
+
@document_alt_user_class.associations[:creator].class_name.should == 'UserstampsTest::AltUser'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,405 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationsTest < Test::Unit::TestCase
|
4
|
+
context "Saving a new document that is invalid" do
|
5
|
+
setup do
|
6
|
+
@document = Doc do
|
7
|
+
key :name, String, :required => true
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
should "not insert document" do
|
12
|
+
doc = @document.new
|
13
|
+
doc.save
|
14
|
+
@document.count.should == 0
|
15
|
+
end
|
16
|
+
|
17
|
+
should "populate document's errors" do
|
18
|
+
doc = @document.new
|
19
|
+
doc.errors.size.should == 0
|
20
|
+
doc.save
|
21
|
+
doc.errors.full_messages.should == ["Name can't be blank"]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "Saving a document that is invalid (destructive)" do
|
26
|
+
setup do
|
27
|
+
@document = Doc do
|
28
|
+
key :name, String, :required => true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
should "raise error" do
|
33
|
+
doc = @document.new
|
34
|
+
lambda { doc.save! }.should raise_error(MongoMapper::DocumentNotValid)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "Creating a document that is invalid (destructive)" do
|
39
|
+
setup do
|
40
|
+
@document = Doc do
|
41
|
+
key :name, String, :required => true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
should "raise error" do
|
46
|
+
lambda { @document.create! }.should raise_error(MongoMapper::DocumentNotValid)
|
47
|
+
end
|
48
|
+
|
49
|
+
should "create a new document" do
|
50
|
+
instance = @document.create!(:name => "James")
|
51
|
+
instance.new_record?.should be_false
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "Saving an existing document that is invalid" do
|
56
|
+
setup do
|
57
|
+
@document = Doc do
|
58
|
+
key :name, String, :required => true
|
59
|
+
end
|
60
|
+
|
61
|
+
@doc = @document.create(:name => 'John Nunemaker')
|
62
|
+
end
|
63
|
+
|
64
|
+
should "not update document" do
|
65
|
+
@doc.name = nil
|
66
|
+
@doc.save
|
67
|
+
@doc.reload.name.should == 'John Nunemaker'
|
68
|
+
end
|
69
|
+
|
70
|
+
should "populate document's errors" do
|
71
|
+
@doc.name = nil
|
72
|
+
@doc.save
|
73
|
+
@doc.errors.full_messages.should == ["Name can't be blank"]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "Adding validation errors" do
|
78
|
+
setup do
|
79
|
+
@document = Doc do
|
80
|
+
key :action, String
|
81
|
+
def action_present
|
82
|
+
errors.add(:action, 'is invalid') if action.blank?
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
should "work with validate :on => :create callback" do
|
88
|
+
@document.validate :action_present, :on => :create
|
89
|
+
|
90
|
+
doc = @document.create(:action => nil)
|
91
|
+
doc.should have_error_on(:action)
|
92
|
+
|
93
|
+
doc.action = 'kick'
|
94
|
+
doc.save
|
95
|
+
doc.should_not have_error_on(:action)
|
96
|
+
end
|
97
|
+
|
98
|
+
should "work with validate :on => :update callback" do
|
99
|
+
@document.validate :action_present, :on => :update
|
100
|
+
|
101
|
+
doc = @document.new
|
102
|
+
doc.action = nil
|
103
|
+
doc.should_not have_error_on(:action)
|
104
|
+
doc.save
|
105
|
+
|
106
|
+
doc.action = nil
|
107
|
+
doc.should have_error_on(:action)
|
108
|
+
|
109
|
+
doc.action = 'kick'
|
110
|
+
doc.should_not have_error_on(:action)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
context "validating uniqueness of" do
|
115
|
+
setup do
|
116
|
+
@document = Doc do
|
117
|
+
key :name, String
|
118
|
+
validates_uniqueness_of :name
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
should "not fail if object is new" do
|
123
|
+
doc = @document.new
|
124
|
+
doc.should_not have_error_on(:name)
|
125
|
+
end
|
126
|
+
|
127
|
+
should "not fail when new object is out of scope" do
|
128
|
+
document = Doc do
|
129
|
+
key :name
|
130
|
+
key :adult
|
131
|
+
validates_uniqueness_of :name, :scope => :adult
|
132
|
+
end
|
133
|
+
doc = document.new("name" => "joe", :adult => true)
|
134
|
+
doc.save.should be_true
|
135
|
+
|
136
|
+
doc2 = document.new("name" => "joe", :adult => false)
|
137
|
+
doc2.should be_valid
|
138
|
+
end
|
139
|
+
|
140
|
+
should "work with i18n taken message" do
|
141
|
+
@document.create(:name => 'joe')
|
142
|
+
doc = @document.create(:name => 'joe')
|
143
|
+
doc.should have_error_on(:name, 'has already been taken')
|
144
|
+
end
|
145
|
+
|
146
|
+
should "allow to update an object" do
|
147
|
+
doc = @document.new("name" => "joe")
|
148
|
+
doc.save.should be_true
|
149
|
+
|
150
|
+
@document \
|
151
|
+
.stubs(:first) \
|
152
|
+
.with(:name => 'joe') \
|
153
|
+
.returns(doc)
|
154
|
+
|
155
|
+
doc.name = "joe"
|
156
|
+
doc.valid?.should be_true
|
157
|
+
doc.should_not have_error_on(:name)
|
158
|
+
end
|
159
|
+
|
160
|
+
should "fail if object name is not unique" do
|
161
|
+
doc = @document.new("name" => "joe")
|
162
|
+
doc.save.should be_true
|
163
|
+
|
164
|
+
@document \
|
165
|
+
.stubs(:first) \
|
166
|
+
.with(:name => 'joe') \
|
167
|
+
.returns(doc)
|
168
|
+
|
169
|
+
doc2 = @document.new("name" => "joe")
|
170
|
+
doc2.should have_error_on(:name)
|
171
|
+
end
|
172
|
+
|
173
|
+
should "allow multiple blank entries if :allow_blank => true" do
|
174
|
+
document = Doc do
|
175
|
+
key :name
|
176
|
+
validates_uniqueness_of :name, :allow_blank => :true
|
177
|
+
end
|
178
|
+
|
179
|
+
doc = document.new("name" => "")
|
180
|
+
doc.save.should be_true
|
181
|
+
|
182
|
+
document \
|
183
|
+
.stubs(:first) \
|
184
|
+
.with(:name => '') \
|
185
|
+
.returns(doc)
|
186
|
+
|
187
|
+
doc2 = document.new("name" => "")
|
188
|
+
doc2.should_not have_error_on(:name)
|
189
|
+
end
|
190
|
+
|
191
|
+
should "allow multiple nil entries if :allow_nil => true" do
|
192
|
+
document = Doc do
|
193
|
+
key :name
|
194
|
+
validates_uniqueness_of :name, :allow_nil => :true
|
195
|
+
end
|
196
|
+
|
197
|
+
doc = document.new('name' => nil)
|
198
|
+
doc.save.should be_true
|
199
|
+
|
200
|
+
doc2 = document.new('name' => nil)
|
201
|
+
doc2.should_not have_error_on(:name)
|
202
|
+
end
|
203
|
+
|
204
|
+
should "allow entries that differ only in case by default" do
|
205
|
+
document = Doc do
|
206
|
+
key :name
|
207
|
+
validates_uniqueness_of :name
|
208
|
+
end
|
209
|
+
|
210
|
+
doc = document.new("name" => "BLAMMO")
|
211
|
+
doc.save.should be_true
|
212
|
+
|
213
|
+
doc2 = document.new("name" => "blammo")
|
214
|
+
doc2.should_not have_error_on(:name)
|
215
|
+
end
|
216
|
+
|
217
|
+
context "with :case_sensitive => false" do
|
218
|
+
setup do
|
219
|
+
@document = Doc do
|
220
|
+
key :name
|
221
|
+
validates_uniqueness_of :name, :case_sensitive => false
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
should "fail on entries that differ only in case" do
|
226
|
+
doc = @document.new("name" => "BLAMMO")
|
227
|
+
doc.save.should be_true
|
228
|
+
|
229
|
+
doc2 = @document.new("name" => "blammo")
|
230
|
+
doc2.should have_error_on(:name)
|
231
|
+
end
|
232
|
+
|
233
|
+
should "not raise an error if value is nil" do
|
234
|
+
doc = @document.new("name" => nil)
|
235
|
+
lambda { doc.valid? }.should_not raise_error
|
236
|
+
end
|
237
|
+
|
238
|
+
should "not raise an error if special Regexp characters used" do
|
239
|
+
doc = @document.new("name" => '?')
|
240
|
+
lambda { doc.valid? }.should_not raise_error
|
241
|
+
end
|
242
|
+
|
243
|
+
should "check for uniqueness using entire string" do
|
244
|
+
doc = @document.new("name" => "John Doe")
|
245
|
+
doc.save.should be_true
|
246
|
+
|
247
|
+
doc2 = @document.new("name" => "John")
|
248
|
+
doc2.valid?.should be_true
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
context "scoped by a single attribute" do
|
253
|
+
setup do
|
254
|
+
@document = Doc do
|
255
|
+
key :name, String
|
256
|
+
key :scope, String
|
257
|
+
validates_uniqueness_of :name, :scope => :scope
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
should "fail if the same name exists in the scope" do
|
262
|
+
doc = @document.new("name" => "joe", "scope" => "one")
|
263
|
+
doc.save.should be_true
|
264
|
+
|
265
|
+
@document \
|
266
|
+
.stubs(:first) \
|
267
|
+
.with(:name => 'joe', :scope => "one") \
|
268
|
+
.returns(doc)
|
269
|
+
|
270
|
+
doc2 = @document.new("name" => "joe", "scope" => "one")
|
271
|
+
doc2.should have_error_on(:name)
|
272
|
+
end
|
273
|
+
|
274
|
+
should "pass if the same name exists in a different scope" do
|
275
|
+
doc = @document.new("name" => "joe", "scope" => "one")
|
276
|
+
doc.save.should be_true
|
277
|
+
|
278
|
+
@document \
|
279
|
+
.stubs(:first) \
|
280
|
+
.with(:name => 'joe', :scope => 'two') \
|
281
|
+
.returns(nil)
|
282
|
+
|
283
|
+
doc2 = @document.new("name" => "joe", "scope" => "two")
|
284
|
+
doc2.should_not have_error_on(:name)
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
context "scoped by a multiple attributes" do
|
289
|
+
setup do
|
290
|
+
@document = Doc do
|
291
|
+
key :name, String
|
292
|
+
key :first_scope, String
|
293
|
+
key :second_scope, String
|
294
|
+
validates_uniqueness_of :name, :scope => [:first_scope, :second_scope]
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
should "fail if the same name exists in the scope" do
|
299
|
+
doc = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "two")
|
300
|
+
doc.save.should be_true
|
301
|
+
|
302
|
+
@document \
|
303
|
+
.stubs(:first) \
|
304
|
+
.with(:name => 'joe', :first_scope => 'one', :second_scope => 'two') \
|
305
|
+
.returns(doc)
|
306
|
+
|
307
|
+
doc2 = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "two")
|
308
|
+
doc2.should have_error_on(:name)
|
309
|
+
end
|
310
|
+
|
311
|
+
should "pass if the same name exists in a different scope" do
|
312
|
+
doc = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "two")
|
313
|
+
doc.save.should be_true
|
314
|
+
|
315
|
+
@document \
|
316
|
+
.stubs(:first) \
|
317
|
+
.with(:name => 'joe', :first_scope => 'one', :second_scope => 'one') \
|
318
|
+
.returns(nil)
|
319
|
+
|
320
|
+
doc2 = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "one")
|
321
|
+
doc2.should_not have_error_on(:name)
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
context "validating associated docs" do
|
327
|
+
setup do
|
328
|
+
@child_class = EDoc do
|
329
|
+
key :name, :required => true
|
330
|
+
end
|
331
|
+
|
332
|
+
@root_class = Doc { }
|
333
|
+
@root_class.many :children, :class => @child_class
|
334
|
+
@root_class.validates_associated :children, :message => 'are invalid'
|
335
|
+
end
|
336
|
+
|
337
|
+
should "pass if there are no associated docs" do
|
338
|
+
doc = @root_class.new
|
339
|
+
doc.save.should be_true
|
340
|
+
end
|
341
|
+
|
342
|
+
should "pass if the associated doc is valid" do
|
343
|
+
doc = @root_class.new
|
344
|
+
doc.children.build(:name => 'Joe')
|
345
|
+
doc.save.should be_true
|
346
|
+
end
|
347
|
+
|
348
|
+
should "fail if the associated doc is invalid" do
|
349
|
+
doc = @root_class.new
|
350
|
+
doc.children.build
|
351
|
+
doc.should have_error_on(:children, 'are invalid')
|
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
|
+
|
386
|
+
end
|
387
|
+
# context "validates uniqueness of with :unique shortcut" do
|
388
|
+
# should "work" do
|
389
|
+
# @document = Doc do
|
390
|
+
# key :name, String, :unique => true
|
391
|
+
# end
|
392
|
+
#
|
393
|
+
# doc = @document.create(:name => 'John')
|
394
|
+
# doc.should_not have_error_on(:name)
|
395
|
+
#
|
396
|
+
# @document \
|
397
|
+
# .stubs(:first) \
|
398
|
+
# .with(:name => 'John') \
|
399
|
+
# .returns(doc)
|
400
|
+
#
|
401
|
+
# second_john = @document.create(:name => 'John')
|
402
|
+
# second_john.should have_error_on(:name, 'has already been taken')
|
403
|
+
# end
|
404
|
+
# end
|
405
|
+
end
|