Sha256: 7bcc702e351b2dfed3e41737611bd8fbb2333b923887024fa428aee3a570f0dd

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module GoodJob
  # ActiveRecord model to share behavior between {Job} and {Execution} models
  # which both read out of the same table.
  class BaseExecution < BaseRecord
    self.table_name = 'good_jobs'

    # With a given class name
    # @!method job_class
    # @!scope class
    # @param string [String] Execution class name
    # @return [ActiveRecord::Relation]
    scope :job_class, ->(name) { where(params_job_class.eq(name)) }

    class << self
      def json_string(json, attr)
        Arel::Nodes::Grouping.new(Arel::Nodes::InfixOperation.new('->>', json, Arel::Nodes.build_quoted(attr)))
      end

      def params_job_class
        json_string(arel_table['serialized_params'], 'job_class')
      end

      def params_execution_count
        Arel::Nodes::InfixOperation.new(
          '::',
          json_string(arel_table['serialized_params'], 'executions'),
          Arel.sql('integer')
        )
      end

      def coalesce_scheduled_at_created_at
        arel_table.coalesce(arel_table['scheduled_at'], arel_table['created_at'])
      end

      def discrete_support?
        if connection.table_exists?('good_job_executions')
          true
        else
          migration_pending_warning!
          false
        end
      end
    end

    # The ActiveJob job class, as a string
    # @return [String]
    def job_class
      discrete? ? attributes['job_class'] : serialized_params['job_class']
    end

    def discrete?
      self.class.discrete_support? && is_discrete?
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
good_job-3.15.7 app/models/good_job/base_execution.rb
good_job-3.15.6 app/models/good_job/base_execution.rb
good_job-3.15.5 app/models/good_job/base_execution.rb
good_job-3.15.4 app/models/good_job/base_execution.rb