Skip to content

Commit

Permalink
Merge pull request #12885 from mkllnk/stock-cleanup
Browse files Browse the repository at this point in the history
Remove use of unnecessary backorderable default column
  • Loading branch information
rioug authored Oct 2, 2024
2 parents 6eb5986 + 4d9f396 commit 63a1b39
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 24 deletions.
14 changes: 2 additions & 12 deletions app/models/concerns/variant_stock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,9 @@ def on_hand=(new_level)
def on_demand
# A variant that has not been saved yet or has been soft-deleted doesn't have a stock item
# This provides a default value for variant.on_demand
# using Spree::StockLocation.backorderable_default
return Spree::StockLocation.first.backorderable_default if new_record? || deleted?

# This can be removed unless we have seen this error in Bugsnag recently
if stock_item.nil?
Bugsnag.notify(
RuntimeError.new("Variant #stock_item called, but the stock_item does not exist!"),
object: as_json
)
return Spree::StockLocation.first.backorderable_default
end
return false if new_record? || deleted?

stock_item.backorderable?
stock_item&.backorderable?
end

# Sets whether the variant can be ordered on demand or not. Note that
Expand Down
3 changes: 2 additions & 1 deletion app/models/spree/stock_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Spree
class StockLocation < ApplicationRecord
self.belongs_to_required_by_default = false
self.ignored_columns += [:backorderable_default]

has_many :stock_items, dependent: :delete_all, inverse_of: :stock_location
has_many :stock_movements, through: :stock_items
Expand All @@ -18,7 +19,7 @@ class StockLocation < ApplicationRecord

# Wrapper for creating a new stock item respecting the backorderable config
def propagate_variant(variant)
stock_items.create!(variant:, backorderable: backorderable_default)
stock_items.create!(variant:)
end

def stock_item(variant)
Expand Down
3 changes: 0 additions & 3 deletions spec/factories/stock_location_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
phone { '(202) 456-1111' }
active { true }

# sets the default value for variant.on_demand
backorderable_default { false }

country { |stock_location| Spree::Country.first || stock_location.association(:country) }
state do |stock_location|
stock_location.country.states.first ||
Expand Down
2 changes: 1 addition & 1 deletion spec/models/concerns/variant_stock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
build_stubbed(
:variant,
stock_locations: [
build_stubbed(:stock_location, backorderable_default: false)
build_stubbed(:stock_location)
]
)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/models/spree/stock_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Spree
RSpec.describe StockLocation do
subject { create(:stock_location_with_items, backorderable_default: true) }
subject { create(:stock_location_with_items) }
let(:stock_item) { subject.stock_items.order(:id).first }
let(:variant) { stock_item.variant }

Expand Down Expand Up @@ -47,7 +47,7 @@ module Spree
end

it 'finds determines if you a variant is backorderable' do
expect(subject.backorderable?(variant)).to be_truthy
expect(subject.backorderable?(variant)).to eq false
end

it 'restocks a variant with a positive stock movement' do
Expand Down
2 changes: 1 addition & 1 deletion spec/services/sets/product_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
subject{ product_set.save }

context 'when the product does not exist yet' do
let!(:stock_location) { create(:stock_location, backorderable_default: false) }
let!(:stock_location) { create(:stock_location) }
let(:collection_hash) do
{
0 => {
Expand Down
2 changes: 1 addition & 1 deletion spec/system/admin/bulk_product_update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
end

it "creating a new product" do
create(:stock_location, backorderable_default: false)
create(:stock_location)

supplier = create(:supplier_enterprise)
distributor = create(:distributor_enterprise)
Expand Down
2 changes: 1 addition & 1 deletion spec/system/admin/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
include FileHelper

let!(:taxon) { create(:taxon) }
let!(:stock_location) { create(:stock_location, backorderable_default: false) }
let!(:stock_location) { create(:stock_location) }
let!(:shipping_category) { DefaultShippingCategory.find_or_create }
let!(:supplier) { create(:supplier_enterprise, name: 'New supplier') }

Expand Down
2 changes: 1 addition & 1 deletion spec/system/admin/products_v3/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
let!(:taxon) { create(:taxon) }

describe "creating a new product" do
let!(:stock_location) { create(:stock_location, backorderable_default: false) }
let!(:stock_location) { create(:stock_location) }
let!(:distributor) { create(:distributor_enterprise) }
let!(:shipping_category) { create(:shipping_category) }

Expand Down
2 changes: 1 addition & 1 deletion spec/system/admin/unit_price_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
include AuthenticationHelper
include WebHelper

let!(:stock_location) { create(:stock_location, backorderable_default: false) }
let!(:stock_location) { create(:stock_location) }

describe "product" do
it "creating a new product" do
Expand Down

0 comments on commit 63a1b39

Please sign in to comment.