Sha256: 2a20d9b30b8d8e2851bf75bfcf206fdf46e4370dc8504af6ec15b5b2f0d9af9e

Contents?: true

Size: 1.27 KB

Versions: 26

Compression:

Stored size: 1.27 KB

Contents

class MigrateOldPreferences < ActiveRecord::Migration
  disable_ddl_transaction!

  def up
    migrate_preferences(Spree::Calculator)
    migrate_preferences(Spree::PromotionRule)
    migrate_preferences(Spree::PaymentMethod)
  end

  def down
  end

  private
  def migrate_preferences klass
    klass.reset_column_information
    klass.find_in_batches do |batch|
      ActiveRecord::Base.transaction do
        batch.each do |record|
          keys = record.class.defined_preferences

          # Batch load preferences for this record.
          preferences = Hash[Spree::Preference.where(
            key: keys.map{ |k| cache_key(record, k) }
          ).pluck(:key, :value)]

          # Copy preferences to the record.
          keys.each do |key|
            value = preferences[cache_key(record, key)]
            record.preferences[key] = value unless value.nil?
          end

          # Persist the preferences.
          record.update_column(:preferences, record.preferences)
        end
      end
    end
  end

  def cache_key model, key
    [
      ENV["RAILS_CACHE_ID"],
      class_underscore_cache[model.type],
      key,
      model.id
    ].compact.join("/")
  end

  def class_underscore_cache
    @class_underscore_cache ||= Hash.new{ |h,k| h[k] = k.underscore }
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
solidus_core-1.2.3 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.1.4 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.0.7 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.0.6 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.2.2 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.2.1 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.1.3 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.0.5 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.2.0 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.2.0.rc2 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.0.4 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.1.2 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.2.0.rc1 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.2.0.beta1 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.0.3 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.1.1 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.1.0 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.1.0.pre2 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.1.0.pre1 db/migrate/20140309023735_migrate_old_preferences.rb
solidus_core-1.1.0.beta1 db/migrate/20140309023735_migrate_old_preferences.rb