Class: Marj::Record

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/marj/record.rb

Overview

The default ActiveRecord class.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dueActiveRecord::Relation

Returns an ActiveRecord::Relation scope for enqueued jobs with a scheduled_at that is either null or in the past.

Returns:

  • (ActiveRecord::Relation)


62
63
64
# File 'lib/marj/record.rb', line 62

def due
  where('scheduled_at IS NULL OR scheduled_at <= ?', Time.now.utc)
end

.orderedActiveRecord::Relation

Returns an ActiveRecord::Relation scope for jobs ordered by priority (null last), then scheduled_at (null last), then enqueued_at.

Returns:

  • (ActiveRecord::Relation)


70
71
72
73
74
75
76
77
78
79
# File 'lib/marj/record.rb', line 70

def ordered
  order(
    Arel.sql(<<~SQL.squish, Time.now.utc)
      CASE WHEN scheduled_at IS NULL OR scheduled_at <= ? THEN 0 ELSE 1 END,
      CASE WHEN priority IS NULL THEN 1 ELSE 0 END, priority,
      CASE WHEN scheduled_at IS NULL THEN 1 ELSE 0 END, scheduled_at,
      enqueued_at
    SQL
  )
end

Instance Method Details

#as_jobActiveJob::Base

Returns a job object for this record which will update the database when successfully executed, enqueued or discarded.

Returns:

  • (ActiveJob::Base)


53
54
55
# File 'lib/marj/record.rb', line 53

def as_job
  Marj.send(:to_job, self)
end