Skip to content

Commit

Permalink
MONGOID-4753 Make global Boolean definition opt-in (#4927)
Browse files Browse the repository at this point in the history
* MONGOID-4753 Make global Boolean definition opt-in

* Add a note about restoring global Boolean

* fix tests

Co-authored-by: Oleg Pudeyev <[email protected]>
  • Loading branch information
p-mongo and p authored Nov 30, 2020
1 parent f4c407c commit a080c06
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 22 deletions.
73 changes: 73 additions & 0 deletions docs/tutorials/mongoid-upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,79 @@ Upgrading to Mongoid 7.3

The following sections describe significant changes in Mongoid 7.3.

``::Boolean`` Removed
---------------------

**Breaking change:** Mongoid 7.3 removes the global ``::Boolean`` class.

This change should have no impact on classes that simply use ``Boolean``
fields, as the ``Boolean`` class is aliased from ``Mongoid::Fields``
(which is included in ``Mongoid::Document``). The following field definition
continues to work in 7.3 as it did in 7.2:

.. code-block:: ruby

class User
include Mongoid::Document

field :verified, type: Boolean
end

However, code that is not executed in the context of a class including
``Mongoid::Document`` may need to explicitly qualify ``Boolean`` references.
The following snippet fails with Mongoid 7.3 due to ``Boolean`` being
unqualified:

.. code-block:: ruby

class User
include Mongoid::Document
end

User.field :verified, type: Boolean

To fix it, use the fully-qualified ``Mongoid::Boolean`` class:

.. code-block:: ruby

User.field :verified, type: Mongoid::Boolean

Note that ``class_eval`` is executed in the scope of the caller, not in
the scope of the class being modified. Thus even when using ``class_eval``
it is necessary to fully qualify ``Mongoid::Boolean``:

.. code-block:: ruby

User.class_eval do
field :verified, type: Mongoid::Boolean
end

Additionally, in Mongoid 7.2 ``::Boolean`` and ``Mongoid::Boolean`` were
different classes. In Mongoid 7.3 there is only one class which is
``Mongoid::Boolean``.

It is possible to restore the global ``::Boolean`` class by executing in
your application:

.. code-block:: ruby

Boolean = Mongoid::Boolean

Note that this aliases ``Mongoid::Boolean`` to ``::Boolean`` such that there
is still only a single Boolean class:

.. code-block:: ruby

# With Mongoid 7.3:
Boolean = Mongoid::Boolean
Boolean == Mongoid::Boolean
# => true

# With Mongoid 7.2:
Boolean == Mongoid::Boolean
# => false


Selector Key Stringification
----------------------------

Expand Down
4 changes: 0 additions & 4 deletions lib/mongoid/criteria/queryable/extensions.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# frozen_string_literal: true
# encoding: utf-8

unless defined?(Boolean)
class Boolean; end
end

if defined?(ActiveSupport)
unless defined?(ActiveSupport::TimeWithZone)
require "active_support/time_with_zone"
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/criteria/queryable/extensions/boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ def evolve(object)
end
end

::Boolean.__send__(:extend, Mongoid::Criteria::Queryable::Extensions::Boolean::ClassMethods)
Mongoid::Boolean.__send__(:extend, Mongoid::Criteria::Queryable::Extensions::Boolean::ClassMethods)
4 changes: 2 additions & 2 deletions lib/mongoid/criteria/queryable/selectable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ def exists(criterion)
end

typed_override(criterion, "$exists") do |value|
::Boolean.evolve(value)
Mongoid::Boolean.evolve(value)
end
end
key :exists, :override, "$exists" do |value|
::Boolean.evolve(value)
Mongoid::Boolean.evolve(value)
end

# Add a $geoIntersects or $geoWithin selection. Symbol operators must
Expand Down
3 changes: 1 addition & 2 deletions lib/mongoid/extensions/boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ class << self
#
# @since 3.0.0
def mongoize(object)
::Boolean.evolve(object)
evolve(object)
end
alias :evolve :mongoize
end
end
end
2 changes: 1 addition & 1 deletion lib/mongoid/extensions/false_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __sortable__
#
# @since 1.0.0
def is_a?(other)
if other == ::Boolean || other.class == ::Boolean
if other == Mongoid::Boolean || other.class == Mongoid::Boolean
return true
end
super(other)
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/extensions/true_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __sortable__
#
# @since 1.0.0
def is_a?(other)
if other == ::Boolean || other.class == ::Boolean
if other == Mongoid::Boolean || other.class == Mongoid::Boolean
return true
end
super(other)
Expand Down
1 change: 1 addition & 0 deletions lib/mongoid/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Fields
extend ActiveSupport::Concern

StringifiedSymbol = Mongoid::StringifiedSymbol
Boolean = Mongoid::Boolean

# For fields defined with symbols use the correct class.
#
Expand Down
2 changes: 1 addition & 1 deletion spec/mongoid/criteria/queryable/extensions/boolean_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require "spec_helper"

describe Boolean do
describe Mongoid::Boolean do

describe ".evolve" do

Expand Down
2 changes: 1 addition & 1 deletion spec/mongoid/extensions/false_class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
context "when provided a Boolean" do

it "returns true" do
expect(false.is_a?(Boolean)).to be true
expect(false.is_a?(Mongoid::Boolean)).to be true
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/mongoid/extensions/true_class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
context "when provided a Boolean" do

it "returns true" do
expect(true.is_a?(Boolean)).to be true
expect(true.is_a?(Mongoid::Boolean)).to be true
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/mongoid/fields/localized_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@
context "when the value is false" do

let(:field) do
described_class.new(:boolean_value, localize: true, type: Boolean)
described_class.new(:boolean_value, localize: true, type: Mongoid::Boolean)
end

let(:value) do
Expand All @@ -463,7 +463,7 @@
context "when the value is true" do

let(:field) do
described_class.new(:boolean_value, localize: true, type: Boolean)
described_class.new(:boolean_value, localize: true, type: Mongoid::Boolean)
end

let(:value) do
Expand Down Expand Up @@ -491,7 +491,7 @@
end

let(:field) do
described_class.new(:boolean_value, localize: true, type: Boolean)
described_class.new(:boolean_value, localize: true, type: Mongoid::Boolean)
end

let(:value) do
Expand All @@ -510,7 +510,7 @@
end

let(:field) do
described_class.new(:boolean_value, localize: true, type: Boolean)
described_class.new(:boolean_value, localize: true, type: Mongoid::Boolean)
end

let(:value) do
Expand Down
8 changes: 4 additions & 4 deletions spec/mongoid/fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@
end

it "converts to Mongoid::Boolean" do
expect(klass.field(:test, type: Boolean).type).to be(Mongoid::Boolean)
expect(klass.field(:test, type: Mongoid::Boolean).type).to be(Mongoid::Boolean)
end
end

Expand Down Expand Up @@ -404,7 +404,7 @@
context "when the options are all standard" do

before do
Band.field :acceptable, type: Boolean
Band.field :acceptable, type: Mongoid::Boolean
end

after do
Expand All @@ -419,7 +419,7 @@
context "when a custom option is provided" do

before do
Band.field :acceptable, type: Boolean, custom: true
Band.field :acceptable, type: Mongoid::Boolean, custom: true
end

it "adds the field to the model" do
Expand Down Expand Up @@ -990,7 +990,7 @@ def testing=(value)
end

before do
Person.field :aliased, as: :alias, type: Boolean, overwrite: true
Person.field :aliased, as: :alias, type: Mongoid::Boolean, overwrite: true
end

it "uses the alias to write the attribute" do
Expand Down

0 comments on commit a080c06

Please sign in to comment.