Bulk update OC variant changes in OC form

This commit is contained in:
Kristina Lim
2019-05-28 19:37:53 +08:00
parent bc6f14105e
commit ea8d189d6c
3 changed files with 87 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
class ExchangeVariantBulkUpdater
def initialize(exchange)
@exchange = exchange
end
def update!(variant_ids)
sanitized_variant_ids = variant_ids.map(&:to_i).uniq
existing_variant_ids = @exchange.variant_ids
disassociate_variants!(existing_variant_ids - sanitized_variant_ids)
associate_variants!(sanitized_variant_ids - existing_variant_ids)
uncache_variant_associations
end
private
def disassociate_variants!(variant_ids)
return if variant_ids.blank?
@exchange.exchange_variants.where(variant_id: variant_ids).delete_all
end
def associate_variants!(variant_ids)
return if variant_ids.blank?
new_exchange_variants = variant_ids.map do |variant_id|
ExchangeVariant.new(exchange_id: @exchange.id, variant_id: variant_id)
end
ExchangeVariant.import!(new_exchange_variants)
end
def uncache_variant_associations
@exchange.exchange_variants.reset
@exchange.variants.proxy_association.reset
end
end

View File

@@ -66,10 +66,13 @@ module OpenFoodNetwork
def add_exchange(sender_id, receiver_id, incoming, attrs = {})
attrs = attrs.reverse_merge(sender_id: sender_id, receiver_id: receiver_id, incoming: incoming)
variant_ids = attrs.delete :variant_ids
exchange = @order_cycle.exchanges.build attrs
if manages_coordinator?
exchange.save!
ExchangeVariantBulkUpdater.new(exchange).update!(variant_ids) unless variant_ids.nil?
@touched_exchanges << exchange
end
end
@@ -85,7 +88,12 @@ module OpenFoodNetwork
end
if permission_for exchange
variant_ids = attrs.delete :variant_ids
exchange.update_attributes!(attrs)
ExchangeVariantBulkUpdater.new(exchange).update!(variant_ids) unless variant_ids.nil?
@touched_exchanges << exchange
end
end

View File

@@ -0,0 +1,43 @@
require "spec_helper"
describe ExchangeVariantBulkUpdater do
let!(:first_variant) { create(:variant) }
let!(:second_variant) { create(:variant) }
let!(:third_variant) { create(:variant) }
it "associates new variants to the exchange" do
exchange = create(:exchange)
described_class.new(exchange).update!([first_variant.id, second_variant.id])
# Check association cache.
expect(exchange.variants).to include(first_variant)
expect(exchange.variants).to include(second_variant)
# Check if changes are actually persisted.
exchange.reload
expect(exchange.variants).to include(first_variant)
expect(exchange.variants).to include(second_variant)
end
it "disassociates variants from the exchange" do
exchange = create(:exchange, variant_ids: [first_variant.id, second_variant.id])
described_class.new(exchange).update!([first_variant.id, third_variant.id])
# Check association cache.
expect(exchange.variants).to include(first_variant)
expect(exchange.variants).to include(third_variant)
# Check if changes are actually persisted.
exchange.reload
expect(exchange.variants).to include(first_variant)
expect(exchange.variants).to include(third_variant)
described_class.new(exchange).update!([])
# Check association cache.
expect(exchange.variants).to be_blank
# Check if changes are actually persisted.
exchange.reload
expect(exchange.variants).to be_blank
end
end