Skip to content

Instantly share code, notes, and snippets.

@willnet
Created November 28, 2018 07:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willnet/5ee69c90312f9d7e5db786937248852f to your computer and use it in GitHub Desktop.
Save willnet/5ee69c90312f9d7e5db786937248852f to your computer and use it in GitHub Desktop.
class WrapperForKaminari
attr_reader :total_count, :per, :page
delegate_missing_to :@models
def initialize(models:, total_count:, per:, page:)
@models = models
@total_count = total_count
@per = per
@page = page
end
def total_pages
(total_count / per) + 1
end
def current_page
page
end
def last_page?
current_page == total_pages
end
def out_of_range?
current_page > total_pages
end
def limit_value
per
end
def next_page
current_page + 1 unless last_page? || out_of_range?
end
def prev_page
current_page - 1 unless first_page? || out_of_range?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment