module Forcast module Model module RuleEngine extend ActiveSupport::Concern included do after_create :send_webhook_create after_update :send_webhook_update after_destroy :send_webhook_destroy end def class_polling Polling.where(polling_model: self.class.name, class_polling?: true) end def class_rule Rule.for_class(self.class.name) end def webhook Webhook.joins(:rule).merge(rule) end def object_webhook Webhook.joins(:rule).merge(Rule.for_object(self)) end def polling Polling.where(polling_model: self.class.name, polling_model_id: id) end def rule Rule.for_object_and_class(self) end def send_webhook_create puts 'send_webhook_create' end def send_webhook_destroy puts 'send_webhook_destroy' end def send_webhook_update puts 'send_webhook_update' JobsWebhooks::ValidateModelWebhook.perform_later( webhook_model: self.class.name, webhook_model_id: id, changed_attrs_json: saved_changes.to_json, created_at: Time.now.to_s ) end def currents_pollings arr1 = polling.where(created_at: 4.week.ago.beginning_of_day..Time.now) .where(active?: true) arr2 = class_polling.where(active?: true) arr2 + arr1 end end end end