mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-21 05:09:15 +00:00
Refactor: move variant duplication to model
I tried to avoid it but rubocop made me move it. I think maybe it will need to go into a concern or service class later, but hopefully it's ok here for now.
This commit is contained in:
@@ -116,15 +116,7 @@ module Admin
|
||||
status = :ok
|
||||
|
||||
begin
|
||||
variant = source_variant.dup #may need a VariantDuplicator like producs?
|
||||
variant.price = source_variant.price
|
||||
variant.save!
|
||||
variant.source_variants << source_variant
|
||||
# Owner is my enterprise which has permission to create sourced variants from that supplier
|
||||
variant.owner_id = EnterpriseRelationship.permitted_by(source_variant.supplier).permitting(spree_current_user.enterprises).with_permission(:create_sourced_variants).pluck(:child_id).first
|
||||
variant.on_demand = source_variant.on_demand
|
||||
variant.on_hand = source_variant.on_hand
|
||||
variant.save!
|
||||
variant = source_variant.create_sourced_variant(spree_current_user)
|
||||
|
||||
flash.now[:success] = t('.success')
|
||||
variant_index = "-#{variant.id}"
|
||||
|
||||
@@ -273,6 +273,24 @@ module Spree
|
||||
@on_hand_desired = ActiveModel::Type::Integer.new.cast(val)
|
||||
end
|
||||
|
||||
# Clone this variant, retaining a 'source' link to it
|
||||
def create_sourced_variant(user)
|
||||
# Owner is my enterprise which has permission to create sourced variants from that supplier
|
||||
owner_id = EnterpriseRelationship.permitted_by(supplier).permitting(user.enterprises)
|
||||
.with_permission(:create_sourced_variants)
|
||||
.pick(:child_id)
|
||||
|
||||
dup.tap do |variant|
|
||||
variant.price = price
|
||||
variant.save!
|
||||
variant.source_variants = [self]
|
||||
variant.owner_id = owner_id
|
||||
variant.on_demand = on_demand
|
||||
variant.on_hand = on_hand
|
||||
variant.save!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def check_currency
|
||||
|
||||
@@ -1005,4 +1005,34 @@ RSpec.describe Spree::Variant do
|
||||
expect(variant.unit_presentation).to eq "My display"
|
||||
end
|
||||
end
|
||||
|
||||
describe "#create_sourced_variant" do
|
||||
let(:user) { create(:user, enterprises: [enterprise]) }
|
||||
let(:supplier) { variant.supplier }
|
||||
let(:enterprise) { create(:enterprise) }
|
||||
|
||||
context "with create_sourced_variants permissions on supplier" do
|
||||
let!(:enterprise_relationship) {
|
||||
create(:enterprise_relationship,
|
||||
parent: supplier,
|
||||
child: enterprise,
|
||||
permissions_list: [:create_sourced_variants])
|
||||
}
|
||||
|
||||
it "clones the variant, retaining a link to the source" do
|
||||
variant.price = 10.95
|
||||
variant.save!
|
||||
variant.on_demand = false
|
||||
variant.on_hand = 5
|
||||
|
||||
sourced_variant = variant.create_sourced_variant(user)
|
||||
|
||||
expect(sourced_variant.source_variants).to eq [variant]
|
||||
expect(sourced_variant.owner).to eq enterprise
|
||||
expect(sourced_variant.price).to eq 10.95
|
||||
expect(sourced_variant.on_demand).to eq false
|
||||
expect(sourced_variant.on_hand).to eq 5
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user