File tree Expand file tree Collapse file tree 3 files changed +25
-4
lines changed Expand file tree Collapse file tree 3 files changed +25
-4
lines changed Original file line number Diff line number Diff line change @@ -194,15 +194,16 @@ def as_document
194
194
#
195
195
# @param [ Hash ] options The options.
196
196
#
197
- # @option options [ true, false ] :compact Whether to include fields with
198
- # nil values in the json document.
197
+ # @option options [ true, false ] :compact (Deprecated) Whether to include fields
198
+ # with nil values in the json document.
199
199
#
200
200
# @return [ Hash ] The document as json.
201
201
#
202
202
# @since 5.1.0
203
203
def as_json ( options = nil )
204
204
rv = super
205
205
if options && options [ :compact ]
206
+ Mongoid . logger . warn ( '#as_json :compact option is deprecated. Please call #compact on the returned Hash object instead.' )
206
207
rv = rv . compact
207
208
end
208
209
rv
Original file line number Diff line number Diff line change @@ -123,7 +123,7 @@ def shard_collections(models = ::Mongoid.models)
123
123
next if model . shard_config . nil?
124
124
125
125
if model . embedded? && !model . cyclic?
126
- logger . warn ( "MONGOID: #{ model } has shard config but is emdedded " )
126
+ logger . warn ( "MONGOID: #{ model } has shard config but is embedded " )
127
127
next
128
128
end
129
129
Original file line number Diff line number Diff line change @@ -501,7 +501,7 @@ class << self; attr_accessor :name; end
501
501
end
502
502
end
503
503
504
- context ':compact option' do
504
+ context 'deprecated :compact option' do
505
505
# Since rails 6 differs in how it treats id fields,
506
506
# run this test on one version of rails. Currently rails 6 is in beta,
507
507
# when it is released this version should be changed to 6.
@@ -513,6 +513,26 @@ class << self; attr_accessor :name; end
513
513
expect ( church . as_json . keys . sort ) . to eq ( %w( _id location name ) )
514
514
end
515
515
516
+ context 'deprecation' do
517
+ let ( :church ) do
518
+ Church . create! ( name : 'St. Basil' )
519
+ end
520
+
521
+ let ( :message ) do
522
+ '#as_json :compact option is deprecated. Please call #compact on the returned Hash object instead.'
523
+ end
524
+
525
+ it 'logs a deprecation warning when :compact is given' do
526
+ expect_any_instance_of ( Logger ) . to receive ( :warn ) . with ( message )
527
+ church . as_json ( compact : true )
528
+ end
529
+
530
+ it 'does not log a deprecation warning when :compact is not given' do
531
+ expect_any_instance_of ( Logger ) . to_not receive ( :warn ) . with ( message )
532
+ church . as_json
533
+ end
534
+ end
535
+
516
536
context 'there is a nil valued attribute' do
517
537
let ( :church ) do
518
538
Church . create! ( name : 'St. Basil' )
You can’t perform that action at this time.
0 commit comments