-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from friendlycart/migrate_order_promotions
Add migrate order promotions rake task
- Loading branch information
Showing
4 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
friendly_promotions/lib/solidus_friendly_promotions/migrate_order_promotions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusFriendlyPromotions | ||
class MigrateOrderPromotions | ||
class << self | ||
def up | ||
Spree::OrderPromotion.all.each do |order_promotion| | ||
friendly_promotion = SolidusFriendlyPromotions::Promotion.find_by!(original_promotion_id: order_promotion.promotion.id) | ||
friendly_promotion_code = friendly_promotion.codes.find_by(value: order_promotion.promotion_code.value) | ||
SolidusFriendlyPromotions::OrderPromotion.find_or_create_by!(order: order_promotion.order, | ||
promotion: friendly_promotion, | ||
promotion_code: friendly_promotion_code) | ||
order_promotion.destroy! | ||
end | ||
end | ||
|
||
def down | ||
SolidusFriendlyPromotions::OrderPromotion.all.each do |friendly_order_promotion| | ||
spree_promotion = friendly_order_promotion.promotion.original_promotion | ||
spree_promotion_code = spree_promotion.promotion_codes.find_by(value: friendly_order_promotion.promotion_code.value) | ||
Spree::OrderPromotion.find_or_create_by!(order: friendly_order_promotion.order, | ||
promotion: spree_promotion, | ||
promotion_code: spree_promotion_code) | ||
friendly_order_promotion.destroy! | ||
end | ||
end | ||
end | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
friendly_promotions/lib/tasks/solidus_friendly_promotions/migrate_order_promotions.rake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :solidus_friendly_promotions do | ||
namespace :migrate_order_promotions do | ||
desc "Migrate order promotions from Spree::OrderPromotion sources to SolidusFriendlyPromotions::FriendlyOrderPromotion sources" | ||
task up: :environment do | ||
require "solidus_friendly_promotions/migrate_order_promotions" | ||
SolidusFriendlyPromotions::MigrateOrderPromotions.up | ||
end | ||
|
||
desc "Migrate order promotions from SolidusFriendlyPromotions::FriendlyOrderPromotion sources to Spree::OrderPromotion sources" | ||
task down: :environment do | ||
require "solidus_friendly_promotions/migrate_order_promotions" | ||
SolidusFriendlyPromotions::MigrateOrderPromotions.down | ||
end | ||
end | ||
end |
66 changes: 66 additions & 0 deletions
66
friendly_promotions/spec/lib/solidus_friendly_promotions/migrate_order_promotions_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
require "solidus_friendly_promotions/promotion_migrator" | ||
require "solidus_friendly_promotions/promotion_map" | ||
require "solidus_friendly_promotions/migrate_order_promotions" | ||
|
||
RSpec.describe SolidusFriendlyPromotions::MigrateOrderPromotions do | ||
let(:promotion) { create(:promotion, :with_adjustable_action) } | ||
let(:order) { create(:order_with_line_items) } | ||
let(:line_item) { order.line_items.first } | ||
let(:promotion_code) { create(:promotion_code, promotion: promotion) } | ||
|
||
before do | ||
order.order_promotions.create!(promotion: promotion, promotion_code: promotion_code) | ||
SolidusFriendlyPromotions::PromotionMigrator.new( | ||
SolidusFriendlyPromotions::PROMOTION_MAP | ||
).call | ||
end | ||
|
||
describe ".up" do | ||
subject { described_class.up } | ||
|
||
it "migrates our order promotion" do | ||
expect { subject }.to change { | ||
Spree::OrderPromotion.count | ||
}.from(1).to(0) | ||
end | ||
|
||
it "creates our order promotion" do | ||
expect { subject }.to change { | ||
SolidusFriendlyPromotions::OrderPromotion.count | ||
}.from(0).to(1) | ||
|
||
order_promotion = SolidusFriendlyPromotions::OrderPromotion.first | ||
expect(order_promotion.order).to eq(order) | ||
expect(order_promotion.promotion).to eq(SolidusFriendlyPromotions::Promotion.first) | ||
expect(order_promotion.promotion_code.value).to eq(promotion_code.value) | ||
end | ||
end | ||
|
||
describe ".down" do | ||
subject { described_class.down } | ||
|
||
before do | ||
described_class.up | ||
end | ||
|
||
it "migrates our order promotion" do | ||
expect { subject }.to change { | ||
Spree::OrderPromotion.count | ||
}.from(0).to(1) | ||
|
||
order_promotion = Spree::OrderPromotion.first | ||
expect(order_promotion.order).to eq(order) | ||
expect(order_promotion.promotion).to eq(promotion) | ||
expect(order_promotion.promotion_code).to eq(promotion_code) | ||
end | ||
|
||
it "creates our order promotion" do | ||
expect { subject }.to change { | ||
SolidusFriendlyPromotions::OrderPromotion.count | ||
}.from(1).to(0) | ||
end | ||
end | ||
end |