Fix new preferences migration

Why:

* In a clean environment, running the new preferences migration fails,
  due to a missing file from Spree core being manually required. This
  file has now been missing since ab707cf.

This change addresses the issue by:

* Copying the missing file from Spree Core 1.3.6.beta into the migration.
  This fixes the issue for now, but also means that a migration merge
  and/or rewrite might be in order for the future.
This commit is contained in:
Pedro Costa
2018-10-28 20:20:15 +00:00
parent 8a3f621b42
commit 91e4f99c19

View File

@@ -1,4 +1,37 @@
require 'spree/core/preference_rescue'
# Spree 1.3.6.beta preference rescue implementation, required for the new
# preferences migration, which is broken since commit ab707cf due to the
# absence of this file.
#
# Migration: db/migrate/20120327000645_new_preferences.rb
# Source: https://raw.githubusercontent.com/spree/spree/1-3-stable/core/lib/spree/core/preference_rescue.rb
#
# rubocop:disable all
module Spree
class OldPrefs < ActiveRecord::Base
self.table_name = "spree_preferences"
belongs_to :owner, :polymorphic => true
attr_accessor :owner_klass
end
class PreferenceRescue
def self.try
OldPrefs.where(:key => nil).each do |old_pref|
next unless owner = (old_pref.owner rescue nil)
unless old_pref.owner_type == "Spree::Activator" || old_pref.owner_type == "Spree::Configuration"
begin
old_pref.key = [owner.class.name, old_pref.name, owner.id].join('::').underscore
old_pref.value_type = owner.preference_type(old_pref.name)
puts "Migrating Preference: #{old_pref.key}"
old_pref.save
rescue NoMethodError => ex
puts ex.message
end
end
end
end
end
end
# rubocop:enable all
class NewPreferences < ActiveRecord::Migration
@@ -45,4 +78,4 @@ class NewPreferences < ActiveRecord::Migration
add_column :spree_preferences, :group_id, :integer
add_column :spree_preferences, :group_type, :string
end
end
end