Modify interface of VariantOverridesIndexed#indexed

Stop using keyword args and accept variant_ids instead of line_items
This commit is contained in:
Rob H
2020-04-13 22:11:58 +10:00
parent 6820919552
commit dcdd3f2444
3 changed files with 11 additions and 20 deletions

View File

@@ -1,8 +1,8 @@
# frozen_string_literal: true
class VariantOverridesIndexed
def initialize(line_items:, distributor_ids:)
@line_items = line_items
def initialize(variant_ids, distributor_ids)
@variant_ids = variant_ids
@distributor_ids = distributor_ids
end
@@ -14,7 +14,7 @@ class VariantOverridesIndexed
private
attr_reader :line_items, :distributor_ids
attr_reader :variant_ids, :distributor_ids
def variant_overrides
VariantOverride
@@ -22,7 +22,7 @@ class VariantOverridesIndexed
.preload(:variant)
.where(
hub_id: distributor_ids,
variant_id: line_items.select(:variant_id)
variant_id: variant_ids,
)
end

View File

@@ -102,8 +102,8 @@ module OpenFoodNetwork
def report_variant_overrides
@report_variant_overrides ||=
VariantOverridesIndexed.new(
line_items: order_permissions.visible_line_items,
distributor_ids: report_line_items.orders.result.select('DISTINCT distributor_id'),
order_permissions.visible_line_items.select('DISTINCT variant_id'),
report_line_items.orders.result.select('DISTINCT distributor_id'),
).indexed
end
end

View File

@@ -1,19 +1,10 @@
require 'spec_helper'
describe VariantOverridesIndexed do
subject(:variant_overrides) do
described_class.new(
line_items: order.line_items,
distributor_ids: [distributor.id],
)
end
subject(:variant_overrides) { described_class.new([variant.id],[distributor.id]) }
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) { create(:variant) }
let!(:variant_override) do
create(
:variant_override,
@@ -25,15 +16,15 @@ describe VariantOverridesIndexed do
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 exist for variants of specified variants' do
let(:vo_variant) { 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 }
distributor.id => { variant => variant_override }
)
end
end