From a434675b24b72efb0ce53bc8d136e8fbdf8e5e55 Mon Sep 17 00:00:00 2001 From: johnnyshields <27655+johnnyshields@users.noreply.github.com> Date: Fri, 24 Nov 2023 02:16:27 +0900 Subject: [PATCH] Remove deprecated Document#as_json hack --- docs/release-notes/mongoid-9.0.txt | 10 +-- lib/mongoid/document.rb | 27 -------- lib/mongoid/warnings.rb | 1 - spec/mongoid/criteria_spec.rb | 19 +---- spec/mongoid/document_spec.rb | 107 ----------------------------- 5 files changed, 7 insertions(+), 157 deletions(-) diff --git a/docs/release-notes/mongoid-9.0.txt b/docs/release-notes/mongoid-9.0.txt index 8209381ced..b8d45c43b8 100644 --- a/docs/release-notes/mongoid-9.0.txt +++ b/docs/release-notes/mongoid-9.0.txt @@ -17,11 +17,6 @@ The complete list of releases is available `on GitHub please consult GitHub releases for detailed release notes and JIRA for the complete list of issues fixed in each release, including bug fixes. -Deprecated class ``Mongoid::Errors::InvalidStorageParent`` removed ------------------------------------------------------------------- - -The deprecated class ``Mongoid::Errors::InvalidStorageParent`` has been removed. - ``around_*`` callbacks for embedded documents are now ignored ------------------------------------------------------------- @@ -75,11 +70,16 @@ prior has been dropped (you must use a minimum of version 8.0.) Deprecated functionality removed -------------------------------- +**Breaking change:** The following deprecated functionality is now removed: + - The ``Mongoid::QueryCache`` module has been removed. Please replace any usages 1-for-1 with ``Mongo::QueryCache``. The method ``Mongoid::QueryCache#clear_cache`` should be replaced with ``Mongo::QueryCache#clear``. All other methods and submodules are identically named. Refer to the `driver query cache documentation `_ for more details. - ``Object#blank_criteria?`` method is removed (was previously deprecated.) +- ``Document#as_json :compact`` option is removed. Please call ```#compact`` on the + returned ``Hash`` object instead. +- The deprecated class ``Mongoid::Errors::InvalidStorageParent`` has been removed. ``touch`` method now clears changed state diff --git a/lib/mongoid/document.rb b/lib/mongoid/document.rb index b619ace2b8..d2382a8b28 100644 --- a/lib/mongoid/document.rb +++ b/lib/mongoid/document.rb @@ -135,33 +135,6 @@ def as_document BSON::Document.new(as_attributes) end - # Calls #as_json on the document with additional, Mongoid-specific options. - # - # @note Rails 6 changes return value of as_json for non-primitive types - # such as BSON::ObjectId. In Rails <= 5, as_json returned these as - # instances of the class. In Rails 6, these are returned serialized to - # primitive types (e.g. {'$oid'=>'5bcfc40bde340b37feda98e9'}). - # See https://github.com/rails/rails/commit/2e5cb980a448e7f4ab00df6e9ad4c1cc456616aa - # for more information. - # - # @example Get the document as json. - # document.as_json(compact: true) - # - # @param [ Hash ] options The options. - # - # @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::Warnings.warn_as_json_compact_deprecated - rv = rv.compact - end - rv - end - # Returns an instance of the specified class with the attributes, # errors, and embedded documents of the current document. # diff --git a/lib/mongoid/warnings.rb b/lib/mongoid/warnings.rb index 0a41abf3e9..79333dcfcf 100644 --- a/lib/mongoid/warnings.rb +++ b/lib/mongoid/warnings.rb @@ -30,7 +30,6 @@ def warning(id, message) end warning :geo_haystack_deprecated, 'The geoHaystack type is deprecated.' - warning :as_json_compact_deprecated, '#as_json :compact option is deprecated. Please call #compact on the returned Hash object instead.' warning :symbol_type_deprecated, 'The BSON Symbol type is deprecated by MongoDB. Please use String or StringifiedSymbol field types instead of the Symbol field type.' warning :legacy_readonly, 'The readonly! method will only mark the document readonly when the legacy_readonly feature flag is switched off.' warning :mutable_ids, 'Ignoring updates to immutable attribute `_id`. Please set Mongoid::Config.immutable_ids to true and update your code so that `_id` is never updated.' diff --git a/spec/mongoid/criteria_spec.rb b/spec/mongoid/criteria_spec.rb index 3808166cc1..c65ab926cf 100644 --- a/spec/mongoid/criteria_spec.rb +++ b/spec/mongoid/criteria_spec.rb @@ -288,23 +288,8 @@ Band.where(name: "Depeche Mode") end - # as_json changed in rails 6 to call as_json on serializable_hash. - # https://github.com/rails/rails/commit/2e5cb980a448e7f4ab00df6e9ad4c1cc456616aa - - context 'rails < 6' do - max_rails_version '5.2' - - it "returns the criteria as a json hash" do - expect(criteria.as_json).to eq([ band.serializable_hash ]) - end - end - - context 'rails >= 6' do - min_rails_version '6.0' - - it "returns the criteria as a json hash" do - expect(criteria.as_json).to eq([ band.serializable_hash.as_json ]) - end + it "returns the criteria as a json hash" do + expect(criteria.as_json).to eq([ band.serializable_hash.as_json ]) end end diff --git a/spec/mongoid/document_spec.rb b/spec/mongoid/document_spec.rb index 547b5f26b1..03915b1669 100644 --- a/spec/mongoid/document_spec.rb +++ b/spec/mongoid/document_spec.rb @@ -429,113 +429,6 @@ class << self; attr_accessor :name; end end end end - - context "when the Mongoid-specific options are provided" do - - let(:options) do - { compact: true } - end - - it "applies the Mongoid-specific options" do - expect(person.as_json(options)["title"]).to eq("Sir") - expect(person.as_json(options)["age"]).to eq(100) - expect(person.as_json(options).keys).not_to include("lunch_time") - end - - context "when options for the super method are provided" do - - let(:options) do - { compact: true, only: [:title, :pets, :ssn] } - end - - it "passes the options through to the super method" do - expect(person.as_json(options)["title"]).to eq("Sir") - expect(person.as_json(options)["pets"]).to eq(false) - end - - it "applies the Mongoid-specific options" do - expect(person.as_json(options).keys).not_to include("ssn") - end - end - end - - 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. - max_rails_version '5.2' - - before do - # These tests require a specific set of defined attributes - # on the model - 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(Mongoid::Warnings).to receive(:warn_as_json_compact_deprecated) - church.as_json(compact: true) - end - - it 'does not log a deprecation warning when :compact is not given' do - expect(Mongoid::Warnings).to_not receive(:warn_as_json_compact_deprecated) - church.as_json - end - end - - context 'there is a nil valued attribute' do - let(:church) do - Church.create!(name: 'St. Basil') - end - - it 'returns non-nil fields and _id only' do - actual = church.as_json(compact: true) - expect(actual).to eq('_id' => church.id, 'name' => 'St. Basil') - end - end - - context 'all attrbutes are nil valued' do - let(:church) do - Church.create! - end - - it 'returns a hash with _id only' do - actual = church.as_json(compact: true) - expect(actual).to eq('_id' => church.id) - end - end - - context 'there are no nil valued attributes' do - let(:church) do - Church.create!(name: 'St. Basil', location: {}) - end - - it 'returns all fields and _id' do - actual = church.as_json(compact: true) - expect(actual).to eq('_id' => church.id, 'name' => 'St. Basil', - 'location' => {}) - end - end - - context 'when option is specified as a truthy value' do - let(:church) do - Church.create!(name: 'St. Basil') - end - - it 'returns non-nil fields and _id only' do - actual = church.as_json(compact: 1) - expect(actual).to eq('_id' => church.id, 'name' => 'St. Basil') - end - end - end end describe "#as_document" do