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.

Revisions

  1. alan-andrade revised this gist Apr 27, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion plan_script.rb
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ def self.doit
    migrate(plans, migrate_plan_sets: true)
    end

    def self.migrate(plans, migrate_plans_set: false)
    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 }
  2. alan-andrade revised this gist Apr 27, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion plan_script.rb
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ module Migrator
    com.lumosity.mobileipad.recurring.onemonth
    com.lumosity.mobileipad.recurring.oneyear)

    def self.migrate
    def self.doit
    plans = Shop::Plan.where internal_name: @plan_names
    migrate(plans, migrate_plan_sets: false)

  3. alan-andrade created this gist Apr 27, 2016.
    70 changes: 70 additions & 0 deletions plan_script.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    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.migrate
    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_plans_set: 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