Fix errors and bugs in the given code, without changing its intended functionality. ONLY…
- User
Fix errors and bugs in the given code, without changing its intended functionality. ONLY return the fixed code and nothing else. Enclose the entire code in a single code block.
class Trigger < ApplicationRecord class Error < StandardError attr_reader :details, :trigger, :at
def initialize(msg, trigger: nil, details: nil, at: nil) super(msg) @trigger = trigger @details = details @at = at endend
acts_as_list scope: %i[type]
scope :active, ->(active = true) { where(active:) } scope :order_by_position, -> { order(:position) } scope :ready_to_run, -> { where("#{Trigger.table_name}.next_run_at <= NOW()") }
enum when: { save: "save", create: "create", update: "update", destroy: "destroy", cron: "cron", manual: "manual" }, _prefix: true
belongs_to :author, class_name: "User" has_one :job_status, class_name: "JobStatus::Status", as: :reference
validates :condition_script, :before_script, :after_script, :cron_script, :manual_script, :async_script, syntax: true validates :name, :when, presence: true validates :name, uniqueness: { scope: %i[type] }, length: { maximum: 255 } validates :timezone, inclusion: { in: proc { ::ActiveSupport::TimeZone.all.map { _1.tzinfo.canonical_identifier } } }, allow_blank: true, if: :when_cron? validates :async_delay, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0, allow_blank: true }
validate :validate_cron, if: :when_cron? validate :validate_filters_json, if: :when_bulk? validate :validate_filters, if: :when_bulk?
before_save :cleanup_changed_fields before_save :reschedule
class << self def has_constraint?(name) constraint = "::Triggers::Constraints::#{name.to_s.camelize}Constraint".constantize included_modules.include?(constraint) end
def registered_entity_classes @registered_entity_classes ||= {} end def register_entity_class(klass) registered_entity_classes[klass.name] = klass end def entity_class_registered?(klass) registered_entity_classes.include?(klass.to_s) end def entity_class @entity_class ||= name.delete_suffix("Trigger").delete_prefix("Triggers::").constantize end def entity_custom_field_class @entity_custom_field_class ||= "#{entity_class.name}CustomField".safe_constantize end def native_entity_fields raise NotImplementedError end def entity_fields RequestStore["#{name.underscore}_entity_fields"] ||= begin fields = native_entity_fields fields += entity_custom_field_class.all.map(&:attribute_name) if entity_custom_field_class fields end end def loading_scope(events: [], changes: nil, **) scope = active.order_by_position scope.where! when: events scope.where! "((changed_fields && ARRAY[?]::VARCHAR[]) OR changed_fields='{}')", changes.keys if changes scope end def for(events: [], changes: nil, **constraints) cache = RequestStore[:triggers_cache] ||= {} cache[[events, changes, constraints]] ||= loading_scope(events:, changes:, **constraints).to_a end def query_scope all end - Assistant