Module: ActiveMigration::KeyMapper

Defined in:
lib/active_migration/key_mapper.rb

Overview

The keymapper allows you to serialize and deserialize primary keys. This is useful in maintaining foreign key relationships, if you choose not to migrate the primary key.

To serialize the keys you simply:

write_key_map true

To deserialize the key for a foreign key you can specifiy the keymap as the third element:

map [['product_id','product_id', :product_migration]]
              ]

If you’d like to access the key from a callback or anywhere outside the mappings array, you can use:

mapped_key(:products, old_key)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_migration/key_mapper.rb', line 28

def self.included(base)
  base.class_eval do
    alias_method_chain :run, :key_mapping
    alias_method_chain :migrate_field, :key_mapping
    alias_method_chain :save, :key_mapping
    class << self
      attr_accessor :map_keys

      # Tells ActiveMigration to serialize the primary key of the legacy model.
      #
      #   write_key_map true
      #
      def write_key_map(map_keys)
        @map_keys = map_keys
      end
      alias map_keys= write_key_map
    end
  end
end

Instance Method Details

#run_with_key_mappingObject

:nodoc:



48
49
50
51
# File 'lib/active_migration/key_mapper.rb', line 48

def run_with_key_mapping #:nodoc:
  run_without_key_mapping
  serialize_key_map(self.storage_path, self.class.to_s.demodulize.underscore) if self.class.map_keys
end