Sha256: 3275458d2a995837d369b994776dbad0886fe21391b2dec8cb006f2c966ab156

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Bulkrax
  class Status < ApplicationRecord
    belongs_to :statusable, polymorphic: true, denormalize: { fields: %i[status_message], if: :latest? }
    belongs_to :runnable, polymorphic: true
    serialize :error_backtrace, Array

    scope :for_importers, -> { where(statusable_type: 'Bulkrax::Importer') }
    scope :for_exporters, -> { where(statusable_type: 'Bulkrax::Exporter') }

    scope :latest_by_statusable, -> { joins(latest_by_statusable_subtable.join_sources) }

    def self.latest_by_statusable_subtable
      status_table = self.arel_table
      latest_status_query = status_table.project(status_table[:statusable_id],
                                                 status_table[:statusable_type],
                                                 status_table[:id].maximum.as("latest_status_id")).group(status_table[:statusable_id], status_table[:statusable_type])

      latest_status_table = Arel::Table.new(latest_status_query).alias(:latest_status)
      status_table.join(latest_status_query.as(latest_status_table.name.to_s), Arel::Nodes::InnerJoin)
                  .on(status_table[:id].eq(latest_status_table[:latest_status_id]))
    end

    def latest?
      # TODO: remove if statment when we stop supporting Hyrax < 4
      self.id == if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new('6.0.0')
                   self.class.where(statusable_id: self.statusable_id, statusable_type: self.statusable_type).order('id desc').pick(:id)
                 else
                   self.class.where(statusable_id: self.statusable_id, statusable_type: self.statusable_type).order('id desc').pluck(:id).first # rubocop:disable Rails/Pick
                 end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bulkrax-8.3.0 app/models/bulkrax/status.rb
bulkrax-8.2.1 app/models/bulkrax/status.rb
bulkrax-8.2.0 app/models/bulkrax/status.rb
bulkrax-8.1.0 app/models/bulkrax/status.rb
bulkrax-8.0.0 app/models/bulkrax/status.rb
bulkrax-7.0.0 app/models/bulkrax/status.rb