diff --git a/app/models/spree/stock_location.rb b/app/models/spree/stock_location.rb index a1e19233c0..e46461d27a 100644 --- a/app/models/spree/stock_location.rb +++ b/app/models/spree/stock_location.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree class StockLocation < ActiveRecord::Base has_many :stock_items, dependent: :delete_all @@ -6,22 +8,22 @@ module Spree belongs_to :state, class_name: 'Spree::State' belongs_to :country, class_name: 'Spree::Country' - validates_presence_of :name + validates :name, presence: true scope :active, -> { where(active: true) } - after_create :create_stock_items, :if => "self.propagate_all_variants?" + after_create :create_stock_items, if: "self.propagate_all_variants?" # Wrapper for creating a new stock item respecting the backorderable config def propagate_variant(variant) - self.stock_items.create!(variant: variant, backorderable: self.backorderable_default) + stock_items.create!(variant: variant, backorderable: backorderable_default) end # Return either an existing stock item or create a new one. Useful in # scenarios where the user might not know whether there is already a stock # item for a given variant def set_up_stock_item(variant) - self.stock_item(variant) || propagate_variant(variant) + stock_item(variant) || propagate_variant(variant) end def stock_item(variant) @@ -57,8 +59,9 @@ module Spree end private - def create_stock_items - Variant.find_each { |variant| self.propagate_variant(variant) } - end + + def create_stock_items + Variant.find_each { |variant| propagate_variant(variant) } + end end end diff --git a/app/models/spree/stock_movement.rb b/app/models/spree/stock_movement.rb index 33281653c0..1b799754d9 100644 --- a/app/models/spree/stock_movement.rb +++ b/app/models/spree/stock_movement.rb @@ -1,9 +1,10 @@ +# frozen_string_literal: true + module Spree class StockMovement < ActiveRecord::Base belongs_to :stock_item, class_name: 'Spree::StockItem' belongs_to :originator, polymorphic: true - after_create :update_stock_item_quantity validates :stock_item, presence: true @@ -16,10 +17,11 @@ module Spree end private + def update_stock_item_quantity return unless Spree::Config[:track_inventory_levels] + stock_item.adjust_count_on_hand quantity end end end - diff --git a/spec/models/spree/stock_location_spec.rb b/spec/models/spree/stock_location_spec.rb index 02a55ac877..261a5b175d 100644 --- a/spec/models/spree/stock_location_spec.rb +++ b/spec/models/spree/stock_location_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' module Spree @@ -102,7 +104,7 @@ module Spree end it 'finds a count_on_hand for a variant' do - subject.count_on_hand(variant).should eq 10 + subject.count_on_hand(variant).should eq 10 end it 'finds determines if you a variant is backorderable' do @@ -128,8 +130,8 @@ module Spree end it 'can be deactivated' do - create(:stock_location, :active => true) - create(:stock_location, :active => false) + create(:stock_location, active: true) + create(:stock_location, active: false) Spree::StockLocation.active.count.should eq 1 end diff --git a/spec/models/spree/stock_movement_spec.rb b/spec/models/spree/stock_movement_spec.rb index fcc387c460..54a10620e4 100644 --- a/spec/models/spree/stock_movement_spec.rb +++ b/spec/models/spree/stock_movement_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Spree::StockMovement do