Skip to content

Instantly share code, notes, and snippets.

@alan-andrade
Last active April 27, 2016 23:27
Show Gist options
  • Save alan-andrade/9b174078baeda319753a883709f9405c to your computer and use it in GitHub Desktop.
Save alan-andrade/9b174078baeda319753a883709f9405c to your computer and use it in GitHub Desktop.
module Migrator
@plan_names = %w(
com.lumosity.mobile.onemonth
com.lumosity.mobileipad.onemonth
com.lumosity.mobile.oneyear
com.lumosity.mobile.recurring.oneyear.20_off
com.lumosity.mobile.recurring.oneyear.25_off
com.lumosity.mobile.recurring.oneyear.35_off
com.lumosity.mobile.recurring.oneyear.40_off
com.lumosity.mobileipad.oneyear
com.lumosity.mobileipad.recurring.oneyear.20_off
com.lumosity.mobileipad.recurring.oneyear.25_off
com.lumosity.mobileipad.recurring.oneyear.35_off
com.lumosity.mobileipad.recurring.oneyear.40_off)
@plan_names_with_plan_set = %w(
com.lumosity.mobile.recurring.onemonth
com.lumosity.mobile.recurring.oneyear
com.lumosity.mobileipad.recurring.onemonth
com.lumosity.mobileipad.recurring.oneyear)
def self.doit
plans = Shop::Plan.where internal_name: @plan_names
migrate(plans, migrate_plan_sets: false)
plans = Shop::Plan.where internal_name: @plan_names_with_plan_set
migrate(plans, migrate_plan_sets: true)
end
def self.migrate(plans, migrate_plan_sets: false)
plans.each do |existing_plan|
new_plan = existing_plan.dup
access = existing_plan.access_items.map {|item| item.dup }
prices = existing_plan.prices.map {|price| price.dup }
translations = existing_plan.translations.map {|tr| tr.dup }
new_plan_name = "#{existing_plan.internal_name}_2"
old_plan_name = existing_plan.internal_name
new_plan.access_items = access
new_plan.prices = prices
new_plan.internal_name = new_plan_name
new_plan.translations = translations
new_plan.save
migrate_plan_sets_for_plan(new_plan_name, old_plan_name) if migrate_plan_sets
end
end
def self.migrate_plan_sets_for_plan(new_plan_name, old_plan_name)
plan_set = Shop::PlanSet.find_by_key 'monthly_1195'
original_plans = plan_set.plans
# Remove the old plans
filtered = original_plans.reject { |plan| plan.internal_name == old_plan_name }
# Retrieve the new plan
new_plan = Shop::Plan.find_by_internal_name(new_plan_name)
# Add the new plan to the filtered list.
filtered << new_plan
# Assign new plans to the plan set
plan_set.plans = filtered
# Save in DB
plan_set.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment