def refresh_metadata_with_custom_fields(name)
      return if !self.persisted? || self.send(:"#{name}_custom_fields").blank? # do not generate a klass without all the information

      old_metadata = self.send(name).relation_metadata

      # puts "old_metadata = #{old_metadata.klass.inspect} / #{old_metadata.object_id.inspect}" # DEBUG

      # puts "[CustomFields] refresh_metadata_with_custom_fields, #{name.inspect}, self = #{self.inspect}"

      self.send(name).__metadata = old_metadata.clone.tap do |metadata|
        # Rails.logger.debug "[CustomFields] refresh_metadata_with_custom_fields #{metadata.klass}" if defined?(Rails) # DEBUG

        # backup the current klass
        metadata[:original_klass] ||= metadata.klass

        metadata.instance_variable_set(:@klass, self.klass_with_custom_fields(name))
      end
      set_attribute_localization(name)
      # puts "new_metadata = #{self.send(name).metadata.klass.inspect} / #{self.send(name).metadata.object_id.inspect}" # DEBUG
    end
def refresh_metadata_with_custom_fields(name)
      return if !self.persisted? || self.send(:"#{name}_custom_fields").blank? # do not generate a klass without all the information

      old_metadata = self.send(name).relation_metadata

      # puts "old_metadata = #{old_metadata.klass.inspect} / #{old_metadata.object_id.inspect}" # DEBUG

      # puts "[CustomFields] refresh_metadata_with_custom_fields, #{name.inspect}, self = #{self.inspect}"

      self.send(name).__metadata = old_metadata.clone.tap do |metadata|
        # Rails.logger.debug "[CustomFields] refresh_metadata_with_custom_fields #{metadata.klass}" if defined?(Rails) # DEBUG

        # backup the current klass
        metadata[:original_klass] ||= metadata.klass

        metadata.instance_variable_set(:@klass, self.klass_with_custom_fields(name))
      end
      set_attribute_localization(name)
      # puts "new_metadata = #{self.send(name).metadata.klass.inspect} / #{self.send(name).metadata.object_id.inspect}" # DEBUG
    end
Change the metadata of a relation enhanced by the custom fields.
 In Mongoid, all the instances of a same document share the same metadata objects.

 @param [ String, Symbol ] name The name of the relation.