Merge pull request #4787 from luisramos0/catalog_domain

Add new domain/engine Catalog
This commit is contained in:
Luis Ramos
2020-03-16 18:47:55 +00:00
committed by GitHub
22 changed files with 243 additions and 172 deletions

View File

@@ -681,7 +681,6 @@ Style/FrozenStringLiteralComment:
- 'app/models/product_import/entry_validator.rb'
- 'app/models/product_import/inventory_reset_strategy.rb'
- 'app/models/product_import/product_importer.rb'
- 'app/models/product_import/products_reset_strategy.rb'
- 'app/models/product_import/reset_absent.rb'
- 'app/models/product_import/settings.rb'
- 'app/models/product_import/spreadsheet_data.rb'
@@ -1257,7 +1256,6 @@ Style/FrozenStringLiteralComment:
- 'spec/models/producer_property_spec.rb'
- 'spec/models/product_import/entry_processor_spec.rb'
- 'spec/models/product_import/inventory_reset_strategy_spec.rb'
- 'spec/models/product_import/products_reset_strategy_spec.rb'
- 'spec/models/product_import/reset_absent_spec.rb'
- 'spec/models/product_import/settings_spec.rb'
- 'spec/models/product_importer_spec.rb'

View File

@@ -12,6 +12,7 @@ gem "activerecord-import"
# Patched version. See http://rubysec.com/advisories/CVE-2015-5312/.
gem 'nokogiri', '>= 1.6.7.1'
gem "catalog", path: "./engines/catalog"
gem "order_management", path: "./engines/order_management"
gem 'web', path: './engines/web'

View File

@@ -75,6 +75,11 @@ GIT
activemodel (>= 3.0)
railties (>= 3.0)
PATH
remote: engines/catalog
specs:
catalog (0.0.1)
PATH
remote: engines/order_management
specs:
@@ -711,6 +716,7 @@ DEPENDENCIES
bugsnag
byebug (~> 9.0.0)
capybara (>= 2.18.0)
catalog!
coffee-rails (~> 3.2.1)
combine_pdf
compass-rails

View File

@@ -74,7 +74,7 @@ module ProductImport
if settings.importing_into_inventory?
InventoryResetStrategy
else
ProductsResetStrategy
Catalog::ProductImport::ProductsResetStrategy
end
end

View File

@@ -1,45 +0,0 @@
module ProductImport
class ProductsResetStrategy
def initialize(excluded_items_ids)
@excluded_items_ids = excluded_items_ids
end
def reset(enterprise_ids)
@enterprise_ids = enterprise_ids
return 0 if enterprise_ids.blank?
reset_variants_on_hand(enterprise_variants_relation)
end
private
attr_reader :excluded_items_ids, :enterprise_ids
def enterprise_variants_relation
relation = Spree::Variant
.joins(:product)
.where(
spree_products: { supplier_id: enterprise_ids },
spree_variants: { is_master: false, deleted_at: nil }
)
return relation if excluded_items_ids.blank?
relation.where('spree_variants.id NOT IN (?)', excluded_items_ids)
end
def reset_variants_on_hand(variants)
updated_records_count = 0
variants.each do |variant|
updated_records_count += 1 if reset_variant_on_hand(variant)
end
updated_records_count
end
def reset_variant_on_hand(variant)
variant.on_hand = 0
variant.on_hand.zero?
end
end
end

View File

@@ -91,6 +91,7 @@ Openfoodnetwork::Application.routes.draw do
# Mount engine routes
mount Web::Engine, :at => '/'
mount Catalog::Engine, :at => '/'
mount OrderManagement::Engine, :at => '/'
# Mount Spree's routes

View File

@@ -0,0 +1,5 @@
# Catalog
This is the rails engine for the Catalog domain.
See our wiki for [more info about domains and engines in OFN](https://github.com/openfoodfoundation/openfoodnetwork/wiki/Tech-Doc:-How-OFN-is-organized-in-Domains-using-Rails-Engines).

View File

@@ -0,0 +1,13 @@
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require_tree .

View File

@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.

View File

@@ -0,0 +1,5 @@
module Catalog
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
end

View File

@@ -0,0 +1,49 @@
# frozen_string_literal: true
module Catalog
module ProductImport
class ProductsResetStrategy
def initialize(excluded_items_ids)
@excluded_items_ids = excluded_items_ids
end
def reset(enterprise_ids)
@enterprise_ids = enterprise_ids
return 0 if enterprise_ids.blank?
reset_variants_on_hand(enterprise_variants_relation)
end
private
attr_reader :excluded_items_ids, :enterprise_ids
def enterprise_variants_relation
relation = Spree::Variant
.joins(:product)
.where(
spree_products: { supplier_id: enterprise_ids },
spree_variants: { is_master: false, deleted_at: nil }
)
return relation if excluded_items_ids.blank?
relation.where('spree_variants.id NOT IN (?)', excluded_items_ids)
end
def reset_variants_on_hand(variants)
updated_records_count = 0
variants.each do |variant|
updated_records_count += 1 if reset_variant_on_hand(variant)
end
updated_records_count
end
def reset_variant_on_hand(variant)
variant.on_hand = 0
variant.on_hand.zero?
end
end
end
end

View File

@@ -0,0 +1,13 @@
$LOAD_PATH.push File.expand_path('lib', __dir__)
require "catalog/version"
Gem::Specification.new do |s|
s.name = "catalog"
s.version = Catalog::VERSION
s.authors = ["developers@ofn"]
s.summary = "Catalog domain of the OFN solution."
s.files = Dir["{app,config,db,lib}/**/*"] + ["LICENSE.txt", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]
end

View File

@@ -0,0 +1,2 @@
Catalog::Engine.routes.draw do
end

View File

@@ -0,0 +1,4 @@
require "catalog/engine"
module Catalog
end

View File

@@ -0,0 +1,5 @@
module Catalog
class Engine < ::Rails::Engine
isolate_namespace Catalog
end
end

View File

@@ -0,0 +1,3 @@
module Catalog
VERSION = "0.0.1".freeze
end

View File

@@ -0,0 +1,127 @@
# frozen_string_literal: true
require 'spec_helper'
module Catalog
module ProductImport
describe ProductsResetStrategy do
let(:products_reset) { described_class.new(excluded_items_ids) }
describe '#reset' do
let(:supplier_ids) { enterprise.id }
let(:product) { create(:product) }
let(:enterprise) { product.supplier }
let(:variant) { product.variants.first }
before { variant.on_hand = 2 }
context 'when there are excluded_items_ids' do
let(:excluded_items_ids) { [variant.id] }
context 'and supplier_ids is []' do
let(:supplier_ids) { [] }
it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is nil' do
let(:supplier_ids) { nil }
it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is set' do
it 'does not update the on_hand of the excluded items' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(2)
end
it 'updates the on_hand of the non-excluded items' do
non_excluded_variant = create(
:variant,
product: variant.product
)
non_excluded_variant.on_hand = 3
products_reset.reset(supplier_ids)
expect(non_excluded_variant.reload.on_hand).to eq(0)
end
end
end
context 'when there are no excluded_items_ids' do
let(:excluded_items_ids) { [] }
context 'and supplier_ids is []' do
let(:supplier_ids) { [] }
it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is nil' do
let(:supplier_ids) { nil }
it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is not nil' do
it 'sets all on_hand to 0' do
updated_records_count = products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(0)
expect(updated_records_count).to eq(1)
end
context 'and there is an unresetable variant' do
before do
variant.stock_items = [] # this makes variant.on_hand raise an error
end
it 'returns correct number of resetted variants' do
expect { products_reset.reset(supplier_ids) }.to raise_error RuntimeError
end
end
end
end
context 'when excluded_items_ids is nil' do
let(:excluded_items_ids) { nil }
context 'and supplier_ids is []' do
let(:supplier_ids) { [] }
it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is nil' do
let(:supplier_ids) { nil }
it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is nil' do
it 'sets all on_hand to 0' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(0)
end
end
end
end
end
end
end

View File

@@ -0,0 +1,3 @@
require "../../spec/spec_helper.rb"
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }

View File

@@ -94,13 +94,13 @@ describe ProductImport::EntryProcessor do
context 'when not importing into inventory' do
let(:reset_stock_strategy) do
instance_double(ProductImport::ProductsResetStrategy)
instance_double(Catalog::ProductImport::ProductsResetStrategy)
end
before do
allow(settings).to receive(:importing_into_inventory?) { false }
allow(ProductImport::ProductsResetStrategy)
allow(Catalog::ProductImport::ProductsResetStrategy)
.to receive(:new).with([1]) { reset_stock_strategy }
end

View File

@@ -1,121 +0,0 @@
require 'spec_helper'
describe ProductImport::ProductsResetStrategy do
let(:products_reset) { described_class.new(excluded_items_ids) }
describe '#reset' do
let(:supplier_ids) { enterprise.id }
let(:product) { create(:product) }
let(:enterprise) { product.supplier }
let(:variant) { product.variants.first }
before { variant.on_hand = 2 }
context 'when there are excluded_items_ids' do
let(:excluded_items_ids) { [variant.id] }
context 'and supplier_ids is []' do
let(:supplier_ids) { [] }
it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is nil' do
let(:supplier_ids) { nil }
it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is set' do
it 'does not update the on_hand of the excluded items' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(2)
end
it 'updates the on_hand of the non-excluded items' do
non_excluded_variant = create(
:variant,
product: variant.product
)
non_excluded_variant.on_hand = 3
products_reset.reset(supplier_ids)
expect(non_excluded_variant.reload.on_hand).to eq(0)
end
end
end
context 'when there are no excluded_items_ids' do
let(:excluded_items_ids) { [] }
context 'and supplier_ids is []' do
let(:supplier_ids) { [] }
it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is nil' do
let(:supplier_ids) { nil }
it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is not nil' do
it 'sets all on_hand to 0' do
updated_records_count = products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(0)
expect(updated_records_count).to eq(1)
end
context 'and there is an unresetable variant' do
before do
variant.stock_items = [] # this makes variant.on_hand raise an error
end
it 'returns correct number of resetted variants' do
expect { products_reset.reset(supplier_ids) }.to raise_error RuntimeError
end
end
end
end
context 'when excluded_items_ids is nil' do
let(:excluded_items_ids) { nil }
context 'and supplier_ids is []' do
let(:supplier_ids) { [] }
it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is nil' do
let(:supplier_ids) { nil }
it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is nil' do
it 'sets all on_hand to 0' do
products_reset.reset(supplier_ids)
expect(variant.reload.on_hand).to eq(0)
end
end
end
end
end

View File

@@ -37,7 +37,7 @@ module ProductImport
)
end
let(:reset_stock_strategy) { instance_double(ProductsResetStrategy) }
let(:reset_stock_strategy) { instance_double(Catalog::ProductImport::ProductsResetStrategy) }
before do
allow(entry_processor)