Sha256: fef65898026343e92075c5ed7bf31a0f8366d220d63c4946fa63138194135d0b

Contents?: true

Size: 851 Bytes

Versions: 24

Compression:

Stored size: 851 Bytes

Contents

# frozen_string_literal: true

class Trigger < ApplicationRecord
  belongs_to :pending_item, polymorphic: true

  class << self
    def post_action(due_at, item, method = "came_due")
      create!(due_at: due_at, pending_item: item,
              trigger_method: method)
      fire
    end

    def fire
      where("due_at <= ?", Time.zone.now).destroy_all
      true
    end

    def remove(pending_item, conditions = {})
      return if pending_item.new_record?

      conditions = conditions.merge(pending_item_id: pending_item.id,
                                    pending_item_type: pending_item.class.to_s)
      where(conditions).delete_all
    end
  end

  # TODO: Ensure errors bubble up to where they are visible
  before_destroy :trigger_pending_item

  def trigger_pending_item
    pending_item&.send(trigger_method)
    true
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
publify_core-10.0.2 app/models/trigger.rb
HornsAndHooves-publify_core-10.5.0 app/models/trigger.rb
HornsAndHooves-publify_core-10.4.0 app/models/trigger.rb
HornsAndHooves-publify_core-10.3.0 app/models/trigger.rb
HornsAndHooves-publify_core-10.2.0 app/models/trigger.rb
publify_core-10.0.1 app/models/trigger.rb
publify_core-10.0.0 app/models/trigger.rb
publify_core-9.2.10 app/models/trigger.rb
HornsAndHooves-publify_core-10.1.1 app/models/trigger.rb
HornsAndHooves-publify_core-10.1.0 app/models/trigger.rb
HornsAndHooves-publify_core-10.0.3 app/models/trigger.rb
HornsAndHooves-publify_core-10.0.2 app/models/trigger.rb
HornsAndHooves-publify_core-10.0.1 app/models/trigger.rb
HornsAndHooves-publify_core-10.0.0 app/models/trigger.rb
publify_core-9.2.9 app/models/trigger.rb
publify_core-9.2.8 app/models/trigger.rb
publify_core-9.2.7 app/models/trigger.rb
publify_core-9.2.6 app/models/trigger.rb
publify_core-9.2.5 app/models/trigger.rb
publify_core-9.2.4 app/models/trigger.rb