Class: TheGrid::Api::Command::Paginate

Inherits:
TheGrid::Api::Command show all
Defined in:
lib/the_grid/api/command/paginate.rb

Instance Method Summary collapse

Methods inherited from TheGrid::Api::Command

#batch?, build, #execute_on, find, register_lookup_scope, scopes

Instance Method Details

#calculate_max_page_for(relation, params) ⇒ Object



20
21
22
23
24
# File 'lib/the_grid/api/command/paginate.rb', line 20

def calculate_max_page_for(relation, params)
  params = configure(relation, params)
  total_count = params[:size].present? ? params[:size] : relation.except(:limit, :offset, :includes).count
  (total_count / params[:per_page].to_f).ceil
end

#configure(relation, params) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/the_grid/api/command/paginate.rb', line 5

def configure(relation, params)
  {}.tap do |o|
    o[:page] = params[:page].to_i
    o[:page] = 1 if o[:page] <= 0
    o[:size] = params[:size]

    o[:per_page] = params[:per_page].to_i
    o[:per_page] = self.class.default_per_page if o[:per_page] <= 0
  end
end

#contextualize(relation, params) ⇒ Object



26
27
28
# File 'lib/the_grid/api/command/paginate.rb', line 26

def contextualize(relation, params)
  {:max_page => calculate_max_page_for(relation, params)}
end

#run_on(relation, params) ⇒ Object



16
17
18
# File 'lib/the_grid/api/command/paginate.rb', line 16

def run_on(relation, params)
  relation.offset((params[:page] - 1) * params[:per_page]).limit(params[:per_page])
end