Skip to content

Commit

Permalink
Remove Paranoia from Product model
Browse files Browse the repository at this point in the history
Remove Paranoia and leave Discard as the only option for soft-deleting
the records.
  • Loading branch information
cedum committed Jan 31, 2020
1 parent 2eb36bd commit 625c12d
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 20 deletions.
2 changes: 1 addition & 1 deletion api/app/controllers/spree/api/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def find_product(id)

def product_scope
if can?(:admin, Spree::Product)
scope = Spree::Product.with_deleted.accessible_by(current_ability, :read).includes(*product_includes)
scope = Spree::Product.with_discarded.accessible_by(current_ability, :read).includes(*product_includes)

unless params[:show_deleted]
scope = scope.not_deleted
Expand Down
2 changes: 1 addition & 1 deletion backend/app/controllers/spree/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def split_params
end

def find_resource
Spree::Product.with_deleted.friendly.find(params[:id])
Spree::Product.with_discarded.friendly.find(params[:id])
end

def location_after_save
Expand Down
2 changes: 1 addition & 1 deletion backend/app/controllers/spree/admin/variants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def redirect_on_empty_option_values
end

def parent
@parent ||= Spree::Product.with_deleted.find_by(slug: params[:product_id])
@parent ||= Spree::Product.with_discarded.find_by(slug: params[:product_id])
@product = @parent
end
end
Expand Down
2 changes: 1 addition & 1 deletion backend/app/views/spree/admin/products/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<div class="col-2">
<div class="field checkbox">
<label>
<%= f.check_box :with_deleted, { checked: params[:q][:with_deleted] == 'true' }, 'true', 'false' %>
<%= f.check_box :with_discarded, { checked: params[:q][:with_discarded] == 'true' }, 'true', 'false' %>
<%= t('spree.show_deleted') %>
</label>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
end
end

context 'when params[:q][:with_deleted] is set to "true"' do
let(:params) { { q: { with_deleted: 'true' } } }
context 'when params[:q][:with_discarded] is set to "true"' do
let(:params) { { q: { with_discarded: 'true' } } }

it 'includes soft-deleted products' do
get :index, params: params
Expand Down
2 changes: 1 addition & 1 deletion backend/spec/features/admin/products/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def build_option_type_with_values(name, values)
expect(page).not_to have_content("apache baseball cap")
check "Show Deleted"
click_button 'Search'
expect(find('input[name="q[with_deleted]"]')).to be_checked
expect(find('input[name="q[with_discarded]"]')).to be_checked
expect(page).to have_content("zomg shirt")
expect(page).to have_content("apache baseball cap")
uncheck "Show Deleted"
Expand Down
7 changes: 3 additions & 4 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ class Product < Spree::Base
extend FriendlyId
friendly_id :slug_candidates, use: :history

acts_as_paranoid
include Spree::ParanoiaDeprecations

include Discard::Model
self.discard_column = :deleted_at

default_scope { kept }

after_discard do
variants_including_master.discard_all
self.product_option_types = []
Expand Down Expand Up @@ -134,7 +133,7 @@ def find_or_build_master
self.whitelisted_ransackable_attributes = %w[name slug]

def self.ransackable_scopes(_auth_object = nil)
%i(with_deleted with_variant_sku_cont)
%i(with_discarded with_variant_sku_cont)
end

# @return [Boolean] true if there are any variants
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Variant < Spree::Base
attr_writer :rebuild_vat_prices
include Spree::DefaultPrice

belongs_to :product, -> { with_deleted }, touch: true, class_name: 'Spree::Product', inverse_of: :variants, optional: false
belongs_to :product, -> { with_discarded }, touch: true, class_name: 'Spree::Product', inverse_of: :variants, optional: false
belongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true

delegate :name, :description, :slug, :available_on, :shipping_category_id,
Expand Down
7 changes: 0 additions & 7 deletions core/spec/models/spree/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,6 @@ class Extension < Spree::Base
end
end
end

context "#really_destroy!" do
it "destroy the product" do
product.really_destroy!
expect(product).not_to be_persisted
end
end
end

context "properties" do
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/controllers/spree/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def accurate_title

def load_product
if try_spree_current_user.try(:has_spree_role?, "admin")
@products = Spree::Product.with_deleted
@products = Spree::Product.with_discarded
else
@products = Spree::Product.available
end
Expand Down

0 comments on commit 625c12d

Please sign in to comment.