Created
October 10, 2016 20:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExamplesController < BaseController | |
private | |
def collection | |
@collection ||= Example.page(params[:page]) | |
end | |
def paginable? | |
true | |
end | |
def options_for_pagination | |
{ index: true } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= sanitize paginated_collection.to_json %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment