mongo_mapper 0.15.0 → 0.15.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -8
  3. data/lib/mongo_mapper.rb +2 -0
  4. data/lib/mongo_mapper/plugins/accessible.rb +1 -1
  5. data/lib/mongo_mapper/plugins/associations/base.rb +10 -2
  6. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +36 -6
  7. data/lib/mongo_mapper/plugins/associations/in_foreign_array_proxy.rb +136 -0
  8. data/lib/mongo_mapper/plugins/associations/many_association.rb +4 -2
  9. data/lib/mongo_mapper/plugins/associations/proxy.rb +5 -1
  10. data/lib/mongo_mapper/plugins/associations/single_association.rb +5 -4
  11. data/lib/mongo_mapper/plugins/identity_map.rb +3 -1
  12. data/lib/mongo_mapper/plugins/keys.rb +8 -0
  13. data/lib/mongo_mapper/plugins/keys/key.rb +13 -8
  14. data/lib/mongo_mapper/plugins/modifiers.rb +12 -4
  15. data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +4 -3
  16. data/lib/mongo_mapper/plugins/strong_parameters.rb +26 -0
  17. data/lib/mongo_mapper/railtie.rb +1 -0
  18. data/lib/mongo_mapper/version.rb +1 -1
  19. data/spec/examples.txt +1655 -1581
  20. data/spec/functional/accessible_spec.rb +6 -0
  21. data/spec/functional/associations/belongs_to_proxy_spec.rb +18 -1
  22. data/spec/functional/associations/in_array_proxy_spec.rb +135 -0
  23. data/spec/functional/associations/in_foreign_array_proxy_spec.rb +321 -0
  24. data/spec/functional/document_spec.rb +0 -3
  25. data/spec/functional/identity_map_spec.rb +2 -2
  26. data/spec/functional/keys_spec.rb +29 -11
  27. data/spec/functional/modifiers_spec.rb +14 -0
  28. data/spec/functional/querying_spec.rb +22 -0
  29. data/spec/functional/strong_parameters_spec.rb +49 -0
  30. data/spec/unit/associations/proxy_spec.rb +0 -4
  31. data/spec/unit/keys_spec.rb +10 -1
  32. data/spec/unit/validations_spec.rb +18 -18
  33. metadata +6 -2
@@ -257,9 +257,6 @@ describe "Document" do
257
257
  @instance.bar.should_not be_nil
258
258
  end
259
259
 
260
- it "should reset nil one association" do
261
- end
262
-
263
260
  it "should reinstantiate embedded associations" do
264
261
  @instance.reload
265
262
  @instance.bars.first.name.should == '1'
@@ -461,7 +461,7 @@ describe "IdentityMap" do
461
461
 
462
462
  blog = Blog.create(:title => 'Jill', :parent => root)
463
463
  assert_in_map(blog)
464
- root.should equal(blog.parent.target)
464
+ root.should equal(blog.parent)
465
465
  end
466
466
 
467
467
  it "should work correctly with one proxy" do
@@ -470,7 +470,7 @@ describe "IdentityMap" do
470
470
 
471
471
  root = Item.create(:title => 'Root', :blog => blog)
472
472
  assert_in_map(root)
473
- blog.should equal(root.blog.target)
473
+ blog.should equal(root.blog)
474
474
  end
475
475
 
476
476
  it "should work correctly with one proxy create" do
@@ -34,28 +34,46 @@ describe "Keys" do
34
34
  instance.foo.should == 'baz'
35
35
  end
36
36
 
37
- context "when persisting an typecasted array" do
38
- TypecastedKeyModel = Doc do
39
- key :people, Array, :typecast => "Person"
40
- end
41
-
37
+ context "when persisting typecasts" do
42
38
  Person = Struct.new(:name) do
43
39
  def self.to_mongo(value)
44
40
  value.name
45
41
  end
46
42
 
47
43
  def self.from_mongo(value)
48
- Person.new value
44
+ new(value)
49
45
  end
50
46
  end
51
47
 
52
- it "should not mutate the model's state" do
53
- person = Person.new "Bob"
54
- doc = TypecastedKeyModel.new(:people => [person])
48
+ context "when persisting a typecast Array" do
49
+ typecast_key_model = Doc do
50
+ key :people, Array, :typecast => "Person"
51
+ end
52
+
53
+ it "should not mutate the model's state" do
54
+ person = Person.new "Bob"
55
+ doc = typecast_key_model.new(:people => [person])
55
56
 
56
- doc.save
57
+ doc.save!
58
+
59
+ doc.people.should == [person]
60
+ end
61
+ end
57
62
 
58
- doc.people.should == [person]
63
+ context "when persisting a typecast Set" do
64
+ typecast_key_model = Doc do
65
+ key :people, Set, :typecast => "Person"
66
+ end
67
+
68
+ it "should not mutate the model's state" do
69
+ person = Person.new "Bob"
70
+
71
+ doc = typecast_key_model.new(:people => Set.new([person]))
72
+
73
+ doc.save!
74
+
75
+ doc.people.should == Set.new([person])
76
+ end
59
77
  end
60
78
  end
61
79
 
@@ -471,6 +471,13 @@ module Modifiers
471
471
  assert_page_counts page, 1, 2, 3
472
472
  end
473
473
 
474
+ it "should be able to increment with just the field name" do
475
+ page = page_class.create
476
+ page.increment :day_count
477
+
478
+ assert_page_counts page, 1, 0, 0
479
+ end
480
+
474
481
  it "should be able to decrement with modifier hashes" do
475
482
  page = page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)
476
483
  page.decrement(:day_count => 1, :week_count => 2, :month_count => 3)
@@ -478,6 +485,13 @@ module Modifiers
478
485
  assert_page_counts page, 0, 0, 0
479
486
  end
480
487
 
488
+ it "should be able to decrement with just the field name" do
489
+ page = page_class.create
490
+ page.decrement :day_count
491
+
492
+ assert_page_counts page, -1, 0, 0
493
+ end
494
+
481
495
  it "should always decrement when decrement is called whether number is positive or negative" do
482
496
  page = page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)
483
497
  page.decrement(:day_count => -1, :week_count => 2, :month_count => -3)
@@ -219,6 +219,16 @@ describe "Querying" do
219
219
  end
220
220
  end
221
221
 
222
+ context "(with array of single id)" do
223
+ it "should return an array" do
224
+ document.find([@doc1._id]).should == [@doc1]
225
+ end
226
+
227
+ it "should return an array for find!" do
228
+ document.find!([@doc1._id]).should == [@doc1]
229
+ end
230
+ end
231
+
222
232
  it "should be able to find using condition auto-detection" do
223
233
  document.first(:first_name => 'John').should == @doc1
224
234
  document.all(:last_name => 'Nunemaker', :order => 'age desc').should == [@doc1, @doc3]
@@ -255,6 +265,18 @@ describe "Querying" do
255
265
  document.find_each(:last_name => 'Nunemaker', :order => 'age desc') {|doc| yield_documents << doc }
256
266
  yield_documents.should == [@doc1, @doc3]
257
267
  end
268
+
269
+ it "should return an enumerator when no block is given" do
270
+ yield_documents = []
271
+ enum = document.find_each(:order => "first_name")
272
+ enum.with_index {|doc, idx| yield_documents << [doc, idx] }
273
+ yield_documents.should == [[@doc1, 0], [@doc3, 1], [@doc2, 2]]
274
+
275
+ yield_documents = []
276
+ enum = document.find_each(:last_name => 'Nunemaker', :order => 'age desc')
277
+ enum.with_index {|doc, idx| yield_documents << [doc, idx] }
278
+ yield_documents.should == [[@doc1, 0], [@doc3, 1]]
279
+ end
258
280
  end
259
281
  end # finding documents
260
282
 
@@ -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
@@ -77,10 +77,6 @@ describe "Proxy" do
77
77
  @proxy.proxy_owner.should == @owner
78
78
  end
79
79
 
80
- it "should alias proxy target to target" do
81
- @proxy.proxy_target.should == @target
82
- end
83
-
84
80
  context "send" do
85
81
  it "should work if proxy responds to method" do
86
82
  @proxy.send(:reset)
@@ -1,6 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
-
4
3
  describe "Key" do
5
4
  context ".new with no id and _id of type integer" do
6
5
  it "should not error" do
@@ -152,4 +151,14 @@ describe "Key" do
152
151
  }.should_not change { counter }
153
152
  end
154
153
  end
154
+
155
+ context "with attributes key" do
156
+ it "should raise an error" do
157
+ lambda do
158
+ klass = Doc do
159
+ key :attributes, Hash
160
+ end
161
+ end.should raise_error("`attributes` is a reserved key name")
162
+ end
163
+ end
155
164
  end # KeyTest
@@ -157,19 +157,19 @@ describe "Validations" do
157
157
  end # numericality of
158
158
 
159
159
  context "validating presence of" do
160
- it "should work with validates_presence_of macro" do
161
- @document.key :name, String
162
- @document.validates_presence_of :name
163
- doc = @document.new
164
- doc.should have_error_on(:name)
165
- end
166
-
167
- it "should work with :required shortcut on key definition" do
168
- @document.key :name, String, :required => true
169
- doc = @document.new
170
- doc.should have_error_on(:name)
171
- end
172
- end
160
+ it "should work with validates_presence_of macro" do
161
+ @document.key :name, String
162
+ @document.validates_presence_of :name
163
+ doc = @document.new
164
+ doc.should have_error_on(:name)
165
+ end
166
+
167
+ it "should work with :required shortcut on key definition" do
168
+ @document.key :name, String, :required => true
169
+ doc = @document.new
170
+ doc.should have_error_on(:name)
171
+ end
172
+ end
173
173
 
174
174
  context "validating exclusion of" do
175
175
  it "should throw error if enumerator not provided" do
@@ -436,19 +436,19 @@ describe "Validations" do
436
436
  end # numericality of
437
437
 
438
438
  context "validating presence of" do
439
- it "should work with validates_presence_of macro" do
439
+ it "should work with validates_presence_of macro" do
440
440
  @embedded_doc.key :name, String
441
441
  @embedded_doc.validates_presence_of :name
442
442
  doc = @embedded_doc.new
443
443
  doc.should have_error_on(:name)
444
- end
444
+ end
445
445
 
446
- it "should work with :required shortcut on key definition" do
446
+ it "should work with :required shortcut on key definition" do
447
447
  @embedded_doc.key :name, String, :required => true
448
448
  doc = @embedded_doc.new
449
449
  doc.should have_error_on(:name)
450
- end
451
- end
450
+ end
451
+ end
452
452
 
453
453
  context "validating exclusion of" do
454
454
  it "should throw error if enumerator not provided" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.0
4
+ version: 0.15.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-09-15 00:00:00.000000000 Z
13
+ date: 2020-10-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mongo
@@ -142,6 +142,7 @@ files:
142
142
  - lib/mongo_mapper/plugins/associations/collection.rb
143
143
  - lib/mongo_mapper/plugins/associations/embedded_collection.rb
144
144
  - lib/mongo_mapper/plugins/associations/in_array_proxy.rb
145
+ - lib/mongo_mapper/plugins/associations/in_foreign_array_proxy.rb
145
146
  - lib/mongo_mapper/plugins/associations/many_association.rb
146
147
  - lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb
147
148
  - lib/mongo_mapper/plugins/associations/many_documents_proxy.rb
@@ -188,6 +189,7 @@ files:
188
189
  - lib/mongo_mapper/plugins/scopes.rb
189
190
  - lib/mongo_mapper/plugins/serialization.rb
190
191
  - lib/mongo_mapper/plugins/stats.rb
192
+ - lib/mongo_mapper/plugins/strong_parameters.rb
191
193
  - lib/mongo_mapper/plugins/timestamps.rb
192
194
  - lib/mongo_mapper/plugins/touch.rb
193
195
  - lib/mongo_mapper/plugins/userstamps.rb
@@ -206,6 +208,7 @@ files:
206
208
  - spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb
207
209
  - spec/functional/associations/belongs_to_proxy_spec.rb
208
210
  - spec/functional/associations/in_array_proxy_spec.rb
211
+ - spec/functional/associations/in_foreign_array_proxy_spec.rb
209
212
  - spec/functional/associations/many_documents_as_proxy_spec.rb
210
213
  - spec/functional/associations/many_documents_proxy_spec.rb
211
214
  - spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb
@@ -242,6 +245,7 @@ files:
242
245
  - spec/functional/scopes_spec.rb
243
246
  - spec/functional/static_keys_spec.rb
244
247
  - spec/functional/stats_spec.rb
248
+ - spec/functional/strong_parameters_spec.rb
245
249
  - spec/functional/timestamps_spec.rb
246
250
  - spec/functional/touch_spec.rb
247
251
  - spec/functional/userstamps_spec.rb