inventory.rb 594 B

12345678910111213141516171819202122
  1. class Inventory < ActiveRecord::Base
  2. validates :item_id, presence: true
  3. def self.update_inv(item, amount, char)
  4. i = Inventory.where(char_id: char.id).find_by(item_id: item.id)
  5. if i.nil? && amount > 0
  6. Inventory.create(char_id: char.id, amount: amount, item_id: item.id)
  7. elsif i && i.amount + amount > 0
  8. i.update(amount: i.amount + amount)
  9. i.reload
  10. elsif i && i.amount + amount == 0
  11. i.delete
  12. true
  13. else
  14. error_embed(
  15. "Cannot update inventory!",
  16. "#{char.name} does not have enough #{item.name}"
  17. )
  18. end
  19. end
  20. end