module Garage::PaginatingResponder

Public Instance Methods

display(resource, *args) click to toggle source
Calls superclass method
# File lib/garage/paginating_responder.rb, line 3
def display(resource, *args)
  if @options[:paginate]
    resource = paginate resource
  end
  super(resource, *args)
end
max_per_page=(count) click to toggle source
# File lib/garage/paginating_responder.rb, line 10
def max_per_page=(count)
  @max_per_page = count
end
reveal_total!() click to toggle source
# File lib/garage/paginating_responder.rb, line 14
def reveal_total!
  @options.delete(:hard_limit)
end

Private Instance Methods

build_path_for(params) click to toggle source
# File lib/garage/paginating_responder.rb, line 98
def build_path_for(params)
  parameters = controller.request.query_parameters.merge(params).tap {|p| p.delete(:access_token) }
  "#{controller.request.path}?#{parameters.to_query}"
end
distinct?() click to toggle source
# File lib/garage/paginating_responder.rb, line 20
def distinct?
  !!@options[:distinct_by]
end
hard_limit() click to toggle source
# File lib/garage/paginating_responder.rb, line 28
def hard_limit
  @options[:hard_limit]
end
hide_total?() click to toggle source
# File lib/garage/paginating_responder.rb, line 24
def hide_total?
  !!@options[:hard_limit]
end
max_per_page() click to toggle source
# File lib/garage/paginating_responder.rb, line 32
def max_per_page
  @options[:max_per_page] || @max_per_page || 100
end
paginate(rs) click to toggle source
# File lib/garage/paginating_responder.rb, line 51
def paginate(rs)
  @options[:hard_limit] ||= 1000 if @options[:hide_total] # backward compat for hide_total

  per_page = [ max_per_page, (controller.params[:per_page] || @options[:per_page] || 20).to_i ].min

  rs = rs.page(controller.params[:page] || 1).per(per_page)

  set_total_count(rs, per_page)

  unless hide_total?
    controller.response.headers['X-List-TotalCount'] = total_count(rs).to_s
  end

  # FIXME construct_links must be called after calling rs.total_count to avoid invalid count cache
  construct_links(rs, per_page)

  if hide_total?
    if rs.offset_value > hard_limit
      rs = []
    elsif rs.offset_value + per_page > hard_limit
      rs = rs.slice 0, (hard_limit - rs.offset_value) # becomes Array here, and hope it's ok
    end
  end

  rs
end
set_total_count(rs, per_page) click to toggle source
# File lib/garage/paginating_responder.rb, line 36
def set_total_count(rs, per_page)
  if hard_limit
    limit = hard_limit
    rs.instance_variable_set(:@total_count, limit)
  end
end
total_count(rs) click to toggle source
# File lib/garage/paginating_responder.rb, line 43
def total_count(rs)
  if distinct?
    rs.total_count(@options[:distinct_by], distinct: true)
  else
    rs.total_count
  end
end