Skip to content

Instantly share code, notes, and snippets.

@Fitzsimmons
Created October 31, 2012 19:20
Show Gist options
  • Save Fitzsimmons/3989230 to your computer and use it in GitHub Desktop.
Save Fitzsimmons/3989230 to your computer and use it in GitHub Desktop.
Framework our quick hack to avoid the extra queries from Kaminari
class Pager
attr_reader :arel, :page, :per_page
def initialize(arel, options={})
@arel = arel
@page = Integer(options[:page]) rescue 1
@per_page = Integer(options[:per_page]) rescue arel.klass.default_per_page
end
def paged_results
offset = (page - 1) * per_page
table_name = arel.klass.table_name
results = arel.
select("COUNT(#{table_name}.id) OVER () AS non_paged_count, #{table_name}.*").
offset(offset).
limit(per_page).all
total_count = non_paged_count_from(results)
Kaminari.paginate_array(results, :total_count => total_count).page(page).per(per_page)
end
def non_paged_count_from(results)
results.empty? ? 0 : results.first.non_paged_count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment