Module: Mongoid::Persistable::Renamable

Extended by:
ActiveSupport::Concern
Included in:
Mongoid::Persistable
Defined in:
build/mongoid-master/lib/mongoid/persistable/renamable.rb

Overview

Defines behavior for $rename operations.

Since:

  • 4.0.0

Instance Method Summary collapse

Instance Method Details

#rename(renames) ⇒ Document

Note:

This does not work for fields in embeds many associations.

Rename fields from one value to another via $rename.

Examples:

Rename the fields.

document.rename(title: "salutation", name: "nombre")

Parameters:

  • renames (Hash)

    The rename pairs of old name/new name.

Returns:

Since:

  • 4.0.0



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'build/mongoid-master/lib/mongoid/persistable/renamable.rb', line 25

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