Sha256: 712bca2f5c93f0907104670b9e233382caeb18722b533e1399429efc28cebc1e

Contents?: true

Size: 799 Bytes

Versions: 4

Compression:

Stored size: 799 Bytes

Contents

module ExternalServices
  module Action
    extend ActiveSupport::Concern

    included do
      scope :processed, -> { where.not(processed_at: nil) }
      scope :unprocessed, -> { where(processed_at: nil) }

      after_commit :kick_active_job
    end

    def processed?
      processed_at.present?
    end

    def set_processed!
      update_attributes! processed_at: Time.now
    end

    def execute!
      raise NotImplementedError
    end

    def kick_active_job
      return if processed? || api_disabled?

      job_class.set(queue: queue).perform_later(id)
    end

    module ClassMethods
      def perform_unprocessed
        Rails.logger.info "Running unprocessed #{self.class.name.demodulize} api actions..."

        unprocessed.each(&:kick_active_job)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
external_services-0.3.1 app/models/concerns/external_services/action.rb
external_services-0.3.0 app/models/concerns/external_services/action.rb
external_services-0.2.4 app/models/concerns/external_services/action.rb
external_services-0.2.3 app/models/concerns/external_services/action.rb