Sha256: a7b4d585ad32bd5420cbdd04a2c29e269eb4564eaf1b09e0bb40e8529f26b5b5

Contents?: true

Size: 830 Bytes

Versions: 2

Compression:

Stored size: 830 Bytes

Contents

class Trigger < ActiveRecord::Base
  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.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) if pending_item
    true
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
publify_core-9.0.1 app/models/trigger.rb
publify_core-9.0.0 app/models/trigger.rb