-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MONGOID-5293 Add Rails-style defaults & config.load_defaults (#5405)
* MONGOID-5293 add skeleton * MONGOID-5293 add load_defaults method to config * MONGOID-5293 comments * MONGOID-5293 add docs and release notes
- Loading branch information
1 parent
346feef
commit 4d757e5
Showing
6 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mongoid | ||
module Config | ||
|
||
# Encapsulates logic for loading defaults. | ||
module Defaults | ||
|
||
# Load the defaults for the feature flags in the given Mongoid version. | ||
# Note that this method will load the *new* functionality introduced in | ||
# the given Mongoid version. | ||
# | ||
# @param [ String | Float ] The version number as X.y. | ||
# | ||
# raises [ ArgumentError ] if an invalid version is given. | ||
def load_defaults(version) | ||
# Note that for 7.x, since all of the feature flag defaults have been | ||
# flipped to the new functionality, all of the settings for those | ||
# versions are to give old functionality. Because of this, it is | ||
# possible to recurse to later version to get all of the options to | ||
# turn off. Note that this won't be true when adding feature flags to | ||
# 8.1, since the default will be the old functionality until the next | ||
# major version is released. More likely, the recursion will have to go | ||
# in the other direction (towards earlier versions). | ||
|
||
case version.to_s | ||
when "7.3" | ||
# flags introduced in 7.4 - old functionality | ||
self.broken_aggregables = true | ||
self.broken_alias_handling = true | ||
self.broken_and = true | ||
self.broken_scoping = true | ||
self.broken_updates = true | ||
self.compare_time_by_ms = false | ||
self.legacy_pluck_distinct = true | ||
self.legacy_triple_equals = true | ||
self.object_id_as_json_oid = true | ||
|
||
load_defaults "7.4" | ||
when "7.4" | ||
# flags introduced in 7.5 - old functionality | ||
self.overwrite_chained_operators = true | ||
|
||
load_defaults "7.5" | ||
when "7.5" | ||
# flags introduced in 8.0 - old functionality | ||
self.legacy_attributes = true | ||
self.map_big_decimal_to_decimal128 = false | ||
when "8.0" | ||
# All flag defaults currently reflect 8.0 behavior. | ||
when "8.1" | ||
# All flag defaults currently reflect 8.1 behavior. | ||
else | ||
raise ArgumentError, "Unknown version: #{version}" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe Mongoid::Config::Defaults do | ||
|
||
let(:config) do | ||
Mongoid::Config | ||
end | ||
|
||
describe ".load_defaults" do | ||
|
||
shared_examples "turns off 7.4 flags" do | ||
it "turns off the 7.4 flags" do | ||
expect(Mongoid.broken_aggregables).to be true | ||
expect(Mongoid.broken_alias_handling).to be true | ||
expect(Mongoid.broken_and).to be true | ||
expect(Mongoid.broken_scoping).to be true | ||
expect(Mongoid.broken_updates).to be true | ||
expect(Mongoid.compare_time_by_ms).to be false | ||
expect(Mongoid.legacy_pluck_distinct).to be true | ||
expect(Mongoid.legacy_triple_equals).to be true | ||
expect(Mongoid.object_id_as_json_oid).to be true | ||
end | ||
end | ||
|
||
shared_examples "turns on 7.4 flags" do | ||
it "turns on the 7.4 flags" do | ||
expect(Mongoid.broken_aggregables).to be false | ||
expect(Mongoid.broken_alias_handling).to be false | ||
expect(Mongoid.broken_and).to be false | ||
expect(Mongoid.broken_scoping).to be false | ||
expect(Mongoid.broken_updates).to be false | ||
expect(Mongoid.compare_time_by_ms).to be true | ||
expect(Mongoid.legacy_pluck_distinct).to be false | ||
expect(Mongoid.legacy_triple_equals).to be false | ||
expect(Mongoid.object_id_as_json_oid).to be false | ||
end | ||
end | ||
|
||
shared_examples "turns off 7.5 flags" do | ||
it "turns off the 7.5 flags" do | ||
expect(Mongoid.overwrite_chained_operators).to be true | ||
end | ||
end | ||
|
||
shared_examples "turns on 7.5 flags" do | ||
it "turns on the 7.5 flags" do | ||
expect(Mongoid.overwrite_chained_operators).to be false | ||
end | ||
end | ||
|
||
shared_examples "turns off 8.0 flags" do | ||
it "turns off the 8.0 flags" do | ||
expect(Mongoid.legacy_attributes).to be true | ||
expect(Mongoid.map_big_decimal_to_decimal128).to be false | ||
end | ||
end | ||
|
||
shared_examples "turns on 8.0 flags" do | ||
it "turns on the 8.0 flags" do | ||
expect(Mongoid.legacy_attributes).to be false | ||
expect(Mongoid.map_big_decimal_to_decimal128).to be true | ||
end | ||
end | ||
|
||
context "when giving a valid version" do | ||
|
||
before do | ||
config.load_defaults(version) | ||
end | ||
|
||
after do | ||
Mongoid::Config.reset | ||
end | ||
|
||
context "when the given version is 7.3" do | ||
|
||
let(:version) { 7.3 } | ||
|
||
it_behaves_like "turns off 7.4 flags" | ||
it_behaves_like "turns off 7.5 flags" | ||
it_behaves_like "turns off 8.0 flags" | ||
end | ||
|
||
context "when the given version is 7.4" do | ||
|
||
let(:version) { 7.4 } | ||
|
||
it_behaves_like "turns on 7.4 flags" | ||
it_behaves_like "turns off 7.5 flags" | ||
it_behaves_like "turns off 8.0 flags" | ||
end | ||
|
||
context "when the given version is 7.5" do | ||
|
||
let(:version) { 7.5 } | ||
|
||
it_behaves_like "turns on 7.4 flags" | ||
it_behaves_like "turns on 7.5 flags" | ||
it_behaves_like "turns off 8.0 flags" | ||
end | ||
|
||
context "when the given version is 8.0" do | ||
|
||
let(:version) { 8.0 } | ||
|
||
it_behaves_like "turns on 7.4 flags" | ||
it_behaves_like "turns on 7.5 flags" | ||
it_behaves_like "turns on 8.0 flags" | ||
end | ||
|
||
context "when the given version is 8.1" do | ||
|
||
let(:version) { 8.0 } | ||
|
||
it_behaves_like "turns on 7.4 flags" | ||
it_behaves_like "turns on 7.5 flags" | ||
it_behaves_like "turns on 8.0 flags" | ||
end | ||
end | ||
|
||
context "when given version an invalid version" do | ||
let(:version) { 4.2 } | ||
|
||
it "raises an error" do | ||
expect do | ||
config.load_defaults(version) | ||
end.to raise_error(ArgumentError, /Unknown version: 4.2/) | ||
end | ||
end | ||
end | ||
end |