Skip to content

Commit

Permalink
Extraced PreferenceRescue to it's own class, so extensions can reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
BDQ authored and Trung Lê committed Feb 19, 2012
1 parent 7d4c6a6 commit 4e6cf4d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
26 changes: 13 additions & 13 deletions core/db/migrate/20120119024710_new_preferences.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'spree/core/preference_rescue'

class NewPreferences < ActiveRecord::Migration

class OldPrefs < ActiveRecord::Base
Expand All @@ -21,21 +23,19 @@ def up
change_column :spree_preferences, :group_type, :string, :null => true

spree_config = Spree::AppConfiguration.new
Spree::Preference.where(:owner_type => 'Spree::Configuration').each do |preference|
preference.key = spree_config.preference_cache_key(preference.name)
preference.value_type = spree_config.preference_type(preference.name)
preference.save(:validate => false)
end

OldPrefs.all.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"
old_pref.key = [owner.class.name, old_pref.name, owner.id].join('::').underscore
old_pref.value_type = owner.preference_type(old_pref.name)
say "Migrating Preference: #{old_pref.key}"
old_pref.save
end
cfgs = execute("select id, type from spree_configurations").to_a
execute("select id, owner_id, name from spree_preferences where owner_type = 'Spree::Configuration'").each do |pref|
configuration = cfgs.detect { |c| c[0].to_s == pref[1].to_s }

value_type = configuration[1].constantize.new.send "preferred_#{pref[2]}_type" rescue 'string'

execute "UPDATE spree_preferences set `key` = '#{configuration[1].underscore}/#{pref[2]}', `value_type` = '#{value_type}' where id = #{pref[0]}" rescue nil
end

Spree::PreferenceRescue.try

OldPrefs.where(:value_type => nil).update_all(:value_type => 'string')
end

def down
Expand Down
21 changes: 21 additions & 0 deletions core/lib/spree/core/preference_rescue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Spree
class OldPrefs < ActiveRecord::Base
set_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"
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
end
end
end
end
end
17 changes: 3 additions & 14 deletions promo/db/migrate/20120119024707_namespace_promo_tables.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'spree/core/preference_rescue'

class NamespacePromoTables < ActiveRecord::Migration

def concat(str1, str2)
Expand Down Expand Up @@ -29,12 +31,6 @@ def replace_column_data(table_names, column_name)
end
end

class OldPrefs < ActiveRecord::Base
set_table_name "spree_preferences"
belongs_to :owner, :polymorphic => true
attr_accessor :owner_klass
end

def self.up
# namespace promo tables
rename_table :promotion_actions, :spree_promotion_actions
Expand Down Expand Up @@ -66,14 +62,7 @@ def self.up
preference.destroy
end

OldPrefs.where(:owner_type => 'Spree::PromotionRule').each do |old_pref|
owner = old_pref.owner
old_pref.key = [owner.class.name, old_pref.name, owner.id].join('::').underscore
old_pref.value_type = owner.preference_type(old_pref.name)
say "Migrating Preference: #{old_pref.key}"
old_pref.save(:validate => false)
end

Spree::PreferenceRescue.try

# This *should* be in the new_preferences migration inside of Core but...
# This is migration needs to have these keys around so that
Expand Down

0 comments on commit 4e6cf4d

Please sign in to comment.