class Tablets::Data::Processing::Paginate

Incapsulate relation pagination logic

Public Instance Methods

apply(relation) click to toggle source

Applies :start and :length from params to relation as offset and limit :length is optional. Default value is used if no length is provided :start recalculated to match beginnning of page

# File lib/tablets/data/processing/paginate.rb, line 11
def apply(relation)
  relation.offset(offset).limit(per_page)
end

Private Instance Methods

default_length() click to toggle source

Returns default length.

# File lib/tablets/data/processing/paginate.rb, line 28
def default_length
  10
end
offset() click to toggle source

Calculates offset as beginning of page.

# File lib/tablets/data/processing/paginate.rb, line 18
def offset
  (page - 1) * per_page
end
page() click to toggle source

Calculates page from start.

# File lib/tablets/data/processing/paginate.rb, line 23
def page
  (params[:start].to_i / per_page) + 1
end
per_page() click to toggle source

Returns length or default value.

# File lib/tablets/data/processing/paginate.rb, line 33
def per_page
  params.fetch(:length, default_length).to_i
end