lib/mongoid/fields.rb in mongoid-7.2.6 vs lib/mongoid/fields.rb in mongoid-7.3.0

- old
+ new

@@ -11,10 +11,11 @@ # This module defines behavior for fields. module Fields extend ActiveSupport::Concern StringifiedSymbol = Mongoid::StringifiedSymbol + Boolean = Mongoid::Boolean # For fields defined with symbols use the correct class. # # @since 4.0.0 TYPE_MAPPINGS = { @@ -35,15 +36,53 @@ stringified_symbol: StringifiedSymbol, symbol: Symbol, time: Time }.with_indifferent_access - # Constant for all names of the id field in a document. + # Constant for all names of the _id field in a document. # - # @since 5.0.0 - IDS = [ :_id, :id, '_id', 'id' ].freeze + # This does not include aliases of _id field. + # + # @api private + IDS = [ :_id, '_id', ].freeze + module ClassMethods + # Returns the list of id fields for this model class, as both strings + # and symbols. + # + # @return [ Array<Symbol | String> ] List of id fields. + # + # @api private + def id_fields + IDS.dup.tap do |id_fields| + aliased_fields.each do |k, v| + if v == '_id' + id_fields << k.to_sym + id_fields << k + end + end + end + end + + # Extracts the id field from the specified attributes hash based on + # aliases defined in this class. + # + # @param [ Hash ] attributes The attributes to inspect. + # + # @return [ Object ] The id value. + # + # @api private + def extract_id_field(attributes) + id_fields.each do |k| + if v = attributes[k] + return v + end + end + nil + end + end + included do class_attribute :aliased_fields class_attribute :localized_fields class_attribute :fields class_attribute :pre_processed_defaults @@ -60,11 +99,10 @@ default: ->{ BSON::ObjectId.new }, pre_processed: true, type: BSON::ObjectId ) - alias :id :_id - alias :id= :_id= + alias_attribute(:id, :_id) end # Apply all default values to the document which are not procs. # # @example Apply all the non-proc defaults.