Skip to content

Commit

Permalink
fix: renames line_item_hashes to line_item_values
Browse files Browse the repository at this point in the history
line_item_hashes reflects the return type of
the value rather than its meaning.

line_item_values better reflects the meaning
of the function.
  • Loading branch information
elasticspoon committed Jan 18, 2024
1 parent 8a02544 commit 56a033c
Show file tree
Hide file tree
Showing 22 changed files with 43 additions and 41 deletions.
4 changes: 2 additions & 2 deletions app/controllers/audits_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
10 changes: 6 additions & 4 deletions app/models/concerns/itemizable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/services/adjustment_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/services/allocate_kit_inventory_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
2 changes: 1 addition & 1 deletion app/services/deallocate_kit_inventory_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
2 changes: 1 addition & 1 deletion app/services/distribution_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/services/distribution_destroy_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions app/services/distribution_update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/services/donation_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/services/donation_destroy_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/services/itemizable_update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/services/purchase_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/services/purchase_destroy_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/services/transfer_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/services/transfer_destroy_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/factories/donations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/purchases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/transfers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions spec/models/storage_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -118,15 +118,15 @@
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

it "does not change inventory quantities if there is an error" do
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
Expand Down
4 changes: 2 additions & 2 deletions spec/services/distribution_destroy_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions spec/services/donation_destroy_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/services/transfer_destroy_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 56a033c

Please sign in to comment.