mongo_mapper 0.15.6 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -51,6 +51,39 @@ describe "EmbeddedDocument" do
51
51
  doc.foo._parent_document.should be(doc)
52
52
  doc.foo._root_document.should be(doc)
53
53
  end
54
+
55
+ context "given subclass of embedded document" do
56
+ before do
57
+ @sub_address_class = Subclass(@address_class, 'SubAddress')
58
+ end
59
+
60
+ it "should embed embedded document" do
61
+ address = @sub_address_class.new(:city => 'South Bend', :state => 'IN')
62
+ doc = @klass.create(:foo => address)
63
+ doc.foo.city.should == 'South Bend'
64
+ doc.foo.state.should == 'IN'
65
+
66
+ doc = doc.reload
67
+ doc.foo.city.should == 'South Bend'
68
+ doc.foo.state.should == 'IN'
69
+ end
70
+
71
+ it "should assign _parent_document and _root_document" do
72
+ address = @sub_address_class.new(:city => 'South Bend', :state => 'IN')
73
+ address._parent_document.should be_nil
74
+ doc = @klass.create(:foo => address)
75
+ address._parent_document.should be(doc)
76
+ address._root_document.should be(doc)
77
+ end
78
+
79
+ it "should assign _parent_document and _root_document when loading" do
80
+ address = @sub_address_class.new(:city => 'South Bend', :state => 'IN')
81
+ doc = @klass.create(:foo => address)
82
+ doc.reload
83
+ doc.foo._parent_document.should be(doc)
84
+ doc.foo._root_document.should be(doc)
85
+ end
86
+ end
54
87
  end
55
88
 
56
89
  it "should correctly instantiate single collection inherited embedded documents" do
@@ -367,4 +367,80 @@ describe "Keys" do
367
367
  instance.a_num.should == 10
368
368
  end
369
369
  end
370
+
371
+ describe 'default value is child of embedded class' do
372
+ class EmbeddedParent
373
+ include MongoMapper::EmbeddedDocument
374
+ end
375
+ class EmbeddedChild < EmbeddedParent
376
+ end
377
+
378
+ context 'with type' do
379
+ class DocumentWithEmbeddedAndDefaultValue
380
+ include MongoMapper::Document
381
+ key :my_embedded, EmbeddedParent, default: -> { EmbeddedChild.new }
382
+ end
383
+
384
+ it "should work" do
385
+ instance = DocumentWithEmbeddedAndDefaultValue.new
386
+ instance.my_embedded.should be_instance_of(EmbeddedChild)
387
+ end
388
+ end
389
+
390
+ context 'without type' do
391
+ class DocumentWithEmbeddedAndDefaultValueWithoutType
392
+ include MongoMapper::Document
393
+ key :my_embedded, EmbeddedParent, default: -> { EmbeddedChild.new }
394
+ end
395
+
396
+ it "should work" do
397
+ instance = DocumentWithEmbeddedAndDefaultValueWithoutType.new
398
+ instance.my_embedded.should be_instance_of(EmbeddedChild)
399
+ end
400
+ end
401
+ end
402
+
403
+ describe 'default value is a custom class' do
404
+ class TimeOfDay
405
+ attr_reader :seconds
406
+
407
+ def initialize(seconds)
408
+ @seconds = seconds
409
+ end
410
+
411
+ def self.from_mongo(value)
412
+ return nil if value.blank?
413
+
414
+ new(value.to_i)
415
+ end
416
+
417
+ def self.to_mongo(value)
418
+ return nil if value.blank?
419
+
420
+ value.seconds
421
+ end
422
+ end
423
+
424
+ context 'with type' do
425
+ class DocumentWithCustomClass
426
+ include MongoMapper::Document
427
+ key :my_embedded, TimeOfDay, default: -> { TimeOfDay.new(900) }
428
+ end
429
+ it "should work" do
430
+ instance = DocumentWithCustomClass.new
431
+ instance.my_embedded.should be_instance_of(TimeOfDay)
432
+ end
433
+ end
434
+
435
+ context 'without type' do
436
+ class DocumentWithCustomClassWithoutType
437
+ include MongoMapper::Document
438
+ key :my_embedded, default: -> { TimeOfDay.new(900) }
439
+ end
440
+ it "should work" do
441
+ instance = DocumentWithCustomClass.new
442
+ instance.my_embedded.should be_instance_of(TimeOfDay)
443
+ end
444
+ end
445
+ end
370
446
  end
data/spec/spec_helper.rb CHANGED
@@ -69,6 +69,18 @@ def EDoc(name='Class', &block)
69
69
  klass
70
70
  end
71
71
 
72
+ def Subclass(super_class, name='Subclass', &block)
73
+ klass = Class.new(super_class) do
74
+ if name
75
+ class_eval "def self.name; '#{name}' end"
76
+ class_eval "def self.to_s; '#{name}' end"
77
+ end
78
+ end
79
+
80
+ klass.class_eval(&block) if block_given?
81
+ klass
82
+ end
83
+
72
84
  def drop_indexes(klass)
73
85
  klass.collection.indexes.drop_all if klass.database.collection_names.include?(klass.collection.name)
74
86
  end
@@ -44,4 +44,54 @@ describe "Inspect" do
44
44
  doc.inspect.should =~ /_id:.*, pet: .*_id.*, name: "Kitten".*/
45
45
  end
46
46
  end
47
+
48
+ context "#inspect with filter_attributes" do
49
+ before do
50
+ MongoMapper::Utils.remove_instance_variable(:@filter) if MongoMapper::Utils.instance_variable_defined?(:@filter)
51
+ MongoMapper.filter_attributes = [:email, :card_number, :phone_number]
52
+ end
53
+
54
+ after do
55
+ MongoMapper.filter_attributes =[]
56
+ MongoMapper::Utils.remove_instance_variable(:@filter) if MongoMapper::Utils.instance_variable_defined?(:@filter)
57
+ end
58
+
59
+ it "should filter the fields given by filter_attributes" do
60
+ document = Doc('User') do
61
+ key :name, String
62
+ key :age, Integer
63
+ key :email, String
64
+ key :card_number, String
65
+ end
66
+ doc = document.new(
67
+ :name => 'John',
68
+ :age => 29,
69
+ :email => 'mongomapper@example.com',
70
+ :card_number => '123'
71
+ )
72
+
73
+ if ActiveSupport.version >= Gem::Version.new("6.0")
74
+ doc.inspect.should =~ /_id:.*, age: 29, card_number: \[FILTERED\], email: \[FILTERED\], name: "John"/
75
+ else
76
+ doc.inspect.should =~ /_id:.*, age: 29, card_number: "123", email: "mongomapper@example.com", name: "John"/
77
+ end
78
+ end
79
+
80
+ it "should filter the fields given by filter_attributes for embedded document" do
81
+ document = EDoc('Profile') do
82
+ key :job, String
83
+ key :phone_number, String
84
+ end
85
+ doc = document.new(
86
+ :job => 'Software Engineer',
87
+ :phone_number => '09011110000'
88
+ )
89
+
90
+ if ActiveSupport.version >= Gem::Version.new("6.0")
91
+ doc.inspect.should =~ /job: "Software Engineer", phone_number: \[FILTERED\]/
92
+ else
93
+ doc.inspect.should =~ /job: "Software Engineer", phone_number: "09011110000"/
94
+ end
95
+ end
96
+ end
47
97
  end
@@ -276,6 +276,17 @@ describe "Key" do
276
276
  it "should work with procs" do
277
277
  Key.new(:foo, String, :default => lambda { return 'hello world' }).default_value.should == "hello world"
278
278
  end
279
+
280
+ it "should work with embedded document" do
281
+ embedded = EDoc()
282
+ Key.new(:foo, embedded, :default => lambda { embedded.new }).default_value.should be_instance_of(embedded)
283
+ end
284
+
285
+ it "should work with subclass of embedded document" do
286
+ embedded = EDoc()
287
+ subclass = Subclass(embedded)
288
+ Key.new(:foo, embedded, :default => lambda { subclass.new }).default_value.should be_instance_of(subclass)
289
+ end
279
290
  end
280
291
  end
281
292
  end # KeyTest
@@ -139,4 +139,13 @@ describe "MongoMapper" do
139
139
  Mongo::Client.instance_methods.should include(:reconnect) # v1
140
140
  end
141
141
  end
142
+
143
+ context "options" do
144
+ it "should sets/returns filtered_attributes correctly" do
145
+ MongoMapper.filter_attributes.should == []
146
+ filtered_attributes = [:password, :credit_number]
147
+ MongoMapper.filter_attributes = filtered_attributes
148
+ MongoMapper.filter_attributes.should == filtered_attributes
149
+ end
150
+ end
142
151
  end
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.6
4
+ version: 0.16.0
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: 2022-06-10 00:00:00.000000000 Z
13
+ date: 2023-10-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mongo
@@ -125,6 +125,7 @@ files:
125
125
  - examples/validating/embedded_docs.rb
126
126
  - lib/mongo_mapper.rb
127
127
  - lib/mongo_mapper/connection.rb
128
+ - lib/mongo_mapper/deprecator.rb
128
129
  - lib/mongo_mapper/document.rb
129
130
  - lib/mongo_mapper/embedded_document.rb
130
131
  - lib/mongo_mapper/exceptions.rb
@@ -145,6 +146,7 @@ files:
145
146
  - lib/mongo_mapper/extensions/time.rb
146
147
  - lib/mongo_mapper/locale/en.yml
147
148
  - lib/mongo_mapper/middleware/identity_map.rb
149
+ - lib/mongo_mapper/options.rb
148
150
  - lib/mongo_mapper/plugins.rb
149
151
  - lib/mongo_mapper/plugins/accessible.rb
150
152
  - lib/mongo_mapper/plugins/active_model.rb