Skip to content

Commit

Permalink
Revert "refactor product and variant model for restrict destroy if li…
Browse files Browse the repository at this point in the history
…ne items present. Change flash to show error of product if not destroyed in admin/products_controller"

This reverts commit f2e3447.
  • Loading branch information
damianlegawiec committed Jun 14, 2016
1 parent a607b1e commit 38ef7a7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
3 changes: 1 addition & 2 deletions backend/app/controllers/spree/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ def destroy

begin
# TODO: why is @product.destroy raising ActiveRecord::RecordNotDestroyed instead of failing with false result
# Issue found for above comment: https://github.com/rails/rails/issues/19761
if @product.destroy
flash[:success] = Spree.t('notice_messages.product_deleted')
else
flash[:error] = @object.errors.full_messages.join(', ')
flash[:error] = Spree.t('notice_messages.product_not_deleted')
end
rescue ActiveRecord::RecordNotDestroyed => e
flash[:error] = Spree.t('notice_messages.product_not_deleted')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,10 @@ def send_request
end

context 'will not successfully destroy product' do
let!(:error_message) { 'Test error' }

before do
allow(Spree::Product).to receive(:friendly).and_return(products)
allow(products).to receive(:find).with(product.id.to_s).and_return(product)
allow(product).to receive(:destroy).and_return(false)
allow(product).to receive_message_chain(:errors, :full_messages).and_return([error_message])
end

describe 'expects to receive' do
Expand All @@ -87,7 +84,7 @@ def send_request
describe 'response' do
before { send_request }
it { expect(response).to have_http_status(:ok) }
it { expect(flash[:error]).to eq(error_message) }
it { expect(flash[:error]).to eq(Spree.t('notice_messages.product_not_deleted')) }
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Product < Spree::Base
after_save :run_touch_callbacks, if: :anything_changed?
after_save :reset_nested_changes
after_touch :touch_taxons
before_destroy :ensure_no_line_items

before_validation :normalize_slug, on: :update
before_validation :validate_master
Expand Down Expand Up @@ -356,6 +357,13 @@ def touch_taxons
Spree::Taxonomy.where(id: taxonomy_ids).update_all(updated_at: Time.current)
end

def ensure_no_line_items
if line_items.any?
errors.add(:base, Spree.t(:cannot_destroy_if_attached_to_line_items))
return false
end
end

def remove_taxon(taxon)
removed_classifications = classifications.where(taxon: taxon)
removed_classifications.each &:remove_from_list
Expand Down
11 changes: 9 additions & 2 deletions core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ class Variant < Spree::Base

with_options inverse_of: :variant do
has_many :inventory_units
has_many :line_items
has_many :stock_items, dependent: :destroy
end

has_many :line_items, dependent: :restrict_with_error

has_many :orders, through: :line_items
with_options through: :stock_items do
has_many :stock_locations
Expand Down Expand Up @@ -49,6 +48,7 @@ class Variant < Spree::Base

after_create :create_stock_items
after_create :set_master_out_of_stock, unless: :is_master?
before_destroy :ensure_no_line_items

after_touch :clear_in_stock_cache

Expand Down Expand Up @@ -258,6 +258,13 @@ def discontinued?

private

def ensure_no_line_items
if line_items.any?
errors.add(:base, Spree.t(:cannot_destroy_if_attached_to_line_items))
return false
end
end

def quantifier
Spree::Stock::Quantifier.new(self)
end
Expand Down
8 changes: 8 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ en:
attributes:
expires_at:
invalid_date_range: must be later than start date
spree/product:
attributes:
base:
cannot_destroy_if_attached_to_line_items: Cannot delete products once they are attached to line items.
spree/refund:
attributes:
amount:
Expand Down Expand Up @@ -346,6 +350,10 @@ en:
attributes:
base:
cannot_destroy_if_attached_to_line_items: Cannot delete variants once they are attached to line items.
spree/shipping_method:
attributes:
base:
cannot_destroy_if_attached_to_line_items: Cannot delete variants once they are attached to line items.

devise:
confirmations:
Expand Down

0 comments on commit 38ef7a7

Please sign in to comment.