Skip to content

Commit

Permalink
Remove deprecated Spree::PromotionAction#remove_from implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielePalombo authored and kennyadsl committed Nov 17, 2020
1 parent af2e17f commit 576280e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
11 changes: 2 additions & 9 deletions core/app/models/spree/promotion_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,8 @@ def perform(_options = {})
#
# @param order [Spree::Order] the order to remove the action from
# @return [void]
def remove_from(order)
Spree::Deprecation.warn("#{self.class.name.inspect} does not define #remove_from. The default behavior may be incorrect and will be removed in a future version of Solidus.", caller)
[order, *order.line_items, *order.shipments].each do |item|
item.adjustments.each do |adjustment|
if adjustment.source == self
item.adjustments.destroy(adjustment)
end
end
end
def remove_from(_order)
raise 'remove_from should be implemented in a sub-class of PromotionAction'
end

def to_partial_path
Expand Down
34 changes: 23 additions & 11 deletions core/spec/models/spree/promotion_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def perform(options = {})
order.adjustments.create!(amount: 1, order: order, source: self, label: 'foo')
true
end

def remove_from(_order)
'Implement your remove logic'
end
end

let(:action) { promotion.actions.first! }
Expand All @@ -19,26 +23,34 @@ def perform(options = {})
# this adjustment should not get removed
let!(:other_adjustment) { create(:adjustment, order: order, source: nil) }

before do
it "generates its own partial path" do
action.perform(order: order)
@action_adjustment = order.adjustments.where(source: action).first!
end

it "generates its own partial path" do
expect(action.to_partial_path).to eq 'spree/admin/promotions/actions/my_promotion_action'
end

it 'removes the action adjustment' do
expect(order.adjustments).to match_array([other_adjustment, @action_adjustment])
it 'executes the remove logic' do
action.perform(order: order)
@action_adjustment = order.adjustments.where(source: action).first!

expect(Spree::Deprecation).to(
receive(:warn).
with(/"MyPromotionAction" does not define #remove_from/, anything)
)
expect(action.remove_from(order)).to eq('Implement your remove logic')
end

context "when PromotionAction doesn't implement perform method" do
before { MyPromotionAction.remove_method :perform }

it 'raises RuntimeError' do
expect { action.perform }.to raise_error(RuntimeError, 'perform should be implemented in a sub-class of PromotionAction')
end
end

action.remove_from(order)
context "when PromotionAction doesn't implement remove_from method" do
before { MyPromotionAction.remove_method :remove_from }

expect(order.adjustments).to eq([other_adjustment])
it 'raises RuntimeError' do
expect { action.remove_from(order) }.to raise_error(RuntimeError, 'remove_from should be implemented in a sub-class of PromotionAction')
end
end
end
end

0 comments on commit 576280e

Please sign in to comment.