Skip to content

Commit eb443d2

Browse files
johnnyshieldsp
authored andcommitted
MONGOID-4737 Deprecate the :compact option on Document#as_json (#5040)
* MONGOID-4737 Deprecate the :compact option on Document#as_json * Update document_spec.rb * Explicitly specify warning to not receive Co-authored-by: shields <[email protected]>
1 parent 3f6e8dc commit eb443d2

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

lib/mongoid/document.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,16 @@ def as_document
194194
#
195195
# @param [ Hash ] options The options.
196196
#
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.
199199
#
200200
# @return [ Hash ] The document as json.
201201
#
202202
# @since 5.1.0
203203
def as_json(options = nil)
204204
rv = super
205205
if options && options[:compact]
206+
Mongoid.logger.warn('#as_json :compact option is deprecated. Please call #compact on the returned Hash object instead.')
206207
rv = rv.compact
207208
end
208209
rv

lib/mongoid/tasks/database.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def shard_collections(models = ::Mongoid.models)
123123
next if model.shard_config.nil?
124124

125125
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")
127127
next
128128
end
129129

spec/mongoid/document_spec.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ class << self; attr_accessor :name; end
501501
end
502502
end
503503

504-
context ':compact option' do
504+
context 'deprecated :compact option' do
505505
# Since rails 6 differs in how it treats id fields,
506506
# run this test on one version of rails. Currently rails 6 is in beta,
507507
# when it is released this version should be changed to 6.
@@ -513,6 +513,26 @@ class << self; attr_accessor :name; end
513513
expect(church.as_json.keys.sort).to eq(%w(_id location name))
514514
end
515515

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+
516536
context 'there is a nil valued attribute' do
517537
let(:church) do
518538
Church.create!(name: 'St. Basil')

0 commit comments

Comments
 (0)