Skip to content

Commit

Permalink
Merge pull request #2398 from jhawthorn/discard_promotion_actions
Browse files Browse the repository at this point in the history
Convert promotion actions from paranoia to discard
  • Loading branch information
jhawthorn authored Jan 10, 2018
2 parents 30581e9 + 3930263 commit 7bc7cc5
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def create

def destroy
@promotion_action = @promotion.promotion_actions.find(params[:id])
if @promotion_action.destroy
if @promotion_action.discard
flash[:success] = t('spree.successfully_removed', resource: t('spree.promotion_action'))
end
respond_to do |format|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CreateAdjustment < PromotionAction

before_validation :ensure_action_has_calculator
before_destroy :remove_adjustments_from_incomplete_orders
before_discard :remove_adjustments_from_incomplete_orders

# Creates the adjustment related to a promotion for the order passed
# through options hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CreateItemAdjustments < PromotionAction

before_validation :ensure_action_has_calculator
before_destroy :remove_adjustments_from_incomplete_orders
before_discard :remove_adjustments_from_incomplete_orders

def perform(payload = {})
order = payload[:order]
Expand Down
6 changes: 6 additions & 0 deletions core/app/models/spree/promotion_action.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
require 'discard'

module Spree
# Base class for all types of promotion action.
#
# PromotionActions perform the necessary tasks when a promotion is activated
# by an event and determined to be eligible.
class PromotionAction < Spree::Base
acts_as_paranoid
include Spree::ParanoiaDeprecations

include Discard::Model
self.discard_column = :deleted_at

belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_actions

Expand Down
16 changes: 13 additions & 3 deletions core/spec/models/spree/promotion/actions/create_adjustment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
end
end

context "#paranoia_destroy" do
shared_examples "destroying adjustments from incomplete orders" do
before(:each) do
action.calculator = Spree::Calculator::FlatRate.new(preferred_amount: 10)
promotion.promotion_actions = [action]
Expand All @@ -87,7 +87,7 @@
context "when order is not complete" do
it "should not keep the adjustment" do
action.perform(payload)
action.paranoia_destroy
subject
expect(order.adjustments.count).to eq(0)
end
end
Expand All @@ -99,7 +99,7 @@

before(:each) do
action.perform(payload)
action.paranoia_destroy
subject
end

it "should keep the adjustment" do
Expand All @@ -111,4 +111,14 @@
end
end
end

context "#discard" do
subject { action.discard }
it_should_behave_like "destroying adjustments from incomplete orders"
end

context "#paranoia_destroy" do
subject { Spree::Deprecation.silence { action.paranoia_destroy } }
it_should_behave_like "destroying adjustments from incomplete orders"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ module Spree
end
end

context "#paranoia_destroy" do
shared_examples "destroying adjustments from incomplete orders" do
let!(:action) { promotion.actions.first }
let(:other_action) { other_promotion.actions.first }
let(:promotion) { create(:promotion, :with_line_item_adjustment) }
Expand All @@ -151,7 +151,7 @@ module Spree
order.adjustments.create!(label: 'Check', amount: 0, order: order, source: action)

expect {
action.paranoia_destroy
subject
}.to change { Adjustment.count }.by(-1)
end
end
Expand All @@ -165,7 +165,7 @@ module Spree

expect {
expect {
action.paranoia_destroy
subject
}.not_to change { adjustment.reload.source_id }
}.not_to change { Spree::Adjustment.count }

Expand All @@ -177,10 +177,20 @@ module Spree
order.adjustments.create!(label: "Check", amount: 0, order: order, source: action)

expect {
action.paranoia_destroy
subject
}.not_to change { other_action.adjustments.count }
end
end
end

describe "#discard" do
subject { action.discard }
it_should_behave_like "destroying adjustments from incomplete orders"
end

describe "#paranoia_destroy" do
subject { Spree::Deprecation.silence { action.paranoia_destroy } }
it_should_behave_like "destroying adjustments from incomplete orders"
end
end
end
11 changes: 5 additions & 6 deletions frontend/spec/features/coupon_code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,12 @@ def create_basic_coupon_promotion(code)
calculator = Spree::Calculator::FlatPercentItemTotal.new
calculator.preferred_flat_percent = 100

action = Spree::Promotion::Actions::CreateAdjustment.new
action.calculator = calculator
action.promotion = promotion
action.save!
promotion.promotion_actions.first.discard

promotion.promotion_actions = [action]
promotion.save!
Spree::Promotion::Actions::CreateAdjustment.create!(
calculator: calculator,
promotion: promotion
)

create(:product, name: "Spree Mug", price: 10)
end
Expand Down

0 comments on commit 7bc7cc5

Please sign in to comment.