mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-10 03:30:22 +00:00
Move VariantOverrides class to app/services
This commit is contained in:
32
app/services/variant_overrides.rb
Normal file
32
app/services/variant_overrides.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class VariantOverrides
|
||||
def initialize(line_items:, distributor_ids:)
|
||||
@line_items = line_items
|
||||
@distributor_ids = distributor_ids
|
||||
end
|
||||
|
||||
def indexed
|
||||
variant_overrides.each_with_object(hash_of_hashes) do |variant_override, indexed|
|
||||
indexed[variant_override.hub_id][variant_override.variant] = variant_override
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :line_items, :distributor_ids
|
||||
|
||||
def variant_overrides
|
||||
VariantOverride
|
||||
.joins(:variant)
|
||||
.preload(:variant)
|
||||
.where(
|
||||
hub_id: distributor_ids,
|
||||
variant_id: line_items.select(:variant_id)
|
||||
)
|
||||
end
|
||||
|
||||
def hash_of_hashes
|
||||
Hash.new { |h, k| h[k] = {} }
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,4 @@
|
||||
require "open_food_network/reports/line_items"
|
||||
require "open_food_network/reports/variant_overrides"
|
||||
require "open_food_network/orders_and_fulfillments_report/supplier_totals_report"
|
||||
require "open_food_network/orders_and_fulfillments_report/supplier_totals_by_distributor_report"
|
||||
require "open_food_network/orders_and_fulfillments_report/distributor_totals_by_supplier_report"
|
||||
@@ -102,7 +101,7 @@ module OpenFoodNetwork
|
||||
|
||||
def report_variant_overrides
|
||||
@report_variant_overrides ||=
|
||||
Reports::VariantOverrides.new(
|
||||
VariantOverrides.new(
|
||||
line_items: order_permissions.visible_line_items,
|
||||
distributor_ids: report_line_items.orders.result.select('DISTINCT distributor_id'),
|
||||
).indexed
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module OpenFoodNetwork
|
||||
module Reports
|
||||
class VariantOverrides
|
||||
def initialize(line_items:, distributor_ids:)
|
||||
@line_items = line_items
|
||||
@distributor_ids = distributor_ids
|
||||
end
|
||||
|
||||
def indexed
|
||||
variant_overrides.each_with_object(hash_of_hashes) do |variant_override, indexed|
|
||||
indexed[variant_override.hub_id][variant_override.variant] = variant_override
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :line_items, :distributor_ids
|
||||
|
||||
def variant_overrides
|
||||
VariantOverride
|
||||
.joins(:variant)
|
||||
.preload(:variant)
|
||||
.where(
|
||||
hub_id: distributor_ids,
|
||||
variant_id: line_items.select(:variant_id)
|
||||
)
|
||||
end
|
||||
|
||||
def hash_of_hashes
|
||||
Hash.new { |h, k| h[k] = {} }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,73 +0,0 @@
|
||||
require 'spec_helper'
|
||||
require 'open_food_network/reports/variant_overrides'
|
||||
|
||||
module OpenFoodNetwork::Reports
|
||||
describe VariantOverrides do
|
||||
subject(:variant_overrides) do
|
||||
described_class.new(
|
||||
line_items: order.line_items,
|
||||
distributor_ids: [distributor.id],
|
||||
)
|
||||
end
|
||||
|
||||
let(:distributor) { create(:distributor_enterprise) }
|
||||
let(:order) do
|
||||
create(:completed_order_with_totals, line_items_count: 1,
|
||||
distributor: distributor)
|
||||
end
|
||||
let(:line_item) { order.line_items.first }
|
||||
let!(:variant_override) do
|
||||
create(
|
||||
:variant_override,
|
||||
hub: vo_distributor,
|
||||
variant: vo_variant,
|
||||
)
|
||||
end
|
||||
|
||||
describe '#indexed' do
|
||||
let(:result) { variant_overrides.indexed }
|
||||
|
||||
context 'when variant overrides exist for variants of specified line items' do
|
||||
let(:vo_variant) { line_item.variant }
|
||||
|
||||
context 'when variant overrides apply to one of the specified distributors' do
|
||||
let(:vo_distributor) { distributor }
|
||||
|
||||
it 'they are included in the mapping' do
|
||||
expect(result).to eq(
|
||||
distributor.id => { line_item.variant => variant_override }
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when variant overrides don\'t apply to one of the specified distributors' do
|
||||
let(:vo_distributor) { create(:distributor_enterprise) }
|
||||
|
||||
it 'they are not included in the mapping' do
|
||||
expect(result).to eq({})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when variant overrides exist for other variants' do
|
||||
let(:vo_variant) { create(:variant) }
|
||||
|
||||
context 'when variant overrides apply to one of the specified distributors' do
|
||||
let(:vo_distributor) { distributor }
|
||||
|
||||
it 'they are not included in the mapping' do
|
||||
expect(result).to eq({})
|
||||
end
|
||||
end
|
||||
|
||||
context 'when variant overrides don\'t apply to one of the specified distributors' do
|
||||
let(:vo_distributor) { create(:distributor_enterprise) }
|
||||
|
||||
it 'they are not included in the mapping' do
|
||||
expect(result).to eq({})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
70
spec/services/variant_overrides_spec.rb
Normal file
70
spec/services/variant_overrides_spec.rb
Normal file
@@ -0,0 +1,70 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe VariantOverrides do
|
||||
subject(:variant_overrides) do
|
||||
described_class.new(
|
||||
line_items: order.line_items,
|
||||
distributor_ids: [distributor.id],
|
||||
)
|
||||
end
|
||||
|
||||
let(:distributor) { create(:distributor_enterprise) }
|
||||
let(:order) do
|
||||
create(:completed_order_with_totals, line_items_count: 1,
|
||||
distributor: distributor)
|
||||
end
|
||||
let(:line_item) { order.line_items.first }
|
||||
let!(:variant_override) do
|
||||
create(
|
||||
:variant_override,
|
||||
hub: vo_distributor,
|
||||
variant: vo_variant,
|
||||
)
|
||||
end
|
||||
|
||||
describe '#indexed' do
|
||||
let(:result) { variant_overrides.indexed }
|
||||
|
||||
context 'when variant overrides exist for variants of specified line items' do
|
||||
let(:vo_variant) { line_item.variant }
|
||||
|
||||
context 'when variant overrides apply to one of the specified distributors' do
|
||||
let(:vo_distributor) { distributor }
|
||||
|
||||
it 'they are included in the mapping' do
|
||||
expect(result).to eq(
|
||||
distributor.id => { line_item.variant => variant_override }
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when variant overrides don\'t apply to one of the specified distributors' do
|
||||
let(:vo_distributor) { create(:distributor_enterprise) }
|
||||
|
||||
it 'they are not included in the mapping' do
|
||||
expect(result).to eq({})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when variant overrides exist for other variants' do
|
||||
let(:vo_variant) { create(:variant) }
|
||||
|
||||
context 'when variant overrides apply to one of the specified distributors' do
|
||||
let(:vo_distributor) { distributor }
|
||||
|
||||
it 'they are not included in the mapping' do
|
||||
expect(result).to eq({})
|
||||
end
|
||||
end
|
||||
|
||||
context 'when variant overrides don\'t apply to one of the specified distributors' do
|
||||
let(:vo_distributor) { create(:distributor_enterprise) }
|
||||
|
||||
it 'they are not included in the mapping' do
|
||||
expect(result).to eq({})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user