Skip to content

Instantly share code, notes, and snippets.

@phensalves
Last active August 29, 2015 14:27
Show Gist options
  • Save phensalves/edaf2721641fae7930cb to your computer and use it in GitHub Desktop.
Save phensalves/edaf2721641fae7930cb to your computer and use it in GitHub Desktop.
class Product < ActiveRecord::Base
validates :name, :estoque_id
has_many :log
end
class User < ActiveRecord::Base
validates :name
has_many :log
end
class Log < ActiveRecord::Base
validates :product_id, :estoque_operation_id, :user_id
belongs_to :user
has_many :product
end
class Estoque < ActiveRecord::Base
validates :product_id, :quantidade
has_many :product
has_many :log
end
class EstoqueOperation < ActiveModel
validates :quantidade_retirada, :product_id
end
class EstoqueController < ApplicationController
def update
find_product
product = Product.first
estoque = Estoque.first
estoque_operation = EstoqueOperation.first
if estoque.quantidade < estoque_operation.quantidade_retirada
raise :excede_estoque, "Você está tentando retirar mais do que possui"
end
estoque.quantidade -= estoque_operation.quantidade_retirada
product.new(:name, :estoque_id).log.create(:product_id, estoque_operation[:quantidade_retirada], :user_id, :created_at)
end
private
def find_product
@product = Product.where(id: product[:id]).first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment