mongo_mapper 0.14.0 → 0.15.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/README.md +72 -0
- data/examples/keys.rb +1 -1
- data/examples/modifiers/set.rb +1 -1
- data/examples/querying.rb +1 -1
- data/examples/safe.rb +2 -2
- data/examples/scopes.rb +1 -1
- data/lib/mongo_mapper.rb +19 -17
- data/lib/mongo_mapper/connection.rb +16 -38
- data/lib/mongo_mapper/extensions/array.rb +1 -1
- data/lib/mongo_mapper/extensions/binary.rb +1 -1
- data/lib/mongo_mapper/extensions/date.rb +1 -1
- data/lib/mongo_mapper/extensions/float.rb +1 -1
- data/lib/mongo_mapper/extensions/hash.rb +1 -1
- data/lib/mongo_mapper/extensions/nil_class.rb +2 -2
- data/lib/mongo_mapper/extensions/object.rb +1 -1
- data/lib/mongo_mapper/extensions/object_id.rb +6 -2
- data/lib/mongo_mapper/extensions/set.rb +1 -1
- data/lib/mongo_mapper/extensions/string.rb +1 -1
- data/lib/mongo_mapper/plugins/accessible.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/base.rb +10 -2
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/many_association.rb +6 -5
- data/lib/mongo_mapper/plugins/associations/{belongs_to_polymorphic_proxy.rb → proxy/belongs_to_polymorphic_proxy.rb} +0 -0
- data/lib/mongo_mapper/plugins/associations/{belongs_to_proxy.rb → proxy/belongs_to_proxy.rb} +6 -0
- data/lib/mongo_mapper/plugins/associations/proxy/collection.rb +55 -0
- data/lib/mongo_mapper/plugins/associations/{embedded_collection.rb → proxy/embedded_collection.rb} +0 -0
- data/lib/mongo_mapper/plugins/associations/{in_array_proxy.rb → proxy/in_array_proxy.rb} +36 -6
- data/lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb +136 -0
- data/lib/mongo_mapper/plugins/associations/{many_documents_as_proxy.rb → proxy/many_documents_as_proxy.rb} +0 -0
- data/lib/mongo_mapper/plugins/associations/{many_documents_proxy.rb → proxy/many_documents_proxy.rb} +0 -4
- data/lib/mongo_mapper/plugins/associations/{many_embedded_polymorphic_proxy.rb → proxy/many_embedded_polymorphic_proxy.rb} +0 -0
- data/lib/mongo_mapper/plugins/associations/{many_embedded_proxy.rb → proxy/many_embedded_proxy.rb} +0 -0
- data/lib/mongo_mapper/plugins/associations/{many_polymorphic_proxy.rb → proxy/many_polymorphic_proxy.rb} +0 -0
- data/lib/mongo_mapper/plugins/associations/{one_as_proxy.rb → proxy/one_as_proxy.rb} +0 -0
- data/lib/mongo_mapper/plugins/associations/{one_embedded_polymorphic_proxy.rb → proxy/one_embedded_polymorphic_proxy.rb} +0 -0
- data/lib/mongo_mapper/plugins/associations/{one_embedded_proxy.rb → proxy/one_embedded_proxy.rb} +3 -1
- data/lib/mongo_mapper/plugins/associations/{one_proxy.rb → proxy/one_proxy.rb} +0 -0
- data/lib/mongo_mapper/plugins/associations/proxy/proxy.rb +164 -0
- data/lib/mongo_mapper/plugins/associations/single_association.rb +5 -13
- data/lib/mongo_mapper/plugins/dirty.rb +29 -37
- data/lib/mongo_mapper/plugins/document.rb +1 -1
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +1 -1
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +1 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +2 -2
- data/lib/mongo_mapper/plugins/identity_map.rb +3 -1
- data/lib/mongo_mapper/plugins/indexes.rb +13 -6
- data/lib/mongo_mapper/plugins/keys.rb +12 -7
- data/lib/mongo_mapper/plugins/keys/key.rb +21 -13
- data/lib/mongo_mapper/plugins/modifiers.rb +39 -14
- data/lib/mongo_mapper/plugins/persistence.rb +6 -2
- data/lib/mongo_mapper/plugins/querying.rb +9 -3
- data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +6 -6
- data/lib/mongo_mapper/plugins/safe.rb +10 -4
- data/lib/mongo_mapper/plugins/scopes.rb +19 -3
- data/lib/mongo_mapper/plugins/stats.rb +1 -3
- data/lib/mongo_mapper/plugins/strong_parameters.rb +26 -0
- data/lib/mongo_mapper/plugins/validations.rb +1 -1
- data/lib/mongo_mapper/railtie.rb +1 -0
- data/lib/mongo_mapper/utils.rb +2 -2
- data/lib/mongo_mapper/version.rb +1 -1
- data/spec/examples.txt +1731 -0
- data/spec/functional/accessible_spec.rb +7 -1
- data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +2 -2
- data/spec/functional/associations/belongs_to_proxy_spec.rb +55 -5
- data/spec/functional/associations/in_array_proxy_spec.rb +149 -14
- data/spec/functional/associations/in_foreign_array_proxy_spec.rb +321 -0
- data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
- data/spec/functional/associations/many_documents_proxy_spec.rb +22 -22
- data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +2 -2
- data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
- data/spec/functional/associations/one_as_proxy_spec.rb +8 -8
- data/spec/functional/associations/one_embedded_proxy_spec.rb +28 -0
- data/spec/functional/associations/one_proxy_spec.rb +19 -9
- data/spec/functional/associations_spec.rb +3 -3
- data/spec/functional/binary_spec.rb +2 -2
- data/spec/functional/caching_spec.rb +15 -22
- data/spec/functional/callbacks_spec.rb +2 -2
- data/spec/functional/counter_cache_spec.rb +10 -10
- data/spec/functional/dirty_spec.rb +48 -10
- data/spec/functional/dirty_with_callbacks_spec.rb +59 -0
- data/spec/functional/document_spec.rb +5 -8
- data/spec/functional/dumpable_spec.rb +1 -1
- data/spec/functional/embedded_document_spec.rb +5 -5
- data/spec/functional/identity_map_spec.rb +8 -8
- data/spec/functional/indexes_spec.rb +19 -18
- data/spec/functional/keys_spec.rb +64 -33
- data/spec/functional/logger_spec.rb +2 -2
- data/spec/functional/modifiers_spec.rb +81 -19
- data/spec/functional/partial_updates_spec.rb +8 -8
- data/spec/functional/protected_spec.rb +1 -1
- data/spec/functional/querying_spec.rb +70 -22
- data/spec/functional/safe_spec.rb +23 -27
- data/spec/functional/sci_spec.rb +7 -7
- data/spec/functional/scopes_spec.rb +89 -1
- data/spec/functional/static_keys_spec.rb +2 -2
- data/spec/functional/stats_spec.rb +28 -12
- data/spec/functional/strong_parameters_spec.rb +49 -0
- data/spec/functional/validations_spec.rb +8 -16
- data/spec/quality_spec.rb +1 -1
- data/spec/spec_helper.rb +39 -8
- data/spec/support/matchers.rb +1 -1
- data/spec/unit/associations/proxy_spec.rb +18 -10
- data/spec/unit/clone_spec.rb +1 -1
- data/spec/unit/document_spec.rb +3 -3
- data/spec/unit/embedded_document_spec.rb +4 -5
- data/spec/unit/extensions_spec.rb +12 -7
- data/spec/unit/identity_map_middleware_spec.rb +65 -96
- data/spec/unit/inspect_spec.rb +1 -1
- data/spec/unit/key_spec.rb +23 -18
- data/spec/unit/keys_spec.rb +17 -8
- data/spec/unit/mongo_mapper_spec.rb +41 -88
- data/spec/unit/rails_spec.rb +2 -2
- data/spec/unit/validations_spec.rb +18 -18
- metadata +70 -38
- data/README.rdoc +0 -56
- data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
- data/lib/mongo_mapper/plugins/associations/collection.rb +0 -29
- data/lib/mongo_mapper/plugins/associations/proxy.rb +0 -141
@@ -42,7 +42,7 @@ describe "Safe" do
|
|
42
42
|
|
43
43
|
it "should not raise an error on duplicate IDs" do
|
44
44
|
k = @klass.create
|
45
|
-
|
45
|
+
lambda { j = @klass.create(:_id => k.id) }.should_not raise_error
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
@@ -61,46 +61,44 @@ describe "Safe" do
|
|
61
61
|
|
62
62
|
it "should raise an error on duplicate IDs" do
|
63
63
|
k = @klass.create
|
64
|
-
|
64
|
+
lambda { j = @klass.create(:_id => k.id) }.should raise_error(Mongo::Error::OperationFailure)
|
65
65
|
end
|
66
66
|
|
67
67
|
context "using safe setting from class" do
|
68
|
-
it "should pass :w => 1 option to
|
69
|
-
|
70
|
-
expect_any_instance_of(Mongo::Collection).to receive(:insert).once.with({'_id' => instance.id, 'email' => 'john@doe.com'}, {:w => 1})
|
71
|
-
instance.save!
|
68
|
+
it "should pass :w => 1 option to the collection" do
|
69
|
+
@klass.collection.write_concern.options.should == { w: 1 }
|
72
70
|
end
|
73
71
|
|
74
72
|
it "should work fine when all is well" do
|
75
|
-
|
73
|
+
lambda {
|
76
74
|
@klass.new(:email => 'john@doe.com').save
|
77
|
-
}.
|
75
|
+
}.should_not raise_error
|
78
76
|
end
|
79
77
|
|
80
78
|
it "should raise error when operation fails" do
|
81
|
-
|
79
|
+
lambda {
|
82
80
|
2.times do
|
83
81
|
@klass.new(:email => 'john@doe.com').save
|
84
82
|
end
|
85
|
-
}.
|
83
|
+
}.should raise_error(Mongo::Error::OperationFailure)
|
86
84
|
end
|
87
85
|
end
|
88
86
|
|
89
87
|
context "overriding safe setting" do
|
90
88
|
it "should raise error if safe is true" do
|
91
|
-
|
89
|
+
lambda {
|
92
90
|
2.times do
|
93
91
|
@klass.new(:email => 'john@doe.com').save(:safe => true)
|
94
92
|
end
|
95
|
-
}.
|
93
|
+
}.should raise_error(Mongo::Error::OperationFailure)
|
96
94
|
end
|
97
95
|
|
98
96
|
it "should not raise error if safe is false" do
|
99
|
-
|
97
|
+
lambda {
|
100
98
|
2.times do
|
101
99
|
@klass.new(:email => 'john@doe.com').save(:safe => false)
|
102
100
|
end
|
103
|
-
}.
|
101
|
+
}.should_not raise_error
|
104
102
|
end
|
105
103
|
end
|
106
104
|
end
|
@@ -120,44 +118,42 @@ describe "Safe" do
|
|
120
118
|
end
|
121
119
|
|
122
120
|
context "using safe setting from class" do
|
123
|
-
it "should pass :safe => options_hash to
|
124
|
-
|
125
|
-
expect_any_instance_of(Mongo::Collection).to receive(:insert).once.with({'_id' => instance.id, 'email' => 'john@doe.com'}, {:j => true})
|
126
|
-
instance.save!
|
121
|
+
it "should pass :safe => options_hash to the collection" do
|
122
|
+
@klass.collection.write_concern.options.should == { j: true }
|
127
123
|
end
|
128
124
|
|
129
125
|
it "should work fine when all is well" do
|
130
|
-
|
126
|
+
lambda {
|
131
127
|
@klass.new(:email => 'john@doe.com').save
|
132
|
-
}.
|
128
|
+
}.should_not raise_error
|
133
129
|
end
|
134
130
|
|
135
131
|
it "should raise error when operation fails" do
|
136
|
-
|
132
|
+
lambda {
|
137
133
|
2.times do
|
138
134
|
@klass.new(:email => 'john@doe.com').save
|
139
135
|
end
|
140
|
-
}.
|
136
|
+
}.should raise_error(Mongo::Error::OperationFailure)
|
141
137
|
end
|
142
138
|
end
|
143
139
|
|
144
140
|
context "overriding safe setting" do
|
145
141
|
it "should raise error if safe is true" do
|
146
|
-
|
142
|
+
lambda {
|
147
143
|
2.times do
|
148
144
|
@klass.new(:email => 'john@doe.com').save(:safe => true)
|
149
145
|
end
|
150
|
-
}.
|
146
|
+
}.should raise_error(Mongo::Error::OperationFailure)
|
151
147
|
end
|
152
148
|
|
153
149
|
it "should not raise error if safe is false" do
|
154
|
-
|
150
|
+
lambda {
|
155
151
|
2.times do
|
156
152
|
@klass.new(:email => 'john@doe.com').save(:safe => false)
|
157
153
|
end
|
158
|
-
}.
|
154
|
+
}.should_not raise_error
|
159
155
|
end
|
160
156
|
end
|
161
157
|
end
|
162
158
|
end
|
163
|
-
end
|
159
|
+
end
|
data/spec/functional/sci_spec.rb
CHANGED
@@ -3,12 +3,12 @@ require 'spec_helper'
|
|
3
3
|
describe "Single collection inheritance (document)" do
|
4
4
|
context "without a connection", :without_connection => true do
|
5
5
|
it "should attempt to create a connection during inheritance" do
|
6
|
-
|
6
|
+
Mongo::Client.should_not_receive(:new)
|
7
7
|
doc = Class.new
|
8
8
|
doc.send(:include, MongoMapper::Document)
|
9
|
-
|
9
|
+
lambda {
|
10
10
|
Class.new(doc)
|
11
|
-
}.
|
11
|
+
}.should_not raise_error
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should pick up a connection if one wasn't set" do
|
@@ -17,7 +17,7 @@ describe "Single collection inheritance (document)" do
|
|
17
17
|
klass = Class.new(doc)
|
18
18
|
klass.connection.should be_nil
|
19
19
|
MongoMapper.connection
|
20
|
-
klass.connection.should be_a Mongo::
|
20
|
+
klass.connection.should be_a Mongo::Client
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -27,7 +27,7 @@ describe "Single collection inheritance (document)" do
|
|
27
27
|
include MongoMapper::Document
|
28
28
|
key :name, String
|
29
29
|
end
|
30
|
-
DocParent.collection.
|
30
|
+
DocParent.collection.drop
|
31
31
|
|
32
32
|
class ::DocDaughter < ::DocParent; end
|
33
33
|
class ::DocSon < ::DocParent; end
|
@@ -61,7 +61,7 @@ describe "Single collection inheritance (document)" do
|
|
61
61
|
it "should use the same connection in the subclass" do
|
62
62
|
parent_class = Class.new do
|
63
63
|
include MongoMapper::Document
|
64
|
-
connection Mongo::
|
64
|
+
connection Mongo::Client.new(['127.0.0.1:27017'])
|
65
65
|
end
|
66
66
|
|
67
67
|
child_class = Class.new(parent_class) do
|
@@ -377,4 +377,4 @@ describe "Single collection inheritance (document)" do
|
|
377
377
|
p.reload.article_parent.sci_polymorphic_posts.all.should include(p)
|
378
378
|
end
|
379
379
|
end
|
380
|
-
end
|
380
|
+
end
|
@@ -170,7 +170,7 @@ describe "Scopes" do
|
|
170
170
|
key :title, String
|
171
171
|
key :published_at, Time
|
172
172
|
end
|
173
|
-
Item.collection.
|
173
|
+
Item.collection.drop
|
174
174
|
|
175
175
|
class ::Page < ::Item; end
|
176
176
|
class ::Blog < ::Item
|
@@ -200,6 +200,19 @@ describe "Scopes" do
|
|
200
200
|
Blog.scopes.keys.map(&:to_s).should =~ %w(by_slug by_title published)
|
201
201
|
end
|
202
202
|
end
|
203
|
+
|
204
|
+
context 'with predefined class method name' do
|
205
|
+
it 'should raise ArgumentError' do
|
206
|
+
bad_names = %i(private public protected allocate new name parent superclass reload)
|
207
|
+
bad_names.each do |bad_name|
|
208
|
+
-> {
|
209
|
+
Doc() do
|
210
|
+
scope bad_name, -> {}
|
211
|
+
end
|
212
|
+
}.should raise_error(ArgumentError, /You tried to define a scope named "#{bad_name}"/)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
203
216
|
end
|
204
217
|
|
205
218
|
describe "with_scope" do
|
@@ -434,4 +447,79 @@ describe "Scopes" do
|
|
434
447
|
@klass.unsent.sorted.all.should == [two_days_ago, one_day_ago]
|
435
448
|
end
|
436
449
|
end
|
450
|
+
|
451
|
+
describe "thread safety" do
|
452
|
+
before do
|
453
|
+
@klass = Doc do
|
454
|
+
key :name, String
|
455
|
+
end
|
456
|
+
|
457
|
+
@threads = []
|
458
|
+
end
|
459
|
+
|
460
|
+
after do
|
461
|
+
@threads.each do |thread|
|
462
|
+
thread.kill
|
463
|
+
end
|
464
|
+
end
|
465
|
+
|
466
|
+
def make_thread(&block)
|
467
|
+
Thread.new(&block).tap do |thread|
|
468
|
+
@threads << thread
|
469
|
+
end
|
470
|
+
end
|
471
|
+
|
472
|
+
def wait_for_threads!
|
473
|
+
@threads.each { |t| t.join }
|
474
|
+
end
|
475
|
+
|
476
|
+
it "should not taint another thread's active scopes" do
|
477
|
+
count_active_scopes = nil
|
478
|
+
second_thread_run = false
|
479
|
+
|
480
|
+
make_thread do
|
481
|
+
@klass.with_scope(name: 'foo') do
|
482
|
+
loop do
|
483
|
+
sleep Float::EPSILON
|
484
|
+
break if second_thread_run
|
485
|
+
end
|
486
|
+
end
|
487
|
+
end
|
488
|
+
|
489
|
+
make_thread do
|
490
|
+
count_active_scopes = @klass.active_scopes.length
|
491
|
+
second_thread_run = true
|
492
|
+
end
|
493
|
+
|
494
|
+
wait_for_threads!
|
495
|
+
|
496
|
+
count_active_scopes.should == 0
|
497
|
+
end
|
498
|
+
|
499
|
+
it "should not taint another thread's count method" do
|
500
|
+
@klass.create!(name: 'foo')
|
501
|
+
@klass.create!(name: 'bar')
|
502
|
+
|
503
|
+
klass_count = nil
|
504
|
+
second_thread_run = false
|
505
|
+
|
506
|
+
make_thread do
|
507
|
+
@klass.with_scope(name: 'foo') do
|
508
|
+
loop do
|
509
|
+
sleep Float::EPSILON
|
510
|
+
break if second_thread_run
|
511
|
+
end
|
512
|
+
end
|
513
|
+
end
|
514
|
+
|
515
|
+
make_thread do
|
516
|
+
klass_count = @klass.count
|
517
|
+
second_thread_run = true
|
518
|
+
end
|
519
|
+
|
520
|
+
wait_for_threads!
|
521
|
+
|
522
|
+
klass_count.should == 2
|
523
|
+
end
|
524
|
+
end
|
437
525
|
end
|
@@ -92,7 +92,7 @@ describe MongoMapper::Plugins::Keys::Static do
|
|
92
92
|
end
|
93
93
|
|
94
94
|
it "should not blow up when loading if there is a key defined in the db that has not been defined (but it should not load it)" do
|
95
|
-
@klass.collection.
|
95
|
+
@klass.collection.insert_one({ :foo => "bar", :valid_key => "something" })
|
96
96
|
@obj = @klass.first
|
97
97
|
@obj.valid_key.should == "something"
|
98
98
|
|
@@ -147,7 +147,7 @@ describe MongoMapper::Plugins::Keys::Static do
|
|
147
147
|
|
148
148
|
lambda {
|
149
149
|
@static_key_object['foo'] = 'bar'
|
150
|
-
}.should raise_error
|
150
|
+
}.should raise_error(MongoMapper::Plugins::Keys::Static::MissingKeyError)
|
151
151
|
end
|
152
152
|
end
|
153
153
|
end
|
@@ -13,7 +13,11 @@ describe "Stats" do
|
|
13
13
|
|
14
14
|
context "with no documents present" do
|
15
15
|
it "should return nil" do
|
16
|
-
|
16
|
+
if Docs.stats == nil
|
17
|
+
Docs.stats.should == nil
|
18
|
+
else
|
19
|
+
Docs.stats['count'].should == 0
|
20
|
+
end
|
17
21
|
end
|
18
22
|
end
|
19
23
|
|
@@ -23,48 +27,60 @@ describe "Stats" do
|
|
23
27
|
Docs.create!
|
24
28
|
end
|
25
29
|
|
30
|
+
def get_stats
|
31
|
+
MongoMapper.database.command(:collstats => 'docs').documents[0]
|
32
|
+
end
|
33
|
+
|
26
34
|
it "should have the correct count" do
|
27
|
-
|
35
|
+
Docs.stats.count.should == get_stats['count']
|
28
36
|
end
|
29
37
|
|
30
38
|
it "should have the correct namespace" do
|
31
|
-
|
39
|
+
Docs.stats.ns.should == get_stats['ns']
|
32
40
|
end
|
33
41
|
|
34
42
|
it "should have the correct size" do
|
35
|
-
|
43
|
+
Docs.stats.size.should == get_stats['size']
|
36
44
|
end
|
37
45
|
|
38
46
|
it "should have the correct storage size" do
|
39
|
-
|
47
|
+
Docs.stats.storage_size.should == get_stats['storageSize']
|
40
48
|
end
|
41
49
|
|
42
50
|
it "should have the correct average object size" do
|
43
|
-
|
51
|
+
Docs.stats.avg_obj_size.should == get_stats['avgObjSize']
|
44
52
|
end
|
45
53
|
|
46
54
|
it "should have the correct number of extents" do
|
47
|
-
|
55
|
+
if get_stats['numExtents']
|
56
|
+
Docs.stats.num_extents.should == get_stats['numExtents']
|
57
|
+
end
|
48
58
|
end
|
49
59
|
|
50
60
|
it "should have the correct number of indexes" do
|
51
|
-
|
61
|
+
Docs.stats.nindexes.should == get_stats['nindexes']
|
52
62
|
end
|
53
63
|
|
54
64
|
it "should have the correct last extent size" do
|
55
|
-
|
65
|
+
if get_stats['lastExtentSize']
|
66
|
+
Docs.stats.last_extent_size.should == get_stats['lastExtentSize']
|
67
|
+
end
|
56
68
|
end
|
57
69
|
|
58
70
|
it "should have the correct padding factor" do
|
59
|
-
|
71
|
+
if get_stats['paddingFactor']
|
72
|
+
Docs.stats.padding_factor.should == get_stats['paddingFactor']
|
73
|
+
end
|
60
74
|
end
|
61
75
|
|
62
76
|
it "should have the correct user flags" do
|
63
|
-
|
77
|
+
if get_stats['userFlags']
|
78
|
+
Docs.stats.user_flags.should == get_stats['userFlags']
|
79
|
+
end
|
64
80
|
end
|
65
81
|
|
66
82
|
it "should have the correct total index size" do
|
67
|
-
|
83
|
+
Docs.stats.total_index_size.should == get_stats['totalIndexSize']
|
68
84
|
end
|
69
85
|
end
|
70
86
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Strong parameters" do
|
4
|
+
context 'A document with strong parameters protection' do
|
5
|
+
if ::ActiveModel.const_defined?(:ForbiddenAttributesProtection)
|
6
|
+
require "action_controller/metal/strong_parameters"
|
7
|
+
|
8
|
+
before do
|
9
|
+
@doc_class = Doc do
|
10
|
+
plugin MongoMapper::Plugins::StrongParameters
|
11
|
+
|
12
|
+
key :name, String
|
13
|
+
key :admin, Boolean, :default => false
|
14
|
+
end
|
15
|
+
|
16
|
+
@doc = @doc_class.create(:name => 'Steve Sloan')
|
17
|
+
end
|
18
|
+
|
19
|
+
let(:params) {
|
20
|
+
{name: "Permitted", admin: true}
|
21
|
+
}
|
22
|
+
|
23
|
+
let(:strong_params) {
|
24
|
+
ActionController::Parameters.new params
|
25
|
+
}
|
26
|
+
|
27
|
+
it "allows assignment of attribute hashes" do
|
28
|
+
@doc.attributes = params
|
29
|
+
@doc.name.should =="Permitted"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "doesn't allow mass assignment of ActionController::Parameters" do
|
33
|
+
lambda {
|
34
|
+
@doc.attributes = strong_params
|
35
|
+
}.should raise_error(ActiveModel::ForbiddenAttributesError)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "does not allow mass assignment of non-permitted attributes" do
|
39
|
+
@doc.attributes = strong_params.permit(:name)
|
40
|
+
@doc.admin.should == false
|
41
|
+
end
|
42
|
+
|
43
|
+
it "allows mass assignment of permitted attributes" do
|
44
|
+
@doc.attributes = strong_params.permit(:name)
|
45
|
+
@doc.name.should == "Permitted"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -157,8 +157,7 @@ describe "Validations" do
|
|
157
157
|
doc = @document.new("name" => "joe")
|
158
158
|
doc.save.should be_truthy
|
159
159
|
|
160
|
-
|
161
|
-
receive(:first).
|
160
|
+
@document.stub(:first).
|
162
161
|
with(:name => 'joe').
|
163
162
|
and_return(doc)
|
164
163
|
|
@@ -171,8 +170,7 @@ describe "Validations" do
|
|
171
170
|
doc = @document.new("name" => "joe")
|
172
171
|
doc.save.should be_truthy
|
173
172
|
|
174
|
-
|
175
|
-
receive(:first).
|
173
|
+
@document.stub(:first).
|
176
174
|
with(:name => 'joe').
|
177
175
|
and_return(doc)
|
178
176
|
|
@@ -189,8 +187,7 @@ describe "Validations" do
|
|
189
187
|
doc = document.new("name" => "")
|
190
188
|
doc.save.should be_truthy
|
191
189
|
|
192
|
-
|
193
|
-
receive(:first).
|
190
|
+
@document.stub(:first).
|
194
191
|
with(:name => '').
|
195
192
|
and_return(doc)
|
196
193
|
|
@@ -272,8 +269,7 @@ describe "Validations" do
|
|
272
269
|
doc = @document.new("name" => "joe", "scope" => "one")
|
273
270
|
doc.save.should be_truthy
|
274
271
|
|
275
|
-
|
276
|
-
receive(:first).
|
272
|
+
@document.stub(:first).
|
277
273
|
with(:name => 'joe', :scope => "one").
|
278
274
|
and_return(doc)
|
279
275
|
|
@@ -285,8 +281,7 @@ describe "Validations" do
|
|
285
281
|
doc = @document.new("name" => "joe", "scope" => "one")
|
286
282
|
doc.save.should be_truthy
|
287
283
|
|
288
|
-
|
289
|
-
receive(:first).
|
284
|
+
@document.stub(:first).
|
290
285
|
with(:name => 'joe', :scope => 'two').
|
291
286
|
and_return(nil)
|
292
287
|
|
@@ -309,8 +304,7 @@ describe "Validations" do
|
|
309
304
|
doc = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "two")
|
310
305
|
doc.save.should be_truthy
|
311
306
|
|
312
|
-
|
313
|
-
receive(:first).
|
307
|
+
@document.stub(:first).
|
314
308
|
with(:name => 'joe', :first_scope => 'one', :second_scope => 'two').
|
315
309
|
and_return(doc)
|
316
310
|
|
@@ -322,8 +316,7 @@ describe "Validations" do
|
|
322
316
|
doc = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "two")
|
323
317
|
doc.save.should be_truthy
|
324
318
|
|
325
|
-
|
326
|
-
receive(:first).
|
319
|
+
@document.stub(:first).
|
327
320
|
with(:name => 'joe', :first_scope => 'one', :second_scope => 'one').
|
328
321
|
and_return(nil)
|
329
322
|
|
@@ -403,8 +396,7 @@ describe "Validations" do
|
|
403
396
|
# doc = @document.create(:name => 'John')
|
404
397
|
# doc.should_not have_error_on(:name)
|
405
398
|
#
|
406
|
-
#
|
407
|
-
# receive(:first).
|
399
|
+
# @document.stub(:first).
|
408
400
|
# with(:name => 'John').
|
409
401
|
# and_return(doc)
|
410
402
|
#
|