lib/mongoid/fields.rb in mongoid-8.1.5 vs lib/mongoid/fields.rb in mongoid-8.1.6

- old
+ new

@@ -45,10 +45,15 @@ # BSON classes that are not supported as field types # # @api private INVALID_BSON_CLASSES = [ BSON::Decimal128, BSON::Int32, BSON::Int64 ].freeze + # The suffix for generated translated fields. + # + # @api private + TRANSLATIONS_SFX = '_translations' + 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. @@ -97,12 +102,12 @@ [].tap do |res| ar = name.split('.') ar.each_with_index do |fn, i| key = fn unless klass.fields.key?(fn) || klass.relations.key?(fn) - if tr = fn.match(/(.*)_translations\z/)&.captures&.first - key = tr + if fn.end_with?(TRANSLATIONS_SFX) + key = fn.delete_suffix(TRANSLATIONS_SFX) else key = fn end end @@ -403,16 +408,16 @@ # # @return [ String ] The name of the field as stored in the database. # # @api private def database_field_name(name, relations, aliased_fields, aliased_associations) + return '' unless name.present? + if Mongoid.broken_alias_handling - return nil unless name normalized = name.to_s aliased_fields[normalized] || normalized else - return nil unless name.present? key = name.to_s segment, remaining = key.split('.', 2) # Don't get the alias for the field when a belongs_to association # is not the last item. Therefore, get the alias when one of the @@ -724,15 +729,15 @@ # @param [ String ] meth The name of the method. # # @api private def create_translations_getter(name, meth) generated_methods.module_eval do - re_define_method("#{meth}_translations") do + re_define_method("#{meth}#{TRANSLATIONS_SFX}") do attributes[name] ||= {} attributes[name].with_indifferent_access end - alias_method :"#{meth}_t", :"#{meth}_translations" + alias_method :"#{meth}_t", :"#{meth}#{TRANSLATIONS_SFX}" end end # Create the translation setter method for the provided field. # @@ -744,17 +749,17 @@ # @param [ Field ] field The field. # # @api private def create_translations_setter(name, meth, field) generated_methods.module_eval do - re_define_method("#{meth}_translations=") do |value| + re_define_method("#{meth}#{TRANSLATIONS_SFX}=") do |value| attribute_will_change!(name) value&.transform_values! do |_value| field.type.mongoize(_value) end attributes[name] = value end - alias_method :"#{meth}_t=", :"#{meth}_translations=" + alias_method :"#{meth}_t=", :"#{meth}#{TRANSLATIONS_SFX}=" end end # Include the field methods as a module, so they can be overridden. #