From cd50d414765dbf822b5ec3d3c86669170eae40ad Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Mon, 28 Feb 2011 00:17:09 +0900 Subject: [PATCH] use PageProxy for current_page --- app/views/kaminari/_first_page.html.erb | 6 +++--- app/views/kaminari/_first_page.html.haml | 6 +++--- app/views/kaminari/_gap.html.erb | 2 +- app/views/kaminari/_gap.html.haml | 2 +- app/views/kaminari/_last_page.html.erb | 6 +++--- app/views/kaminari/_last_page.html.haml | 6 +++--- app/views/kaminari/_next_page.html.erb | 6 +++--- app/views/kaminari/_next_page.html.haml | 6 +++--- app/views/kaminari/_page.html.erb | 4 ++-- app/views/kaminari/_page.html.haml | 4 ++-- app/views/kaminari/_paginator.html.erb | 2 +- app/views/kaminari/_paginator.html.haml | 2 +- app/views/kaminari/_prev_page.html.erb | 6 +++--- app/views/kaminari/_prev_page.html.haml | 6 +++--- lib/kaminari/helpers/helpers.rb | 11 ++++++++++- 15 files changed, 42 insertions(+), 33 deletions(-) diff --git a/app/views/kaminari/_first_page.html.erb b/app/views/kaminari/_first_page.html.erb index 2fb9642..c00c72b 100644 --- a/app/views/kaminari/_first_page.html.erb +++ b/app/views/kaminari/_first_page.html.erb @@ -1,11 +1,11 @@ <%# Link to the "First" page - available local variables url: url to the first page - current_page: the page number of currently displayed page + current_page: a page object for the currently displayed page num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote -%> - - <%= link_to_unless current_page == 1, raw(t 'views.pagination.first'), url, :remote => remote %> + + <%= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, :remote => remote %> diff --git a/app/views/kaminari/_first_page.html.haml b/app/views/kaminari/_first_page.html.haml index e40501d..35b2136 100644 --- a/app/views/kaminari/_first_page.html.haml +++ b/app/views/kaminari/_first_page.html.haml @@ -1,9 +1,9 @@ -# Link to the "First" page - available local variables url: url to the first page - current_page: the page number of currently displayed page + current_page: a page object for the currently displayed page num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote -%span{:class => "first#{' disabled' if current_page == 1}"} - = link_to_unless current_page == 1, raw(t 'views.pagination.first'), url, :remote => remote +%span{:class => "first#{' disabled' if current_page.first?}"} + = link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, :remote => remote diff --git a/app/views/kaminari/_gap.html.erb b/app/views/kaminari/_gap.html.erb index 5b294c2..d1800e1 100644 --- a/app/views/kaminari/_gap.html.erb +++ b/app/views/kaminari/_gap.html.erb @@ -1,6 +1,6 @@ <%# Non-link tag that stands for skipped pages... - available local variables - current_page: the page number of currently displayed page + current_page: a page object for the currently displayed page num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote diff --git a/app/views/kaminari/_gap.html.haml b/app/views/kaminari/_gap.html.haml index b97fe9d..4190b04 100644 --- a/app/views/kaminari/_gap.html.haml +++ b/app/views/kaminari/_gap.html.haml @@ -1,6 +1,6 @@ -# Non-link tag that stands for skipped pages... - available local variables - current_page: the page number of currently displayed page + current_page: a page object for the currently displayed page num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote diff --git a/app/views/kaminari/_last_page.html.erb b/app/views/kaminari/_last_page.html.erb index 5791bf0..017b607 100644 --- a/app/views/kaminari/_last_page.html.erb +++ b/app/views/kaminari/_last_page.html.erb @@ -1,11 +1,11 @@ <%# Link to the "Last" page - available local variables url: url to the last page - current_page: the page number of currently displayed page + current_page: a page object for the currently displayed page num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote -%> - - <%= link_to_unless current_page == num_pages, raw(t 'views.pagination.last'), url, {:remote => remote} %> + + <%= link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, {:remote => remote} %> diff --git a/app/views/kaminari/_last_page.html.haml b/app/views/kaminari/_last_page.html.haml index 971b051..6d88fff 100644 --- a/app/views/kaminari/_last_page.html.haml +++ b/app/views/kaminari/_last_page.html.haml @@ -1,9 +1,9 @@ -# Link to the "Last" page - available local variables url: url to the last page - current_page: the page number of currently displayed page + current_page: a page object for the currently displayed page num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote -%span{:class => "last#{' disabled' if current_page == num_pages}"} - = link_to_unless current_page == num_pages, raw(t 'views.pagination.last'), url, {:remote => remote} +%span{:class => "last#{' disabled' if current_page.last?}"} + = link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, {:remote => remote} diff --git a/app/views/kaminari/_next_page.html.erb b/app/views/kaminari/_next_page.html.erb index 1b2fc99..4d1ddb7 100644 --- a/app/views/kaminari/_next_page.html.erb +++ b/app/views/kaminari/_next_page.html.erb @@ -1,11 +1,11 @@ <%# Link to the "Next" page - available local variables url: url to the next page - current_page: the page number of currently displayed page + current_page: a page object for the currently displayed page num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote -%> - - <%= link_to_unless current_page == num_pages, raw(t 'views.pagination.next'), url, :rel => 'next', :remote => remote %> + + <%= link_to_unless current_page.last?, raw(t 'views.pagination.next'), url, :rel => 'next', :remote => remote %> diff --git a/app/views/kaminari/_next_page.html.haml b/app/views/kaminari/_next_page.html.haml index b4e0ff1..d7c66f6 100644 --- a/app/views/kaminari/_next_page.html.haml +++ b/app/views/kaminari/_next_page.html.haml @@ -1,9 +1,9 @@ -# Link to the "Next" page - available local variables url: url to the next page - current_page: the page number of currently displayed page + current_page: a page object for the currently displayed page num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote -%span{:class => "next#{' disabled' if current_page == num_pages}"} - = link_to_unless current_page == num_pages, raw(t 'views.pagination.next'), url, :rel => 'next', :remote => remote +%span{:class => "next#{' disabled' if current_page.last?}"} + = link_to_unless current_page.last?, raw(t 'views.pagination.next'), url, :rel => 'next', :remote => remote diff --git a/app/views/kaminari/_page.html.erb b/app/views/kaminari/_page.html.erb index ff3cec5..1683069 100644 --- a/app/views/kaminari/_page.html.erb +++ b/app/views/kaminari/_page.html.erb @@ -1,8 +1,8 @@ <%# Link showing page number - available local variables - page: the PageProxy for the current page + page: a page object for "this" page url: url to this page - current_page: the page number of currently displayed page + current_page: a page object for the currently displayed page num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote diff --git a/app/views/kaminari/_page.html.haml b/app/views/kaminari/_page.html.haml index 5f78fed..3962d0f 100644 --- a/app/views/kaminari/_page.html.haml +++ b/app/views/kaminari/_page.html.haml @@ -1,8 +1,8 @@ -# Link showing page number - available local variables - page: the PageProxy for the current page + page: a page object for "this" page url: url to this page - current_page: the page number of currently displayed page + current_page: a page object for the currently displayed page num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote diff --git a/app/views/kaminari/_paginator.html.erb b/app/views/kaminari/_paginator.html.erb index 896dc83..fab28b2 100644 --- a/app/views/kaminari/_paginator.html.erb +++ b/app/views/kaminari/_paginator.html.erb @@ -1,6 +1,6 @@ <%# The container tag - available local variables - current_page: the page number of currently displayed page + current_page: a page object for the currently displayed page num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote diff --git a/app/views/kaminari/_paginator.html.haml b/app/views/kaminari/_paginator.html.haml index b08161a..0cc5f85 100644 --- a/app/views/kaminari/_paginator.html.haml +++ b/app/views/kaminari/_paginator.html.haml @@ -1,6 +1,6 @@ -# The container tag - available local variables - current_page: the page number of currently displayed page + current_page: a page object for the currently displayed page num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote diff --git a/app/views/kaminari/_prev_page.html.erb b/app/views/kaminari/_prev_page.html.erb index c412cc3..0634336 100644 --- a/app/views/kaminari/_prev_page.html.erb +++ b/app/views/kaminari/_prev_page.html.erb @@ -1,11 +1,11 @@ <%# Link to the "Previous" page - available local variables url: url to the previous page - current_page: the page number of currently displayed page + current_page: a page object for the currently displayed page num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote -%> - - <%= link_to_unless current_page == 1, raw(t 'views.pagination.previous'), url, :rel => 'prev', :remote => remote %> + + <%= link_to_unless current_page.first?, raw(t 'views.pagination.previous'), url, :rel => 'prev', :remote => remote %> diff --git a/app/views/kaminari/_prev_page.html.haml b/app/views/kaminari/_prev_page.html.haml index 0d7e24b..f844126 100644 --- a/app/views/kaminari/_prev_page.html.haml +++ b/app/views/kaminari/_prev_page.html.haml @@ -1,9 +1,9 @@ -# Link to the "Previous" page - available local variables url: url to the previous page - current_page: the page number of currently displayed page + current_page: a page object for the currently displayed page num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote -%span{:class => "prev#{' disabled' if current_page == 1}"} - = link_to_unless current_page == 1, raw(t 'views.pagination.previous'), url, :rel => 'prev', :remote => remote +%span{:class => "prev#{' disabled' if current_page.first?}"} + = link_to_unless current_page.first?, raw(t 'views.pagination.previous'), url, :rel => 'prev', :remote => remote diff --git a/lib/kaminari/helpers/helpers.rb b/lib/kaminari/helpers/helpers.rb index d6d0419..d72010f 100644 --- a/lib/kaminari/helpers/helpers.rb +++ b/lib/kaminari/helpers/helpers.rb @@ -12,6 +12,7 @@ module Kaminari h[:right] = options.delete(:right) || outer_window || 0 end @template, @options = template, options + @options[:current_page] = PageProxy.new @window_options.merge(@options), @options[:current_page], nil # so that this instance can actually "render". Black magic? @output_buffer = ActionView::OutputBuffer.new end @@ -117,7 +118,7 @@ module Kaminari # inside the inner window or not def inside_window? - (@page - @options[:current_page]).abs <= @options[:window] + (@options[:current_page] - @page).abs <= @options[:window] end # The last rendered tag was "truncated" or not @@ -133,6 +134,14 @@ module Kaminari number.to_s end + def +(other) + to_i + other.to_i + end + + def -(other) + to_i - other.to_i + end + def <=>(other) to_i <=> other.to_i end