mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-02 02:11:33 +00:00
Abstract OFN's default stock location into a class
This commit is contained in:
15
app/services/default_stock_location.rb
Normal file
15
app/services/default_stock_location.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
# Encapsulates the concept of default stock location that OFN has, as explained
|
||||
# in https://github.com/openfoodfoundation/openfoodnetwork/wiki/Spree-Upgrade%3A-Stock-locations
|
||||
class DefaultStockLocation
|
||||
NAME = 'OFN default'.freeze
|
||||
|
||||
def self.create!
|
||||
country = Spree::Country.find_by_iso(ENV['DEFAULT_COUNTRY_CODE'])
|
||||
state = country.states.first
|
||||
Spree::StockLocation.create!(name: NAME, country_id: country.id, state_id: state.id)
|
||||
end
|
||||
|
||||
def self.destroy_all
|
||||
Spree::StockLocation.where(name: NAME).destroy_all
|
||||
end
|
||||
end
|
||||
@@ -656,8 +656,10 @@ end
|
||||
|
||||
FactoryBot.modify do
|
||||
factory :stock_location, class: Spree::StockLocation do
|
||||
name { 'default' }
|
||||
|
||||
# keeps the test stock_location unique
|
||||
initialize_with { Spree::StockLocation.find_or_create_by_name(name)}
|
||||
initialize_with { DefaultStockLocation.find_or_create }
|
||||
end
|
||||
|
||||
factory :shipment, class: Spree::Shipment do
|
||||
|
||||
32
spec/services/default_stock_location_spec.rb
Normal file
32
spec/services/default_stock_location_spec.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe DefaultStockLocation do
|
||||
describe '.create!' do
|
||||
it "names the location 'OFN default'" do
|
||||
stock_location = described_class.create!
|
||||
expect(stock_location.name).to eq('OFN default')
|
||||
end
|
||||
|
||||
it 'sets the location in the default country' do
|
||||
default_country = Spree::Country.find_by_iso(ENV['DEFAULT_COUNTRY_CODE'])
|
||||
stock_location = described_class.create!
|
||||
expect(stock_location.country).to eq(default_country)
|
||||
end
|
||||
|
||||
it 'sets the first state in the country' do
|
||||
default_country = Spree::Country.find_by_iso(ENV['DEFAULT_COUNTRY_CODE'])
|
||||
stock_location = described_class.create!
|
||||
expect(stock_location.state).to eq(default_country.states.first)
|
||||
end
|
||||
end
|
||||
|
||||
describe '.destroy_all' do
|
||||
it "removes all stock locations named 'OFN default'" do
|
||||
create(:stock_location, name: 'OFN default')
|
||||
create(:stock_location, name: 'OFN default')
|
||||
|
||||
expect { described_class.destroy_all }
|
||||
.to change { Spree::StockLocation.count }.from(2).to(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user