Modelo Store
unknown
ruby
4 years ago
1.0 kB
15
Indexable
class Store < ApplicationRecord
resourcify
belongs_to :user
belongs_to :branch_office
has_many :store_orders
has_many :purcharse_orders
has_many :movement_products
has_many :sale_documents
has_many :store_products
validates :user_id, presence: true
validates :address, presence: true
validates :name, presence: true
validates_uniqueness_of :address
validates_uniqueness_of :name
after_create :set_products
before_save :recalculate_stock_in_all_products, if: proc { fictional_changed? && !fictional }
scope :no_fictional, -> { where(fictional: false) }
def set_products
products = Product.all
products.each do |product|
next if StoreProduct.where(store_id: id, product_id: product.id).present?
StoreProduct.create(product_id: product.id, store_id: id, stock: 0)
end
end
def recalculate_stock_in_all_products
StoreProduct.ids.each do |store_product_id|
RecalculateProductStockJob.perform_later(store_product_id)
end
end
end
Editor is loading...