Skip to content

Commit

Permalink
Add after_save call to private method #update_inventory, using saved_…
Browse files Browse the repository at this point in the history
…change_to? hook to update associated Item inventory
  • Loading branch information
Brennan Ayers committed May 25, 2019
1 parent 9377d35 commit dc2086c
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/models/order_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,24 @@ class OrderItem < ApplicationRecord
validates_presence_of :quantity, :price
validates_inclusion_of :fulfilled, in: [true, false]

after_save :update_inventory

def sub_total
quantity * price
end

private

def update_inventory
if saved_change_to_fulfilled?

if fulfilled
new_inventory = item.inventory - quantity
item.update(inventory: new_inventory)
else
new_inventory = item.inventory + quantity
item.update(inventory: new_inventory)
end
end
end
end

0 comments on commit dc2086c

Please sign in to comment.