diff --git a/app/controllers/audits_controller.rb b/app/controllers/audits_controller.rb index 50717d2cde..9f40d068e1 100644 --- a/app/controllers/audits_controller.rb +++ b/app/controllers/audits_controller.rb @@ -38,8 +38,8 @@ def finalize increasing_adjustment, decreasing_adjustment = @audit.adjustment.split_difference ActiveRecord::Base.transaction do - @audit.storage_location.increase_inventory(increasing_adjustment.line_item_hashes) - @audit.storage_location.decrease_inventory(decreasing_adjustment.line_item_hashes) + @audit.storage_location.increase_inventory(increasing_adjustment.line_item_values) + @audit.storage_location.decrease_inventory(decreasing_adjustment.line_item_values) AuditEvent.publish(@audit) end @audit.finalized! diff --git a/app/models/concerns/itemizable.rb b/app/models/concerns/itemizable.rb index 2a9db88332..e8aa83a198 100644 --- a/app/models/concerns/itemizable.rb +++ b/app/models/concerns/itemizable.rb @@ -102,7 +102,7 @@ def total_quantity line_items.total end - def line_item_hashes + def line_item_values line_items.map do |l| item = Item.find(l.item_id) { item_id: item.id, name: item.name, quantity: l.quantity, active: item.active }.with_indifferent_access @@ -111,9 +111,11 @@ def line_item_hashes # TODO: I'm not sure if this should just be removed or leave as a warning for now? def to_a - Rails.logger.error("Calling Itemizable#to_a is deprecated. Use Itemizable#line_item_hashes instead.") - Rails.logger.error("Called on #{inspect} by #{caller}") - line_item_hashes + line_item_values unless Flipper.enabled?(:deprecate_to_a) + + Rails.logger.warn "Called #to_a on an Itemizable #{inspect}." + Rails.logger.warn caller.join("\n") + raise StandardError, "Calling to_a on an Itemizable is deprecated. Use #line_item_values instead." end private diff --git a/app/services/adjustment_create_service.rb b/app/services/adjustment_create_service.rb index 67820a2dd8..6e71909c78 100644 --- a/app/services/adjustment_create_service.rb +++ b/app/services/adjustment_create_service.rb @@ -25,8 +25,8 @@ def call # Split into positive and negative portions. # N.B. -- THIS CHANGES THE ORIGINAL LINE ITEMS ON @adjustment DO **NOT** RESAVE AS THAT WILL CHANGE ANY NEGATIVE LINE ITEMS ON THE ADJUSTMENT TO POSITIVES increasing_adjustment, decreasing_adjustment = @adjustment.split_difference - @adjustment.storage_location.increase_inventory(increasing_adjustment.line_item_hashes) - @adjustment.storage_location.decrease_inventory(decreasing_adjustment.line_item_hashes) + @adjustment.storage_location.increase_inventory(increasing_adjustment.line_item_values) + @adjustment.storage_location.decrease_inventory(decreasing_adjustment.line_item_values) rescue InsufficientAllotment => e @adjustment.errors.add(:base, e.message) raise e diff --git a/app/services/allocate_kit_inventory_service.rb b/app/services/allocate_kit_inventory_service.rb index 161a3265fe..f38c4e4c9e 100644 --- a/app/services/allocate_kit_inventory_service.rb +++ b/app/services/allocate_kit_inventory_service.rb @@ -78,7 +78,7 @@ def set_error(error) end def kit_content - kit.line_item_hashes.map do |item| + kit.line_item_values.map do |item| item.merge({ quantity: item[:quantity] * increase_by }) diff --git a/app/services/deallocate_kit_inventory_service.rb b/app/services/deallocate_kit_inventory_service.rb index 22378a21b2..e26fb1fbc7 100644 --- a/app/services/deallocate_kit_inventory_service.rb +++ b/app/services/deallocate_kit_inventory_service.rb @@ -83,7 +83,7 @@ def set_error(error) end def kit_content - kit.line_item_hashes.map do |item| + kit.line_item_values.map do |item| item.merge({ quantity: item[:quantity] * decrease_by }) diff --git a/app/services/distribution_create_service.rb b/app/services/distribution_create_service.rb index 8a4c72fa31..d98a848a0a 100644 --- a/app/services/distribution_create_service.rb +++ b/app/services/distribution_create_service.rb @@ -14,7 +14,7 @@ def call DistributionEvent.publish(distribution) - distribution.storage_location.decrease_inventory(distribution.line_item_hashes) + distribution.storage_location.decrease_inventory(distribution.line_item_values) distribution.reload @request&.update!(distribution_id: distribution.id, status: 'fulfilled') send_notification if distribution.partner&.send_reminders diff --git a/app/services/distribution_destroy_service.rb b/app/services/distribution_destroy_service.rb index db1c8c064d..f788bd4bdb 100644 --- a/app/services/distribution_destroy_service.rb +++ b/app/services/distribution_destroy_service.rb @@ -7,7 +7,7 @@ def call perform_distribution_service do DistributionDestroyEvent.publish(distribution) distribution.destroy! - distribution.storage_location.increase_inventory(distribution.line_item_hashes) + distribution.storage_location.increase_inventory(distribution.line_item_values) end end end diff --git a/app/services/distribution_update_service.rb b/app/services/distribution_update_service.rb index b1692c9fa1..5889d2a622 100644 --- a/app/services/distribution_update_service.rb +++ b/app/services/distribution_update_service.rb @@ -2,7 +2,7 @@ class DistributionUpdateService < DistributionService def initialize(old_distribution, new_distribution_params) @distribution = old_distribution @params = new_distribution_params - @old_line_items = old_distribution.line_item_hashes + @old_line_items = old_distribution.line_item_values end def call @@ -27,7 +27,7 @@ def resend_notification? end def distribution_content - @distribution_content ||= DistributionContentChangeService.new(@old_line_items, distribution.line_item_hashes).call + @distribution_content ||= DistributionContentChangeService.new(@old_line_items, distribution.line_item_values).call end private diff --git a/app/services/donation_create_service.rb b/app/services/donation_create_service.rb index 4ed78aa653..56d470dcf9 100644 --- a/app/services/donation_create_service.rb +++ b/app/services/donation_create_service.rb @@ -2,7 +2,7 @@ module DonationCreateService class << self def call(donation) if donation.save - donation.storage_location.increase_inventory(donation.line_item_hashes) + donation.storage_location.increase_inventory(donation.line_item_values) DonationEvent.publish(donation) end end diff --git a/app/services/donation_destroy_service.rb b/app/services/donation_destroy_service.rb index a861e96f8a..57f6092424 100644 --- a/app/services/donation_destroy_service.rb +++ b/app/services/donation_destroy_service.rb @@ -10,7 +10,7 @@ def call ActiveRecord::Base.transaction do organization = Organization.find(organization_id) donation = organization.donations.find(donation_id) - donation.storage_location.decrease_inventory(donation.line_item_hashes) + donation.storage_location.decrease_inventory(donation.line_item_values) DonationDestroyEvent.publish(donation) donation.destroy! end diff --git a/app/services/itemizable_update_service.rb b/app/services/itemizable_update_service.rb index f638891c12..7ef851f69f 100644 --- a/app/services/itemizable_update_service.rb +++ b/app/services/itemizable_update_service.rb @@ -36,13 +36,13 @@ def self.call(itemizable:, type: :increase, params: {}, event_class: nil) # @param to_location [StorageLocation] def self.update_storage_location(itemizable:, apply_change_method:, undo_change_method:, params:, from_location:, to_location:) - from_location.public_send(undo_change_method, itemizable.line_item_hashes) + from_location.public_send(undo_change_method, itemizable.line_item_values) # Delete the line items -- they'll be replaced later itemizable.line_items.delete_all # Update the current model with the new parameters itemizable.update!(params) itemizable.reload # Apply the new changes to the storage location inventory - to_location.public_send(apply_change_method, itemizable.line_item_hashes) + to_location.public_send(apply_change_method, itemizable.line_item_values) end end diff --git a/app/services/purchase_create_service.rb b/app/services/purchase_create_service.rb index b6c3783b08..fdfa2b85d3 100644 --- a/app/services/purchase_create_service.rb +++ b/app/services/purchase_create_service.rb @@ -2,7 +2,7 @@ class PurchaseCreateService class << self def call(purchase) if purchase.save - purchase.storage_location.increase_inventory(purchase.line_item_hashes) + purchase.storage_location.increase_inventory(purchase.line_item_values) PurchaseEvent.publish(purchase) end end diff --git a/app/services/purchase_destroy_service.rb b/app/services/purchase_destroy_service.rb index 7d437c68fe..c32ca02921 100644 --- a/app/services/purchase_destroy_service.rb +++ b/app/services/purchase_destroy_service.rb @@ -2,7 +2,7 @@ class PurchaseDestroyService class << self def call(purchase) ActiveRecord::Base.transaction do - purchase.storage_location.decrease_inventory(purchase.line_item_hashes) + purchase.storage_location.decrease_inventory(purchase.line_item_values) PurchaseDestroyEvent.publish(purchase) purchase.destroy! end diff --git a/app/services/transfer_create_service.rb b/app/services/transfer_create_service.rb index 31a9cbe331..c4cb5cc12c 100644 --- a/app/services/transfer_create_service.rb +++ b/app/services/transfer_create_service.rb @@ -4,8 +4,8 @@ def call(transfer) if transfer.valid? ActiveRecord::Base.transaction do transfer.save - transfer.from.decrease_inventory(transfer.line_item_hashes) - transfer.to.increase_inventory(transfer.line_item_hashes) + transfer.from.decrease_inventory(transfer.line_item_values) + transfer.to.increase_inventory(transfer.line_item_values) TransferEvent.publish(transfer) end else diff --git a/app/services/transfer_destroy_service.rb b/app/services/transfer_destroy_service.rb index 5992649aa5..42e5ad786a 100644 --- a/app/services/transfer_destroy_service.rb +++ b/app/services/transfer_destroy_service.rb @@ -24,7 +24,7 @@ def transfer end def revert_inventory_transfer! - transfer.to.decrease_inventory(transfer.line_item_hashes) - transfer.from.increase_inventory(transfer.line_item_hashes) + transfer.to.decrease_inventory(transfer.line_item_values) + transfer.from.increase_inventory(transfer.line_item_values) end end diff --git a/spec/factories/donations.rb b/spec/factories/donations.rb index cb339aa3f3..3a11978fb1 100644 --- a/spec/factories/donations.rb +++ b/spec/factories/donations.rb @@ -58,7 +58,7 @@ end after(:create) do |instance, evaluator| - evaluator.storage_location.increase_inventory(instance.line_item_hashes) + evaluator.storage_location.increase_inventory(instance.line_item_values) end end end diff --git a/spec/factories/purchases.rb b/spec/factories/purchases.rb index 0ec08f46f0..4ca7ff9272 100644 --- a/spec/factories/purchases.rb +++ b/spec/factories/purchases.rb @@ -47,7 +47,7 @@ end after(:create) do |instance, evaluator| - evaluator.storage_location.increase_inventory(instance.line_item_hashes) + evaluator.storage_location.increase_inventory(instance.line_item_values) end end end diff --git a/spec/factories/transfers.rb b/spec/factories/transfers.rb index 263ba91c4a..5b71cb46af 100644 --- a/spec/factories/transfers.rb +++ b/spec/factories/transfers.rb @@ -39,7 +39,7 @@ end after(:create) do |instance, evaluator| - evaluator.from.increase_inventory(instance.line_item_hashes) + evaluator.from.increase_inventory(instance.line_item_values) end end end diff --git a/spec/models/storage_location_spec.rb b/spec/models/storage_location_spec.rb index fda088d404..4d978cc895 100644 --- a/spec/models/storage_location_spec.rb +++ b/spec/models/storage_location_spec.rb @@ -72,7 +72,7 @@ it "increases inventory quantities from an itemizable object" do expect do - subject.increase_inventory(donation.line_item_hashes) + subject.increase_inventory(donation.line_item_values) end.to change { subject.size }.by(66) end end @@ -83,7 +83,7 @@ it "creates those new inventory items in the storage location" do expect do - subject.increase_inventory(donation_with_new_items.line_item_hashes) + subject.increase_inventory(donation_with_new_items.line_item_values) end.to change { subject.inventory_items.count }.by(1) end end @@ -94,7 +94,7 @@ it "re-activates the item as part of the creation process" do expect do - subject.increase_inventory(donation_with_inactive_item.line_item_hashes) + subject.increase_inventory(donation_with_inactive_item.line_item_values) end.to change { subject.inventory_items.count }.by(1) .and change { Item.count }.by(1) end @@ -108,7 +108,7 @@ it "decreases inventory quantities from an itemizable object" do storage_location = create(:storage_location, :with_items, item_quantity: 100, item: item, organization: @organization) expect do - storage_location.decrease_inventory(distribution.line_item_hashes) + storage_location.decrease_inventory(distribution.line_item_values) end.to change { storage_location.size }.by(-66) end @@ -118,7 +118,7 @@ it "gives informative errors" do storage_location = create(:storage_location, :with_items, item_quantity: 10, item: item, organization: @organization) expect do - storage_location.decrease_inventory(distribution_but_too_much.line_item_hashes).errors + storage_location.decrease_inventory(distribution_but_too_much.line_item_values).errors end.to raise_error(Errors::InsufficientAllotment) end @@ -126,7 +126,7 @@ storage_location = create(:storage_location, :with_items, item_quantity: 10, item: item, organization: @organization) starting_size = storage_location.size begin - storage_location.decrease_inventory(distribution.line_item_hashes) + storage_location.decrease_inventory(distribution.line_item_values) rescue Errors::InsufficientAllotment end storage_location.reload diff --git a/spec/services/distribution_destroy_service_spec.rb b/spec/services/distribution_destroy_service_spec.rb index 77ba37322a..79c34a51cb 100644 --- a/spec/services/distribution_destroy_service_spec.rb +++ b/spec/services/distribution_destroy_service_spec.rb @@ -43,7 +43,7 @@ end before do allow(distribution).to receive(:storage_location).and_return(fake_storage_location) - allow(distribution).to receive(:line_item_hashes).and_return(fake_items) + allow(distribution).to receive(:line_item_values).and_return(fake_items) allow(fake_storage_location).to receive(:increase_inventory) end @@ -106,7 +106,7 @@ before do allow(distribution).to receive(:storage_location).and_return(fake_storage_location) - allow(distribution).to receive(:line_item_hashes).and_return(fake_insufficient_items) + allow(distribution).to receive(:line_item_values).and_return(fake_insufficient_items) allow(fake_storage_location).to receive(:increase_inventory) .with(fake_insufficient_items) .and_raise(fake_insufficient_allotment_error) diff --git a/spec/services/donation_destroy_service_spec.rb b/spec/services/donation_destroy_service_spec.rb index fdaaa654c7..f632df7dcd 100644 --- a/spec/services/donation_destroy_service_spec.rb +++ b/spec/services/donation_destroy_service_spec.rb @@ -67,7 +67,7 @@ allow(fake_organization_donations).to receive(:find) .with(donation_id) .and_return(fake_donation) - allow(fake_donation).to receive(:line_item_hashes).and_return(fake_insufficient_items) + allow(fake_donation).to receive(:line_item_values).and_return(fake_insufficient_items) allow(fake_storage_location).to receive(:decrease_inventory) .with(fake_insufficient_items) .and_raise(fake_insufficient_allotment_error) @@ -109,7 +109,7 @@ allow(fake_organization_donations).to receive(:find) .with(donation_id) .and_return(fake_donation) - allow(fake_donation).to receive(:line_item_hashes).and_return(fake_insufficient_items) + allow(fake_donation).to receive(:line_item_values).and_return(fake_insufficient_items) allow(fake_storage_location).to receive(:decrease_inventory).with(fake_insufficient_items) allow(fake_donation).to receive(:destroy!).and_raise('boom') end diff --git a/spec/services/transfer_destroy_service_spec.rb b/spec/services/transfer_destroy_service_spec.rb index 246ca6439a..cd4d423b29 100644 --- a/spec/services/transfer_destroy_service_spec.rb +++ b/spec/services/transfer_destroy_service_spec.rb @@ -29,7 +29,7 @@ allow(transfer).to receive(:from).and_return(fake_from) allow(transfer).to receive(:to).and_return(fake_to) allow(transfer).to receive(:destroy!) - allow(transfer).to receive(:line_item_hashes).and_return(fake_items) + allow(transfer).to receive(:line_item_values).and_return(fake_items) # Now that that the `transfer.from` and `transfer.to` is stubbed # to return the doubles of StorageLocation, we must program them @@ -73,7 +73,7 @@ let(:fake_error) { Errors::InsufficientAllotment.new('msg') } before do - allow(transfer).to receive(:line_item_hashes).and_return(fake_items) + allow(transfer).to receive(:line_item_values).and_return(fake_items) allow(fake_from).to receive(:increase_inventory).with(fake_items).and_raise(fake_error) end @@ -87,8 +87,8 @@ context 'because undoing the transfer inventory changes by decreasing the inventory of `to` failed' do let(:fake_error) { Errors::InsufficientAllotment.new('random-error') } before do - allow(transfer).to receive(:line_item_hashes).and_return(fake_items) - allow(fake_to).to receive(:decrease_inventory).with(transfer.line_item_hashes).and_raise(fake_error) + allow(transfer).to receive(:line_item_values).and_return(fake_items) + allow(fake_to).to receive(:decrease_inventory).with(transfer.line_item_values).and_raise(fake_error) end it 'should return a OpenStruct with the raised error' do