diff --git a/lib/mongoid/document.rb b/lib/mongoid/document.rb index 5fb5365d93..2d603b9f0a 100644 --- a/lib/mongoid/document.rb +++ b/lib/mongoid/document.rb @@ -173,13 +173,14 @@ def as_document # # @param [ Hash ] options The options. # - # @option options [ true, false ] :compact Whether to include fields with - # nil values in the json document. + # @option options [ true, false ] :compact (Deprecated) Whether to include fields + # with nil values in the json document. # # @return [ Hash ] The document as json. def as_json(options = nil) rv = super if options && options[:compact] + Mongoid.logger.warn('#as_json :compact option is deprecated. Please call #compact on the returned Hash object instead.') rv = rv.compact end rv diff --git a/lib/mongoid/tasks/database.rb b/lib/mongoid/tasks/database.rb index 2e9191238c..5a4db0589f 100644 --- a/lib/mongoid/tasks/database.rb +++ b/lib/mongoid/tasks/database.rb @@ -118,7 +118,7 @@ def shard_collections(models = ::Mongoid.models) next if model.shard_config.nil? if model.embedded? && !model.cyclic? - logger.warn("MONGOID: #{model} has shard config but is emdedded") + logger.warn("MONGOID: #{model} has shard config but is embedded") next end diff --git a/spec/mongoid/document_spec.rb b/spec/mongoid/document_spec.rb index 7546b08949..f94186c0a0 100644 --- a/spec/mongoid/document_spec.rb +++ b/spec/mongoid/document_spec.rb @@ -500,7 +500,7 @@ class << self; attr_accessor :name; end end end - context ':compact option' do + context 'deprecated :compact option' do # Since rails 6 differs in how it treats id fields, # run this test on one version of rails. Currently rails 6 is in beta, # when it is released this version should be changed to 6. @@ -512,6 +512,26 @@ class << self; attr_accessor :name; end expect(church.as_json.keys.sort).to eq(%w(_id location name)) end + context 'deprecation' do + let(:church) do + Church.create!(name: 'St. Basil') + end + + let(:message) do + '#as_json :compact option is deprecated. Please call #compact on the returned Hash object instead.' + end + + it 'logs a deprecation warning when :compact is given' do + expect_any_instance_of(Logger).to receive(:warn).with(message) + church.as_json(compact: true) + end + + it 'does not log a deprecation warning when :compact is not given' do + expect_any_instance_of(Logger).to_not receive(:warn).with(message) + church.as_json + end + end + context 'there is a nil valued attribute' do let(:church) do Church.create!(name: 'St. Basil')