@@ -286,88 +286,6 @@ for details on driver options.
286
286
# error. (default: true)
287
287
belongs_to_required_by_default: true
288
288
289
- # Maintain broken behavior of sum over empty result sets for backwards
290
- # compatibility. When calculating a sum on a field with a null context,
291
- # for example:
292
- #
293
- # Product.none.sum(:price)
294
- #
295
- # ... return field name (`:price') instead of 0.
296
- #
297
- # When calculating a sum via a database query with an empty result set,
298
- # for example:
299
- #
300
- # Product.where(impossible_condition: true).sum(:price)
301
- #
302
- # ... return nil instead of 0.
303
- # (default: false)
304
- #broken_aggregables: true
305
-
306
- # Ignore aliased fields in embedded documents when performing pluck and
307
- # distinct operations, for backwards compatibility.
308
- # (default: false)
309
- #broken_alias_handling: true
310
-
311
- # Maintain broken `and' method behavior that existed in Mongoid 7.3
312
- # and earlier for backwards compatibility: in some situations, conditions
313
- # already present in a Criteria object would be replaced by newer
314
- # conditions when `and' method is used instead of the new conditions
315
- # being added to the existing conditions. This would happen when using
316
- # the same operator on the same field multiple times. For example:
317
- #
318
- # Band.where(id: 1).and({year: {'$in' => [2020]}}, {year: {'$in' => [2021]}}).where(id: 2)
319
- #
320
- # yields the following criteria:
321
- #
322
- # <Mongoid::Criteria
323
- # selector: {"_id"=>1, "year"=>{"$in"=>[2020]}, "$and"=>[{"_id"=>2}]}
324
- # options: {}
325
- # class: Band
326
- # embedded: false>
327
- #
328
- # This is obviously incorrect as the {"$in"=>[2021]} clause is lost.
329
- # Notice that the clause is only lost when both clauses are added using
330
- # the #and method.
331
- # (default: false)
332
- #broken_and: true
333
-
334
- # When exiting a nested `with_scope' block, set the current scope to
335
- # nil instead of the parent scope for backwards compatibility.
336
- # (default: false)
337
- #broken_scoping: true
338
-
339
- # Maintain broken update behavior in some cases for backwards
340
- # compatibility.
341
- #
342
- # In Mongoid 7.3 and earlier, when assigning a value to an embedded
343
- # document, then setting it to nil, then assigning the original value
344
- # to it again, the second update would not work and the value for the
345
- # embedded document would remain nil. Take this case:
346
- #
347
- # canvas.palette = palette
348
- # canvas.palette = nil
349
- # canvas.palette = palette
350
- #
351
- # ... where canvas embeds_one palette.
352
- #
353
- # In Mongoid 7.3 and earlier, canvas.palette would be nil when we would
354
- # expect it to be palette. Set this option to true to keep this behavior,
355
- # set the option to false to perform the second update correctly.
356
- # (default: false)
357
- #broken_updates: true
358
-
359
- # Time objects in Ruby have nanosecond precision, whereas MongoDB server
360
- # can only store times with millisecond precision. Set this option to
361
- # true to truncate times to millisecond precision when performing
362
- # queries on already loaded embedded associations (this is also called
363
- # "embedded matching" and is done completely in Ruby), to obtain the
364
- # same query results when performing time comparisons regardless of
365
- # which documents are being queried. Setting this option to false will
366
- # produce different results for queries on embedded associations that
367
- # are already loaded into memory vs queries on unloaded associations and
368
- # top-level models. (default: true)
369
- #compare_time_by_ms: false
370
-
371
289
# Set the global discriminator key. (default: "_type")
372
290
discriminator_key: "_type"
373
291
@@ -389,21 +307,6 @@ for details on driver options.
389
307
# to parent contexts by default. (default: false)
390
308
join_contexts: false
391
309
392
- # When this flag is true, the attributes method on a document will return
393
- # a BSON::Document when that document is retrieved from the database, and
394
- # a Hash otherwise. When this flag is false, the attributes method will
395
- # always return a Hash. (default: false)
396
- #legacy_attributes: true
397
-
398
- # Maintain legacy behavior of pluck and distinct, which does not demongoize
399
- # values on returning them. Setting this option to false will cause
400
- # pluck and distinct to return demongoized values. Setting this option to
401
- # false will also allow retrieving *_translations fields from pluck and
402
- # distinct and will return embedded values themselves (i.e. without
403
- # putting them in a hash).
404
- # (default: false)
405
- #legacy_pluck_distinct: true
406
-
407
310
# When this flag is false, a document will become read-only only once the
408
311
# #readonly! method is called, and an error will be raised on attempting
409
312
# to save or update such documents, instead of just on delete. When this
@@ -412,17 +315,8 @@ for details on driver options.
412
315
# deletable/destroyable, but will be savable/updatable.
413
316
# When this feature flag is turned on, the read-only state will be reset on
414
317
# reload, but when it is turned off, it won't be.
415
- # (default: true)
416
- legacy_readonly: false
417
-
418
- # Maintain legacy behavior of === on Mongoid document classes, which
419
- # returns true in a number of cases where Ruby's === implementation would
420
- # return false. Note that the behavior of === on Mongoid document
421
- # instances differs from both the behavior of === on document classes
422
- # and from Ruby's behavior of === on simple object instances regardless
423
- # of the value of this option.
424
318
# (default: false)
425
- #legacy_triple_equals : true
319
+ #legacy_readonly : true
426
320
427
321
# Set the Mongoid and Ruby driver log levels when Mongoid is not using
428
322
# Ruby on Rails logger instance. (default: :info)
@@ -432,18 +326,6 @@ for details on driver options.
432
326
# as a BSON::Decimal128 instead of a string. (default: true)
433
327
#map_big_decimal_to_decimal128: true
434
328
435
- # Force ``BSON::ObjectId#as_json`` method to return the hash
436
- # { "$oid" => id.to_s }. When this option is false, and bson-ruby 5
437
- # is used, the return value will be the hexadecimal ObjectId string only.
438
- # (default: false)
439
- #object_id_as_json_oid: true
440
-
441
- # When chaining the same operators that use the same field, setting this
442
- # feature flag to false will cause those operators to be combined using an
443
- # and. Setting this feature flag to true will cause the later chained
444
- # operators to overwrite the earlier ones. (default: false)
445
- #overwrite_chained_operators: false
446
-
447
329
# Preload all models in development, needed when models use
448
330
# inheritance. (default: false)
449
331
preload_models: false
@@ -456,11 +338,6 @@ for details on driver options.
456
338
# existing method. (default: false)
457
339
scope_overwrite_exception: false
458
340
459
- # Use ActiveSupport's time zone in time operations instead of
460
- # the Ruby default time zone. See the time zone section below for
461
- # further information. (default: true)
462
- use_activesupport_time_zone: true
463
-
464
341
# Return stored times as UTC. See the time zone section below for
465
342
# further information. Most applications should not use this option.
466
343
# (default: false)
@@ -469,6 +346,7 @@ for details on driver options.
469
346
# (Deprecated) In MongoDB 4.0 and earlier, set whether to create
470
347
# indexes in the background by default. (default: false)
471
348
background_indexing: false
349
+
472
350
# Configure driver-specific options. (optional)
473
351
driver_options:
474
352
# When this flag is turned off, inline options will be correctly
@@ -510,22 +388,23 @@ This way, when upgrading to a new version of Mongoid, your code will run with
510
388
the configuration options from the previous version of Mongoid. Then,
511
389
one-by-one, you can change the feature flags for the new version, and test that
512
390
your code still acts as expected. Once all of the new feature flags have been
513
- accounted for, the call to ``load_defaults`` can be changed to take in the *new*
514
- version, and all of the changed feature flags can be removed. For example, say
515
- we're upgrading from 7.5 to 8.0. Between these two versions, only two feature
516
- flags were added: ``legacy_attributes`` and ``map_big_decimal_to_decimal128``.
517
- Before upgrading to Mongoid 8, the following line can be added:
391
+ accounted for, the call to ``load_defaults`` may be changed to take in the *new*
392
+ version, and all of the changed feature flags may be removed.
393
+
394
+ For example, suppose we're upgrading from 7.5 to 8.0. Between these two versions,
395
+ two feature flags were added: ``legacy_attributes`` and ``map_big_decimal_to_decimal128``.
396
+ Before upgrading to Mongoid 8, add the following to your ``Mongoid::Config``:
518
397
519
398
.. code:: ruby
520
399
521
400
Mongoid.configure do |config|
522
401
config.load_defaults 7.5
523
402
end
524
403
525
- Now, after upgrading, those two feature flags will default to their 7.5
526
- functionality ( ``legacy_attributes: true, map_big_decimal_to_decimal128: false``).
527
- Now you can set these feature flags one-by-one and flip them to their 8.0
528
- behavior:
404
+ After upgrading to Mongoid 8.0 in your ``Gemfile``, any feature flags will
405
+ remain set to their 7.5 default behavior: ``legacy_attributes: true,
406
+ map_big_decimal_to_decimal128: false``. You may then flip these feature flags
407
+ one-by-one to their 8.0 behavior:
529
408
530
409
.. code:: ruby
531
410
@@ -535,8 +414,8 @@ behavior:
535
414
# config.map_big_decimal_to_decimal128 = true
536
415
end
537
416
538
- It is advised to do these one at a time, so I have left the second flag
539
- commented out. After verifying your code works as expected with the
417
+ We recommend do these one at a time, so in the example above we leave the
418
+ second flag commented out. After verifying your code works as expected with the
540
419
``legacy_attributes`` flag turned off, the ``map_big_decimal_to_decimal128``
541
420
setting can be uncommented. Once that functionality is verified as well, both
542
421
of those lines can be removed and the ``load_defaults`` replaced with:
@@ -726,10 +605,10 @@ To set the driver logger to be the same as the Mongoid logger:
726
605
Time Zones
727
606
==========
728
607
729
- Ruby has limited time zone support in the standard library. ActiveSupport
730
- (which Mongoid depends on) offers more comprehensive time zone support.
731
- Importantly, Ruby and ActiveSupport may be configured with different default
732
- time zones .
608
+ Mongoid uses ActiveSupport's time zone functionality, which is far
609
+ more robust than Ruby's standard library. Importantly, ActiveSupport
610
+ allows configuration of ``Time.zone``, a thread-global variable which
611
+ provides context for working with date and time values .
733
612
734
613
While a thorough treatment of time zones in Ruby is outside the scope
735
614
of this tutorial, the easiest and most reliable way of achieving correct
@@ -741,7 +620,7 @@ time zone handling is as follows:
741
620
742
621
cp /usr/share/zoneinfo/UTC /etc/localtime
743
622
744
- 2. Set ActiveSupport's time zone to UTC:
623
+ 2. Set your application default time zone to UTC:
745
624
746
625
.. code-block:: ruby
747
626
@@ -753,69 +632,74 @@ time zone handling is as follows:
753
632
# If not using Rails:
754
633
Time.zone = 'UTC'
755
634
756
- 3. Store and persist all times in UTC. Perform all calculations on times
757
- in UTC.
758
- 4. When working with user input in local time, convert such user input to UTC
759
- times as soon as possible, and then work with the UTC times.
760
- 5. When rendering or otherwise presenting times, convert them to local time
761
- after performing all calculations, when actually rendering.
762
- 6. Date to time (for example, the time when a particular day starts or ends)
763
- conversions are a common source of errors. Such conversions should generally
764
- be performed while explicitly specifying the time zone in which the date
765
- is understood to be.
766
-
767
- Applications using Mongoid should generally configure ActiveSupport's
768
- time zone as described above, and then use ``Time.zone`` rather than ``Time``
769
- (for example, ``Time.zone.now`` instead of ``Time.now``) to invoke the
770
- ActiveSupport time zone machinery. This also helps achieve correct results
771
- when the system time zone is not UTC, as is common in development environments.
772
-
773
- Note that MongoDB stores all times in UTC without time zone information.
774
-
775
- Mongoid offers the following time zone-related configuration options:
776
-
777
- - ``use_activesupport_time_zone``: If true, prefer to work with times using
778
- ``ActiveSupport::TimeWithZone``. Values in fields of type ``Time``
779
- will be returned as instances of ``ActiveSupport::TimeWithZone``.
780
- When parsing times without time zone information (such as when
781
- mongoizing strings or arrays to time), assume the times are specified
782
- in ActiveSupport's time zone. This is the default.
783
-
784
- If false, prefer to work with times using Ruby standard library ``Time`` class.
785
- Values in fields of type ``Time`` will be returned as ``Time`` instances.
786
- When parsing times without time zone information, assume the times
787
- are specified in the Ruby time zone.
788
-
789
- Note that the ``use_activesupport_time_zone`` setting does not affect
790
- fields of types ``Date`` or ``DateTime``, which use ``Date`` and
791
- ``DateTime`` classes for their values, respectively.
792
-
793
- Also note that Mongoid may still utilize both ``Time`` and
794
- ``ActiveSupport::TimeWithZone`` classes internally, as appropriate,
795
- regardless of the ``use_activesupport_time_zone`` setting.
796
- - ``use_utc``:
797
- If true, times stored in MongoDB will be returned in UTC.
798
- If false, times stored in MongoDB will be returned in local time
799
- (as instances of either ``Time`` or ``ActiveSupport::TimeWithZone``,
800
- respectively in the Ruby default time zone or the ActiveSupport time zone,
801
- based on the value of ``use_activesupport_time_zone`` setting).
802
- The default is false.
803
-
804
- The ``use_utc`` setting does not affect how times are parsed - parsing
805
- is always done in local time when the input being parsed does not
806
- include time zone information. To parse dates in UTC, set the
807
- system/Ruby or ActiveSupport time zone to UTC (as mentioned above,
808
- setting all three to UTC leads to the fewest headaches).
809
-
810
- Setting ``use_activesupport_time_zone`` to true and ``Time.zone`` to
811
- UTC (and using ActiveSupport time machinery for all time-related
812
- operations) is recommended over setting ``use_utc`` to true.
813
-
814
- Note that ``use_activesupport_time_zone`` and ``use_utc`` options do not
815
- throw away time zone information when it is available. For example, a Time
816
- instance does have an associated time zone, and this time zone will be used
817
- even if it is different from ActiveSupport's configured time zone when
818
- ``use_activesupport_time_zone`` is true.
635
+ 3. In each controller and job class, set the appropriate time zone in a
636
+ ``before_filter`` at the earliest possible stage. As an example,
637
+ if each user of your application can set their own time zone,
638
+ you may wish to do:
639
+
640
+ .. code-block:: ruby
641
+
642
+ class ApplicationController < ActionController::Base
643
+ before_filter :fetch_user,
644
+ :set_time_zone
645
+
646
+ def set_time_zone
647
+ Time.zone = @user.time_zone
648
+ end
649
+ end
650
+
651
+ 4. From here, you may work with times naturally in the local time zone.
652
+ For example, in a view:
653
+
654
+ .. code-block:: ruby
655
+
656
+ Turned into a pumpkin after <%= cinderella.updated_at.seconds_after_midnight %> seconds!
657
+
658
+ 5. Use ActiveSupport methods instead of the Ruby standard library.
659
+
660
+ - ``Time.zone.now`` or ``Time.current` instead of ``Time.now``
661
+ - ``Date.current`` instead of ``Date.today``
662
+
663
+ Critically, note that the latter Ruby standard library methods reference
664
+ your system time zone (e.g. UTC) and not the value of ``Time.zone``.
665
+ As it is very easy to mistake these similarly named methods, we recommend to
666
+ use `Rubocop's Rails/TimeZone cop
667
+ <https://docs.rubocop.org/rubocop-rails/cops_rails.html#railstimezone>`_ in your CI.
668
+
669
+ Setting time zone on data loaded from MongoDB
670
+ ---------------------------------------------
671
+
672
+ MongoDB stores all times in UTC without time zone information.
673
+ Mongoid models load and returns time values as instances of
674
+ ``ActiveSupport::TimeWithZone``. You may set the ``use_utc`` option
675
+ to control how Mongoid sets the time zone when loading from the database:
676
+
677
+ - If false (default), Mongoid will use ``Time.zone`` to set the time
678
+ zone of time values are loaded from database.
679
+ - If true, Mongoid will always set the time zone as UTC on loaded
680
+ time values.
681
+
682
+ ``use_utc`` only affects how data is loaded, and does not affect
683
+ how data is persisted. For example, if you assign a ``Time`` or
684
+ ``ActiveSupport::TimeWithZone`` instance to a time field, the time
685
+ zone information of the assigned instance always will be used
686
+ irrespective of the ``use_utc`` setting. Alternatively, if you
687
+ assign a string value to a time field, any time zone information
688
+ in the string will be used if present. If the string does not include
689
+ time zone information it will be parsed according to ``Time.zone``.
690
+ To illustrate:
691
+
692
+ .. code-block:: ruby
693
+
694
+ Time.use_zone("Asia/Kolkata") do
695
+
696
+ # String does not include time zone, so "Asia/Kolkata" will be used
697
+ ghandi.born_at = "1869-10-02 7:10 PM"
698
+
699
+ # Time zone in string (-0600) will be used
700
+ amelia.born_at = "1897-07-24 11:30 -0600"
701
+ end
702
+
819
703
820
704
Configuring ``SSLContext``
821
705
==========================
0 commit comments