Skip to content

Commit

Permalink
Merge pull request #12887 from mkllnk/stock-cleanup2
Browse files Browse the repository at this point in the history
Remove unneeded StockLocation code
  • Loading branch information
rioug authored Oct 6, 2024
2 parents b7c34ce + e5ee398 commit e5b7f89
Show file tree
Hide file tree
Showing 12 changed files with 11 additions and 36 deletions.
1 change: 0 additions & 1 deletion app/models/spree/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def initialize(user)
can [:read, :update, :destroy], Spree::User, id: user.id
can [:index, :read], State
can [:index, :read], StockItem
can [:index, :read], StockLocation
can [:index, :read], StockMovement
can [:index, :read], Taxon
can [:index, :read], Variant
Expand Down
12 changes: 1 addition & 11 deletions app/models/spree/stock/quantifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Quantifier

def initialize(variant)
@variant = variant
@stock_items = fetch_stock_items
@stock_items = @variant.stock_items
end

def total_on_hand
Expand All @@ -25,16 +25,6 @@ def backorderable?
def can_supply?(required)
total_on_hand >= required || backorderable?
end

private

def fetch_stock_items
# Don't re-fetch associated stock items from the DB if we've already eager-loaded them
return @variant.stock_items if @variant.stock_items.loaded?

Spree::StockItem.joins(:stock_location).
where(:variant_id => @variant, Spree::StockLocation.table_name => { active: true })
end
end
end
end
10 changes: 2 additions & 8 deletions app/models/spree/stock_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Spree
class StockLocation < ApplicationRecord
self.belongs_to_required_by_default = false
self.ignored_columns += [:backorderable_default]
self.ignored_columns += [:backorderable_default, :active]

has_many :stock_items, dependent: :delete_all, inverse_of: :stock_location
has_many :stock_movements, through: :stock_items
Expand All @@ -13,15 +13,9 @@ class StockLocation < ApplicationRecord

validates :name, presence: true

scope :active, -> { where(active: true) }

after_create :create_stock_items

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

def stock_item(variant)
stock_items.where(variant_id: variant).order(:id).first
end
Expand Down Expand Up @@ -57,7 +51,7 @@ def fill_status(variant, quantity)
private

def create_stock_items
Variant.find_each { |variant| propagate_variant(variant) }
Variant.find_each { |variant| stock_items.create!(variant:) }
end
end
end
2 changes: 1 addition & 1 deletion app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def create_stock_items
return unless stock_items.empty?

StockLocation.find_each do |stock_location|
stock_location.propagate_variant(self)
stock_items.create!(stock_location:)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@

= f.field_container :stock_location do
= f.label :stock_location, t('.stock_location')
= f.select :stock_location_id, Spree::StockLocation.active.all.collect{ |l| [l.name, l.id] }, { style: 'height:100px;', class: 'fullwidth' }
= f.select :stock_location_id, Spree::StockLocation.all.collect{ |l| [l.name, l.id] }, { style: 'height:100px;', class: 'fullwidth' }
= f.error_message_on :reason
2 changes: 1 addition & 1 deletion spec/controllers/api/v0/shipments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
let(:current_api_user) { build(:admin_user) }
let!(:order) { shipment.order }
let(:order_ship_address) { create(:address) }
let!(:stock_location) { Spree::StockLocation.first || create(:stock_location) }
let!(:stock_location) { DefaultStockLocation.find_or_create }
let!(:variant) { create(:variant) }
let(:params) do
{ quantity: 2,
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/product_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
variant_unit_name { '' }

# ensure stock item will be created for this products master
before(:create) { create(:stock_location) if Spree::StockLocation.count.zero? }
before(:create) { DefaultStockLocation.find_or_create }

factory :product do
transient do
Expand Down
4 changes: 2 additions & 2 deletions spec/factories/shipment_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
state { 'pending' }
order
address
stock_location { Spree::StockLocation.first || create(:stock_location) }
stock_location { DefaultStockLocation.find_or_create }

after(:create) do |shipment, _evalulator|
shipment.add_shipping_method(create(:shipping_method), true)
Expand All @@ -31,7 +31,7 @@
state { 'pending' }
order
address
stock_location { Spree::StockLocation.first || create(:stock_location) }
stock_location { DefaultStockLocation.find_or_create }

trait :shipping_method do
transient do
Expand Down
1 change: 0 additions & 1 deletion spec/factories/stock_location_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
city { 'Washington' }
zipcode { '20500' }
phone { '(202) 456-1111' }
active { true }

country { |stock_location| Spree::Country.first || stock_location.association(:country) }
state do |stock_location|
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/variant_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
product { association :base_product }

# ensure stock item will be created for this variant
before(:create) { create(:stock_location) if Spree::StockLocation.count.zero? }
before(:create) { DefaultStockLocation.find_or_create }

factory :variant do
transient do
Expand Down
7 changes: 0 additions & 7 deletions spec/models/spree/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,6 @@
end
end

context 'for StockLocation' do
let(:resource) { Spree::StockLocation.new }
context 'requested by any user' do
it_should_behave_like 'read only'
end
end

context 'for StockMovement' do
let(:resource) { Spree::StockMovement.new }
context 'requested by any user' do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/spree/stock_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Spree
subject { StockLocation.new(name: "testing") }

specify do
expect(subject).to receive(:propagate_variant).at_least(:once)
expect(subject.stock_items).to receive(:create!).at_least(:once)
subject.save!
end
end
Expand Down

0 comments on commit e5b7f89

Please sign in to comment.