mongoid icon indicating copy to clipboard operation
mongoid copied to clipboard

MONGOID-5210 Prefer field :type as Symbol + custom field type symbols

Open johnnyshields opened this issue 3 years ago • 3 comments

This is blocked until after https://github.com/mongodb/mongoid/pull/5269 is merged.


Fixes MONGOID-5210

Prior to this PR, Mongoid already supports defining field :type as a Symbol rather than a Class:

  field :first_name, type: :string  # works today, even without this PR!

Generally speaking, it is much better to to use Symbol than Class, because Class uses autoloading etc.

This PR adds the following:

  • Main documentation is updated to prefer using Symbol rather than Class. I haven't done this change in 100% of doc files; I plan to do that after this PR is merged in a separate PR.
  • Usage of Class is deprecated and now logs a warning. Usage of Class is intended to be removed in Mongoid 8.0.
  • It is now possible to define custom symbols using the following:
  Mongoid::Fields.configure do
    type :point, Point
  end

Further Plans

In Mongoid 8.1+, once everything is symbols we should consider extracting out the mongoization logic into separate modules and not inserting methods into Ruby Kernel classes. For example, String.mongoize should be Mongoizers::String.mongoize(string).

johnnyshields avatar Jan 01 '22 11:01 johnnyshields

@p-mongo tests are now written. This is ready for final review and merge.

johnnyshields avatar Apr 05 '22 13:04 johnnyshields

@p-mongo I will be happy to split out that code soon. However, please see my reply in https://jira.mongodb.org/browse/MONGOID-5210, in the future, I still believe there is strong justification to switch to symbols.

johnnyshields avatar May 09 '22 18:05 johnnyshields

@p-mongo PR split out, please see: https://github.com/mongodb/mongoid/pull/5269

johnnyshields avatar May 09 '22 19:05 johnnyshields