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

Convert store credits to discard #2489

Merged
merged 1 commit into from
Jan 10, 2018
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
7 changes: 7 additions & 0 deletions core/app/models/spree/store_credit.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
require 'discard'

class Spree::StoreCredit < Spree::PaymentSource
acts_as_paranoid
include Spree::ParanoiaDeprecations

include Discard::Model
self.discard_column = :deleted_at

VOID_ACTION = 'void'
CREDIT_ACTION = 'credit'
Expand Down Expand Up @@ -33,6 +39,7 @@ class Spree::StoreCredit < Spree::PaymentSource
before_validation :associate_credit_type
before_validation :validate_category_unchanged, on: :update
before_destroy :validate_no_amount_used
Copy link
Contributor Author

@jhawthorn jhawthorn Jan 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this existing line never worked. Though errors are added to the model, deleted_at is still set and persisted.

EDIT: confirmed broken since at least 1.4

validate :validate_no_amount_used, if: :discarded?

attr_accessor :action, :action_amount, :action_originator, :action_authorization_code, :update_reason

Expand Down
6 changes: 6 additions & 0 deletions core/app/models/spree/store_credit_event.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
require 'discard'

module Spree
class StoreCreditEvent < Spree::Base
acts_as_paranoid
include Spree::ParanoiaDeprecations

include Discard::Model
self.discard_column = :deleted_at

belongs_to :store_credit
belongs_to :originator, polymorphic: true
Expand Down
14 changes: 9 additions & 5 deletions core/spec/models/spree/store_credit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@

context "amount used is greater than zero" do
let(:store_credit) { create(:store_credit, amount: 100, amount_used: 1) }
subject { store_credit.paranoia_destroy }

it 'can not delete the store credit' do
subject
expect(store_credit.reload).to eq store_credit
expect(store_credit.errors[:amount_used]).to include("is greater than zero. Can not delete store credit")
describe "#discard" do
subject { store_credit.discard }

it 'can not delete the store credit' do
subject
expect(store_credit.reload).to eq store_credit
expect(store_credit.errors[:amount_used]).to include("is greater than zero. Can not delete store credit")
expect(store_credit).not_to be_discarded
end
end
end

Expand Down