From a944ae925aeb2fe80b24e4cd24d0206dfc33e451 Mon Sep 17 00:00:00 2001 From: Jose Galisteo Date: Wed, 19 Aug 2015 23:30:42 +0200 Subject: [PATCH] Full configuration --- lib/jbuilder/pagination/configuration.rb | 9 ++++----- lib/jbuilder/pagination/pages.rb | 19 ++++++++++++------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/jbuilder/pagination/configuration.rb b/lib/jbuilder/pagination/configuration.rb index 58262de..91eee5b 100644 --- a/lib/jbuilder/pagination/configuration.rb +++ b/lib/jbuilder/pagination/configuration.rb @@ -1,16 +1,15 @@ module Jbuilder::Pagination class Configuration - attr_accessor :per_page_method - attr_accessor :page_method - attr_accessor :page_param - attr_accessor :per_page_param + attr_accessor :current_page_method, :total_pages_method, :per_page_method, :page_method, :page_param, :per_page_param def initialize + @total_pages_method = :total_pages + @current_page_method = :current_page @per_page_method = :per_page + @per_page_param = :per_page @page_method = :page @page_param = :page - @per_page_param = :per_page end end end diff --git a/lib/jbuilder/pagination/pages.rb b/lib/jbuilder/pagination/pages.rb index 9e90c97..b2117e4 100644 --- a/lib/jbuilder/pagination/pages.rb +++ b/lib/jbuilder/pagination/pages.rb @@ -1,4 +1,5 @@ class Jbuilder + def pages!(collection, options={}) return unless collection @@ -11,7 +12,7 @@ def pages!(collection, options={}) private - delegate :per_page_method, :page_param, :per_page_param, to: :configuration + delegate :current_page_method, :total_pages_method, :per_page_method, :page_param, :per_page_param, to: :configuration def configuration Pagination.configuration @@ -19,18 +20,22 @@ def configuration def pages_from(collection) - return {} if collection.total_pages == 1 + total_pages = collection.send(total_pages_method) + current_page = collection.send(configuration.current_page_method) + + return {} if total_pages == 1 {}.tap do |pages| - unless collection.current_page == 1 + unless current_page == 1 pages[:first] = 1 - pages[:prev] = collection.current_page - 1 + pages[:prev] = current_page - 1 end - unless collection.current_page == collection.total_pages - pages[:last] = collection.total_pages - pages[:next] = collection.current_page + 1 + unless current_page == total_pages + pages[:last] = total_pages + pages[:next] = current_page + 1 end end end + end