rails-guide-store/app/models/product/notifications.rb

19 lines
457 B
Ruby
Raw Normal View History

2025-01-24 19:11:47 +00:00
module Product::Notifications
extend ActiveSupport::Concern
included do
has_many :subscribers, dependent: :destroy
after_update_commit :notify_subscribers, if: :back_in_stock?
def back_in_stock?
inventory_count_previously_was.zero? && inventory_count > 0
end
def notify_subscribers
subscribers.each do |sub|
ProductMailer.with(product: self, subscriber: sub).in_stock.deliver_later
end
end
end
end