Sha256: 8bbc4e943cd2c8263e46e2af64bbf8218d1ebb3b4587ce169337de0642a28457

Contents?: true

Size: 963 Bytes

Versions: 2

Compression:

Stored size: 963 Bytes

Contents

class MissionControl::Jobs::Page
  DEFAULT_PAGE_SIZE = 10

  attr_reader :jobs_relation, :index, :page_size

  def initialize(jobs_relation, page: 1, page_size: DEFAULT_PAGE_SIZE)
    @jobs_relation = jobs_relation
    @page_size = page_size
    @index = [ page, 1 ].max
  end

  def jobs
    jobs_relation.limit(page_size).offset(offset)
  end

  def first?
    index == 1
  end

  def last?
    index == pages_count || empty? || jobs.empty?
  end

  def empty?
    total_count == 0
  end

  def previous_index
    [ index - 1, 1 ].max
  end

  def next_index
    pages_count ? [ index + 1, pages_count ].min : index + 1
  end

  def pages_count
    (total_count.to_f / 10).ceil unless total_count.infinite?
  end

  def total_count
    @total_count ||= jobs_relation.count # Potentially expensive when filtering and a lot of jobs, with adapter in charge of doing the filtering in memory
  end

  private
    def offset
      (index - 1) * page_size
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mission_control-jobs-0.1.1 app/models/mission_control/jobs/page.rb
mission_control-jobs-0.1.0 app/models/mission_control/jobs/page.rb