Skip to content

Commit eaa6d26

Browse files
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 af797ed commit eaa6d26

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
@@ -173,13 +173,14 @@ def as_document
173173
#
174174
# @param [ Hash ] options The options.
175175
#
176-
# @option options [ true, false ] :compact Whether to include fields with
177-
# nil values in the json document.
176+
# @option options [ true, false ] :compact (Deprecated) Whether to include fields
177+
# with nil values in the json document.
178178
#
179179
# @return [ Hash ] The document as json.
180180
def as_json(options = nil)
181181
rv = super
182182
if options && options[:compact]
183+
Mongoid.logger.warn('#as_json :compact option is deprecated. Please call #compact on the returned Hash object instead.')
183184
rv = rv.compact
184185
end
185186
rv

lib/mongoid/tasks/database.rb

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

120120
if model.embedded? && !model.cyclic?
121-
logger.warn("MONGOID: #{model} has shard config but is emdedded")
121+
logger.warn("MONGOID: #{model} has shard config but is embedded")
122122
next
123123
end
124124

spec/mongoid/document_spec.rb

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

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

515+
context 'deprecation' do
516+
let(:church) do
517+
Church.create!(name: 'St. Basil')
518+
end
519+
520+
let(:message) do
521+
'#as_json :compact option is deprecated. Please call #compact on the returned Hash object instead.'
522+
end
523+
524+
it 'logs a deprecation warning when :compact is given' do
525+
expect_any_instance_of(Logger).to receive(:warn).with(message)
526+
church.as_json(compact: true)
527+
end
528+
529+
it 'does not log a deprecation warning when :compact is not given' do
530+
expect_any_instance_of(Logger).to_not receive(:warn).with(message)
531+
church.as_json
532+
end
533+
end
534+
515535
context 'there is a nil valued attribute' do
516536
let(:church) do
517537
Church.create!(name: 'St. Basil')

0 commit comments

Comments
 (0)