Skip to content

Instantly share code, notes, and snippets.

@Blohinyuriy
Created October 10, 2016 20:09
Show Gist options
  • Save Blohinyuriy/a080c81b76a90047336c7f932ac31ebe to your computer and use it in GitHub Desktop.
Save Blohinyuriy/a080c81b76a90047336c7f932ac31ebe to your computer and use it in GitHub Desktop.
One view in `app/views/application/` for all controllers where collection needs pagination
class BaseController
before_action :set_paginable_variant, only: :index
helper_method :paginated_collection
private
def set_paginable_variant
request.variant = :paginable if paginable?
end
#
# Default value
#
def paginable?
false
end
def paginated_collection
{
collection: collection.decorate(context: options_for_pagination),
total_pages: collection.total_pages,
total_count: collection.total_count,
current_page: collection.current_page
}
end
#
# Default value
#
def options_for_pagination
{}
end
end
class ExamplesController < BaseController
private
def collection
@collection ||= Example.page(params[:page])
end
def paginable?
true
end
def options_for_pagination
{ index: true }
end
end
<%= sanitize paginated_collection.to_json %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment