Skip to content

Commit cc0cd85

Browse files
johnnyshieldsneilshwekyp-mongocomandeo-mongoalexbevi
authored
[READY FOR REVIEW] MONGOID-5509 Merge 8.1-stable (deprecation changes) to master (#5561)
* MONGOID-5334 ensure localized demongoization works with symbol keys (#5416) * MONGOID-5334 ensure localized demongoization works with symbol keys * MONGOID-5334 add type check * MONGOID-5334 change back type check * MONGOID-5437 extract fallback enabling logic into macro (#5428) * MONGOID-5438 initial changes from Johnny's PR * MONGOID-5438 additional changes from johnny's PR * MONGOID-5437 fix tests * MONGOID-5437 fix uniqueness test * MONGOID-5334 enforce Hash type in localized demongoize (#5430) * MONGOID-5334 add type check * MONGOID-5334 change back type check * MONGOID-5334 enforce Hash type in localized demongoize * MONGOID-5456 cast castable values for integer/float/big decimal (#5431) * MONGOID-5456 cast castable values for integer/float/big decimal * MONGOID-5456 assert duration * MONGOID-4403 Support Dirty Tracking changed from/to (#5432) * MONGOID-4403 Support Dirty Tracking changed from/to * Apply suggestions from code review Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-4403 add false example Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-5456 Add documentation for responding to to_* (#5434) * MONGOID-5456 Add documentation for responding to to_* * Apply suggestions from code review Co-authored-by: Oleg Pudeyev <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-5461 Change custom field options example (#5438) * MONGOID-5461 Change custom field options example * Apply suggestions from code review Co-authored-by: Oleg Pudeyev <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-5458 Document fallbacks option on localized fields (#5439) * MONGOID-5460 Update custom field type protocol documentation (#5441) * MONGOID-5457 Document + example of non-String field types in localized fields (#5444) * MONGOID-4698 update warning when overriding criteria methods in scope (#5442) * MONGOID-4698 update warning when overriding criteria methods in scope * Apply suggestions from code review Co-authored-by: Oleg Pudeyev <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-5417 Fix memory leak when call 'with' (#5409) * MONGOID-4528 Add more dirty methods (#5440) Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-4698 Correct warning to point out that klass.band doesnt conflict (#5450) * MONGOID-5226 Allow setting "store_in collection" on document subclass (#5449) * MONGOID-5226 Allow setting "store_in collection" on document subclass * remove comment * MONGOID-5226 add docs and testing * Apply suggestions from code review Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-5226 answer comments Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-5293 put legacy_attributes in 7.5 defaults (#5425) * MONGOID-5293 put legacy_attributes in 7.5 * switch order * MONGOID-5293 fix defaults tests * MONGOID-5438 Implement local override for i18n parameters (#5427) * MONGOID-5438 Implement local override for i18n parameters * MONGOID-5438 get rid of enfore_available_locale changes * MONGOID-5438 get rid of before alls * MONGOID-5438 fix tests caused by rspec precedence * MONGOID-5438 attempt to fix tests when fallbacks disabled * MONGOID-5438 make fallbacks default to return [self] * MONGOID-5438 reset fallbacks * MONGOID-5438 attempt to fix tests * MONGOID-5438 retry_test * MONGOID-5438 undefine fallback methods after fallback tests * MONGOID-5438 add back defaults * MONGOID-5438 use new macros * MONGOID-5438 use before and after suite * MONGOID-5438 update mrss * MONGOID-5438 remove suite checks * MONGOID-5370 Add collection options support (#5452) Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-5474 add readonly! and raise on save/update (#5455) * MONGOID-5474 add readonly! and raise on save/update * MONGOID-5474 flip flag for 9.0 and add default * MONGOID-5474 fix error tests * Update spec/mongoid/config_spec.rb * Apply suggestions from code review Co-authored-by: Oleg Pudeyev <[email protected]> * Update lib/mongoid/config.rb Co-authored-by: Oleg Pudeyev <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-5474/MONGOID-5478 add docs and release notes (#5457) * MONGOID-5474 add docs and release notes * MONGOID-5474 use read-only * MONGOID-5474 add configuration docs * MOGOID-5474 switch order of flag docs * add MONGOID-5477 use case * MONGOID-5477 / MONGOID-5474 / MONGOID-5478 add user test (#5459) * MONGOID-5474 add user test * MONGOID-5474 change to read-only * MOGOID-5474 switch order of flag docs * MONGOID-5476 update 8.1 installation version (#5464) * MONGOID-5474 flip the feature flag in 8.1 (#5456) * MONGOID-5474 flip the feature flag in 8.1 * MONGOID-5474 flip flag in configuration * MONGOID-5474 update config test * MONGOID-5227 Add test and adjust docs for dotted attribute assignment in default scope (#5474) * MONGOID-5227 Add test and adjust docs for dotted attribute assignment in default scope * MONGOID-5227 adjust docs * MONGOID-5227 clarify the docs and add tests * Update docs/reference/queries.txt Co-authored-by: Oleg Pudeyev <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-5493: Update Gem::Specification team info (#5476) * MONGOID-5312 Document attributes method in reference (#5477) * MONGOID-5312 Document attributes method in reference * Apply suggestions from code review Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-5312 update languagw Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-5082 Create contributing guidelines (#5472) * MONGOID-5082 Create contributing guidelines * MONGOID-5082 propose test coverage * MONGOID-5082 whitespace * MONGOID-5082 update language * add intro * MONGOID-5445 Add load_async to criteria (#5454) Co-authored-by: Neil Shweky <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-5291 Clarify whether :touch option is applicable for all updates (#5481) * MONGOID-5261 Use config_override when a test requires a certain configuration option value (#5479) * MONGOID-5261 Use config_override when a test requires a certain configuration option value * fix syntax error * MONGOID-5261 a little more cleanup * MONGOID-5261 more cleanup * more clean up * MONGOID-5261 add persistence context override * MONGOID-5261 add comment * MONGOID-5100 allow selector arguments for .exists? (#5466) * MONGOID-5100 allow selector arguments for .exists? * MONGOID-5100 add examples to the docstrings * MONGOID-5100 add docs and release notes * Empty-Commit * MONGOID-5100 allow false to be passed * MONGOID-5100 update docs and docstrings * MONGOID-5100 remove false as a param * MONGOID-5100 put back nil/false * MONGOID-5100 fix memory tests and fix limit == 0 * MONGOID-5262 Implement local time zone override for tests (#5480) * MONGOID-5506 Update 8.x docs to document unscoped behavior changing in 9.0 (#5483) * MONGOID-5506 Update 8.x docs to document unscoped behavior changing in 9.0 * Update lib/mongoid/scopable.rb * RUBY-2942: Update documentation links (#5482) * MONGOID-5363 Change #upsert to perform updating upsert rather than replacing upsert (#5492) * MONGOID-5363 add :replace option to upsert method * MONGOID-5363 add docs, release notes for 8.1/9.0 * MONGOID-5363 switch default in 8.1 (#5493) * MONGOID-5363 update docstrings, flag default, and tests for 8.1 * specify default in mongoid 9 * MONGOID-5331 Document hash assignment to associations (#5494) * MONGOID-5331 change buildable docs * MONGOID-5331 add proxy tests and fix embedded_in proxy * MONGOID-5331 update models * Update spec/mongoid/association/embedded/embedded_in/proxy_spec.rb * MONGOID-5331 update doc strings in batchable * MONGOID-5331 add docs * MONGOID-5331 add docs/release notes * Apply suggestions from code review * Update spec/mongoid/association/embedded/embedded_in/proxy_spec.rb * RUBY-3097 add/use monotonic time for deadlines (#5491) * RUBY-3097 add/use monotonic time for deadlines * remove license and fix tests * MONGOID-3834 Document association availability in custom getters and setters (#5503) * MONGOID-3834 Document association availability in custom getters and setters * add callbacks note * MONGOID-5496: Update Getting Started (Rails) tutorial for Rails 7 (#5509) * Clone Rails Tutorial and create variation for Rails 7 * Update Existing Application docs for Rails 7 * MONGOID-5532: Fix path to comments partial (#5511) * MONGOID-5533: Unnecessary edits to Comment model in Rails Getting Started (#5512) * Add note about workaround for MONGOID-4885 in Rails 6 docs * Fix whitespace * MONGOID-5521 migrate Mongoid docs to snooty (#5513) * fix code blocks * MONGOID-5521 fix all parse errors * build docs with github action * Revert "build docs with github action" This reverts commit 9ffda4493f774843f360517e27538ede6dd9020f. * MONGOID-5496: Update Rails 7 Tutorial (#5515) * Remove `--skip-bundle` from _Optionally Skip Tests_ section * MONGOID-5518: Add Puma dependency to Sinatra tutorial (#5516) * MONGOID-5453 Add .none_of query method (#5524) * "none_of" query method * update docs and release notes * fix underline for header * MONGOID-5539: Fix example in aggregation reference documentation (#5536) * Update aggregation.txt * Update aggregation.txt * MONGOID-5228 disallow _id to be updated on persisted documents (ported to 8.1-stable) (#5545) * port #5542 to 8.1-stable * test tweak * update 8.1 release notes to mention immutable_ids * MONGOID-5490: Deprecate :use_activesupport_time_zone (#5488) * Deprecate the use_activesupport_time_zone_deprecated config option. * Typo * Update config.rb * Update mongoid-8.1.txt --------- Co-authored-by: shields <[email protected]> * MONGOID-5509: Deprecate all feature flags which were introduced in Mongoid 7.x (#5489) * Deprecate the use_activesupport_time_zone_deprecated config option. * Typo * Mongoid 8.1 should deprecate all feature flags which were introduced in the 7.x series, with intent to remove them in Mongoid 9.0. --------- Co-authored-by: shields <[email protected]> * Remove :use_activesupport_time_zone option + improve readme around timezone usage given the change * Remove deprecated :broken_aggregables config option * Remove deprecated :broken_alias_handling config option * Remove deprecated :broken_and config option * Remove deprecated :broken_scoping config option * Remove deprecated :broken_updates config option * Remove deprecated :compare_time_by_ms config option * Remove deprecated :legacy_attributes config option * Remove deprecated :legacy_pluck_distinct config option * Remove deprecated :legacy_triple_equals config option * Remove deprecated :object_id_as_json_oid config option * Remove deprecated :overwrite_chained_operators config option * Drop support for config.load_defaults versions "7.3", "7.4", "7.5", as their options are no longer available. * Fix broken spec in Mongoid 8.1 --------- Co-authored-by: Neil Shweky <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]> Co-authored-by: Dmitry Rybakov <[email protected]> Co-authored-by: Alex Bevilacqua <[email protected]> Co-authored-by: Jamis Buck <[email protected]> Co-authored-by: Jamis Buck <[email protected]> Co-authored-by: shields <[email protected]>
1 parent 1c5e498 commit cc0cd85

File tree

6 files changed

+152
-228
lines changed

6 files changed

+152
-228
lines changed

source/reference/configuration.txt

Lines changed: 87 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -286,88 +286,6 @@ for details on driver options.
286286
# error. (default: true)
287287
belongs_to_required_by_default: true
288288

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-
371289
# Set the global discriminator key. (default: "_type")
372290
discriminator_key: "_type"
373291

@@ -389,21 +307,6 @@ for details on driver options.
389307
# to parent contexts by default. (default: false)
390308
join_contexts: false
391309

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-
407310
# When this flag is false, a document will become read-only only once the
408311
# #readonly! method is called, and an error will be raised on attempting
409312
# to save or update such documents, instead of just on delete. When this
@@ -412,17 +315,8 @@ for details on driver options.
412315
# deletable/destroyable, but will be savable/updatable.
413316
# When this feature flag is turned on, the read-only state will be reset on
414317
# 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.
424318
# (default: false)
425-
#legacy_triple_equals: true
319+
#legacy_readonly: true
426320

427321
# Set the Mongoid and Ruby driver log levels when Mongoid is not using
428322
# Ruby on Rails logger instance. (default: :info)
@@ -432,18 +326,6 @@ for details on driver options.
432326
# as a BSON::Decimal128 instead of a string. (default: true)
433327
#map_big_decimal_to_decimal128: true
434328

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-
447329
# Preload all models in development, needed when models use
448330
# inheritance. (default: false)
449331
preload_models: false
@@ -456,11 +338,6 @@ for details on driver options.
456338
# existing method. (default: false)
457339
scope_overwrite_exception: false
458340

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-
464341
# Return stored times as UTC. See the time zone section below for
465342
# further information. Most applications should not use this option.
466343
# (default: false)
@@ -469,6 +346,7 @@ for details on driver options.
469346
# (Deprecated) In MongoDB 4.0 and earlier, set whether to create
470347
# indexes in the background by default. (default: false)
471348
background_indexing: false
349+
472350
# Configure driver-specific options. (optional)
473351
driver_options:
474352
# 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
510388
the configuration options from the previous version of Mongoid. Then,
511389
one-by-one, you can change the feature flags for the new version, and test that
512390
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``:
518397

519398
.. code:: ruby
520399

521400
Mongoid.configure do |config|
522401
config.load_defaults 7.5
523402
end
524403

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:
529408

530409
.. code:: ruby
531410

@@ -535,8 +414,8 @@ behavior:
535414
# config.map_big_decimal_to_decimal128 = true
536415
end
537416

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
540419
``legacy_attributes`` flag turned off, the ``map_big_decimal_to_decimal128``
541420
setting can be uncommented. Once that functionality is verified as well, both
542421
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:
726605
Time Zones
727606
==========
728607

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.
733612

734613
While a thorough treatment of time zones in Ruby is outside the scope
735614
of this tutorial, the easiest and most reliable way of achieving correct
@@ -741,7 +620,7 @@ time zone handling is as follows:
741620

742621
cp /usr/share/zoneinfo/UTC /etc/localtime
743622

744-
2. Set ActiveSupport's time zone to UTC:
623+
2. Set your application default time zone to UTC:
745624

746625
.. code-block:: ruby
747626

@@ -753,69 +632,74 @@ time zone handling is as follows:
753632
# If not using Rails:
754633
Time.zone = 'UTC'
755634

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+
819703

820704
Configuring ``SSLContext``
821705
==========================

source/reference/crud.txt

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,8 @@ operations with examples.
3939

4040
* - ``Model#attributes``
4141

42-
*Returns a hash of the document's attributes with its values in
43-
mongoized form (i.e. the way they are stored in the db).*
44-
45-
*In Mongoid 8.0 by default, and Mongoid 7.5 with the*
46-
``legacy_attributes`` *flag set to false, the object returned from this
47-
method will have class* ``Hash``, *and have string keys. In Mongoid 7.5
48-
by default, and in Mongoid 8.0 with the* ``legacy_attributes`` *flag set
49-
to true, the object returned from this method will be a*
50-
``BSON::Document`` *(and thus, indifferently accessible) if the document
51-
has been retrieved from the database, and a* ``Hash`` *with string keys
52-
if it is a new document.*
42+
*Returns the document's attributes as a ``Hash`` with string keys, and
43+
its values in Mongoized form (i.e. the way they are stored in the db).*
5344

5445
*The attributes hash also contains the attributes of all embedded
5546
documents, as well as their embedded documents, etc. If an embedded

0 commit comments

Comments
 (0)