Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transparently replace promotion adjustments #39

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ module SolidusFriendlyPromotions
module AdjustmentDecorator
def self.prepended(base)
base.scope :friendly_promotion, -> { where(source_type: "SolidusFriendlyPromotions::PromotionAction") }
base.scope :promotion, -> { where(source_type: ["SolidusFriendlyPromotions::PromotionAction", "Spree::PromotionAction"]) }
end

def friendly_promotion?
source_type == "SolidusFriendlyPromotions::PromotionAction"
end

def promotion?
super || source_type == "SolidusFriendlyPromotions::PromotionAction"
end

private

def require_promotion_code?
!friendly_promotion? && super
end

Spree::Adjustment.prepend self
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def ensure_promotions_eligible
recalculate
errors.add(:base, I18n.t("solidus_friendly_promotions.promotion_total_changed_before_complete"))
end

super
errors.empty?
end

def discountable_item_total
Expand Down
4 changes: 2 additions & 2 deletions app/models/solidus_friendly_promotions/order_discounter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def call
# @param [Array<SolidusFriendlyPromotions::ItemDiscount>] item_discounts a list of calculated discounts for an item
# @return [void]
def update_adjustments(item, item_discounts)
promotion_adjustments = item.adjustments.select(&:friendly_promotion?)
promotion_adjustments = item.adjustments.select(&:promotion?)

active_adjustments = item_discounts.map do |item_discount|
update_adjustment(item, item_discount)
end
item.update(promo_total: active_adjustments.sum(&:amount))
# Remove any tax adjustments tied to rates which no longer match.
# Remove any tax adjustments tied to promotion actions which no longer match.
unmatched_adjustments = promotion_adjustments - active_adjustments

item.adjustments.destroy(unmatched_adjustments)
Expand Down
12 changes: 12 additions & 0 deletions spec/models/solidus_friendly_promotions/order_discounter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@
subject.call
}.to change { adjustable.adjustments.length }.by(0)
end

context "for an line item that has an adjustment from the old promotion system" do
let(:old_promotion_action) { create(:promotion, :with_adjustable_action, apply_automatically: false).actions.first }
let!(:adjustment) { create(:adjustment, source: old_promotion_action, adjustable: line_item) }

it "removes the old adjustment from the line item" do
adjustable.reload
expect {
subject.call
}.to change { adjustable.reload.adjustments.length }.by(-1)
end
end
end
end

Expand Down
73 changes: 73 additions & 0 deletions spec/models/spree/adjustment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Spree::Adjustment do
describe ".promotion" do
let(:spree_promotion_action) { create(:promotion, :with_action).actions.first }
let!(:spree_promotion_action_adjustment) { create(:adjustment, source: spree_promotion_action) }
let(:friendly_promotion_action) { create(:friendly_promotion, :with_adjustable_action).actions.first }
let!(:friendly_promotion_action_adjustment) { create(:adjustment, source: friendly_promotion_action) }
let(:tax_rate) { create(:tax_rate) }
let!(:tax_adjustment) { create(:adjustment, source: tax_rate) }

subject { described_class.promotion }

it { is_expected.to contain_exactly(friendly_promotion_action_adjustment, spree_promotion_action_adjustment) }
end

describe ".friendly_promotion" do
let(:spree_promotion_action) { create(:promotion, :with_action).actions.first }
let!(:spree_promotion_action_adjustment) { create(:adjustment, source: spree_promotion_action) }
let(:friendly_promotion_action) { create(:friendly_promotion, :with_adjustable_action).actions.first }
let!(:friendly_promotion_action_adjustment) { create(:adjustment, source: friendly_promotion_action) }
let(:tax_rate) { create(:tax_rate) }
let!(:tax_adjustment) { create(:adjustment, source: tax_rate) }

subject { described_class.friendly_promotion }

it { is_expected.to contain_exactly(friendly_promotion_action_adjustment) }
end

describe "#promotion?" do
let(:source) { create(:friendly_promotion, :with_adjustable_action).actions.first }
let!(:adjustment) { build(:adjustment, source: source) }

subject { adjustment.promotion? }

it { is_expected.to be true }

context "with a Spree Promotion Action source" do
let(:source) { create(:promotion, :with_action).actions.first }

it { is_expected.to be true }
end

context "with a Tax Rate source" do
let(:source) { create(:tax_rate) }

it { is_expected.to be false }
end
end

describe "#friendly_promotion?" do
let(:source) { create(:friendly_promotion, :with_adjustable_action).actions.first }
let!(:adjustment) { build(:adjustment, source: source) }

subject { adjustment.friendly_promotion? }

it { is_expected.to be true }

context "with a Spree Promotion Action source" do
let(:source) { create(:promotion, :with_action).actions.first }

it { is_expected.to be false }
end

context "with a Tax Rate source" do
let(:source) { create(:tax_rate) }

it { is_expected.to be false }
end
end
end