Sha256: 338e3ad096d8070e3fdce1f7b369033d55b91fd4ad84dd63921d66db13757c61

Contents?: true

Size: 1.12 KB

Versions: 25

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Mongoid
  module Persistable

    # Defines behavior for $rename operations.
    module Renamable
      extend ActiveSupport::Concern

      # Rename fields from one value to another via $rename.
      #
      # @example Rename the fields.
      #   document.rename(title: "salutation", name: "nombre")
      #
      # @note This does not work for fields in embeds many associations.
      #
      # @param [ Hash ] renames The rename pairs of old name/new name.
      #
      # @return [ Document ] The document.
      def rename(renames)
        prepare_atomic_operation do |ops|
          process_atomic_operations(renames) do |old_field, new_field|
            new_name = new_field.to_s
            if executing_atomically?
              process_attribute new_name, attributes[old_field]
              process_attribute old_field, nil
            else
              attributes[new_name] = attributes.delete(old_field)
            end
            ops[atomic_attribute_name(old_field)] = atomic_attribute_name(new_name)
          end
          { "$rename" => ops }
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
mongoid-8.1.8 lib/mongoid/persistable/renamable.rb
mongoid-8.1.7 lib/mongoid/persistable/renamable.rb
mongoid-8.1.6 lib/mongoid/persistable/renamable.rb
mongoid-8.0.8 lib/mongoid/persistable/renamable.rb
mongoid-8.1.5 lib/mongoid/persistable/renamable.rb
mongoid-8.1.4 lib/mongoid/persistable/renamable.rb
mongoid-8.0.7 lib/mongoid/persistable/renamable.rb
mongoid-8.1.3 lib/mongoid/persistable/renamable.rb
mongoid-8.1.2 lib/mongoid/persistable/renamable.rb
mongoid-8.0.6 lib/mongoid/persistable/renamable.rb
mongoid-7.5.4 lib/mongoid/persistable/renamable.rb
mongoid-8.1.1 lib/mongoid/persistable/renamable.rb
mongoid-8.0.5 lib/mongoid/persistable/renamable.rb
mongoid-8.1.0 lib/mongoid/persistable/renamable.rb
mongoid-7.5.3 lib/mongoid/persistable/renamable.rb
mongoid-8.0.4 lib/mongoid/persistable/renamable.rb
mongoid-8.0.3 lib/mongoid/persistable/renamable.rb
mongoid-7.5.2 lib/mongoid/persistable/renamable.rb
mongoid-8.0.2 lib/mongoid/persistable/renamable.rb
mongoid-7.5.1 lib/mongoid/persistable/renamable.rb