From 059ac2d200620ae66d11ca8e7b7f117d5d9d70e1 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Tue, 15 Feb 2011 17:29:18 +0900 Subject: [PATCH 1/9] little bit readable than it used to be? --- lib/kaminari/helpers.rb | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/kaminari/helpers.rb b/lib/kaminari/helpers.rb index 6e43f6a..421d9ca 100644 --- a/lib/kaminari/helpers.rb +++ b/lib/kaminari/helpers.rb @@ -11,29 +11,45 @@ module Kaminari @left, @window, @right = (options[:left] || options[:outer_window] || 1), (options[:window] || options[:inner_window] || 4), (options[:right] || options[:outer_window] || 1) end + def current_page(page) + CurrentPage.new self, :page => page + end + + def page_link(page) + case page + when 1 + FirstPageLink + when @options[:num_pages] + LastPageLink + else + PageLink + end.new self, :page => page + end + + %w[prev_link prev_span next_link next_span truncated_span].each do |tag| + eval <<-DEF + def #{tag} + #{tag.classify}.new self + end + DEF + end + def tagify_links #:nodoc: num_pages, current_page, left, window, right = @options[:num_pages], @options[:current_page], @left, @window, @right return [] if num_pages <= 1 tags = [] - tags << (current_page > 1 ? PrevLink.new(self) : PrevSpan.new(self)) + tags << (current_page > 1 ? prev_link : prev_span) 1.upto(num_pages) do |i| if i == current_page - tags << CurrentPage.new(self, :page => i) + tags << current_page(i) elsif (i <= left + 1) || ((num_pages - i) <= right) || ((i - current_page).abs <= window) - case i - when 1 - tags << FirstPageLink.new(self, :page => i) - when num_pages - tags << LastPageLink.new(self, :page => i) - else - tags << PageLink.new(self, :page => i) - end + tags << page_link(i) else - tags << TruncatedSpan.new(self) unless tags.last.is_a? TruncatedSpan + tags << truncated_span unless tags.last.is_a? TruncatedSpan end end - tags << (num_pages > current_page ? NextLink.new(self) : NextSpan.new(self)) + tags << (num_pages > current_page ? next_link : next_span) end def partial_exists?(name) #:nodoc: From 3f0199af00f2d9c876bafaaba04d92d7b447a151 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Tue, 15 Feb 2011 18:40:30 +0900 Subject: [PATCH 2/9] little bit readable than it used to be? (Part II) --- lib/kaminari/helpers.rb | 53 +++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/lib/kaminari/helpers.rb b/lib/kaminari/helpers.rb index 421d9ca..6dfcb02 100644 --- a/lib/kaminari/helpers.rb +++ b/lib/kaminari/helpers.rb @@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), 'tags') module Kaminari module Helpers class PaginationRenderer - attr_reader :options, :params + attr_reader :options, :params, :left, :window, :right def initialize(template, options) #:nodoc: @template, @options = template, options @@ -11,19 +11,19 @@ module Kaminari @left, @window, @right = (options[:left] || options[:outer_window] || 1), (options[:window] || options[:inner_window] || 4), (options[:right] || options[:outer_window] || 1) end - def current_page(page) - CurrentPage.new self, :page => page + def current_page + CurrentPage.new self, :page => @page end - def page_link(page) - case page + def page_link + case @page when 1 FirstPageLink when @options[:num_pages] LastPageLink else PageLink - end.new self, :page => page + end.new self, :page => @page end %w[prev_link prev_span next_link next_span truncated_span].each do |tag| @@ -34,17 +34,24 @@ module Kaminari DEF end + def each_page + 1.upto(@options[:num_pages]) do |i| + @page = i + yield PageProxy.new(self, i) + end + end + def tagify_links #:nodoc: - num_pages, current_page, left, window, right = @options[:num_pages], @options[:current_page], @left, @window, @right + num_pages, current_page = @options[:num_pages], @options[:current_page] return [] if num_pages <= 1 tags = [] tags << (current_page > 1 ? prev_link : prev_span) - 1.upto(num_pages) do |i| - if i == current_page - tags << current_page(i) - elsif (i <= left + 1) || ((num_pages - i) <= right) || ((i - current_page).abs <= window) - tags << page_link(i) + each_page do |page| + if page.current? + tags << current_page() + elsif page.left_outer? || page.right_outer? || page.inside_window? + tags << page_link else tags << truncated_span unless tags.last.is_a? TruncatedSpan end @@ -108,6 +115,28 @@ module Kaminari def clear_content_for(name) @template.instance_variable_get('@_content_for')[name] = ActiveSupport::SafeBuffer.new end + + class PageProxy + def initialize(renderer, page) + @renderer, @page = renderer, page + end + + def current? + @page == @renderer.options[:current_page] + end + + def left_outer? + @page <= @renderer.left + 1 + end + + def right_outer? + @renderer.options[:num_pages] - @page <= @renderer.right + end + + def inside_window? + (@page - @renderer.options[:current_page]).abs <= @renderer.window + end + end end # = Helpers From ec5c1be4584f8547ca2692bb2b60b3d3396d2b65 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Tue, 15 Feb 2011 18:50:00 +0900 Subject: [PATCH 3/9] little bit readable than it used to be? (Part III) --- lib/kaminari/helpers.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/kaminari/helpers.rb b/lib/kaminari/helpers.rb index 6dfcb02..72e8a58 100644 --- a/lib/kaminari/helpers.rb +++ b/lib/kaminari/helpers.rb @@ -12,7 +12,7 @@ module Kaminari end def current_page - CurrentPage.new self, :page => @page + @last = CurrentPage.new self, :page => @page end def page_link @@ -29,7 +29,7 @@ module Kaminari %w[prev_link prev_span next_link next_span truncated_span].each do |tag| eval <<-DEF def #{tag} - #{tag.classify}.new self + @last = #{tag.classify}.new self end DEF end @@ -37,7 +37,7 @@ module Kaminari def each_page 1.upto(@options[:num_pages]) do |i| @page = i - yield PageProxy.new(self, i) + yield PageProxy.new(self, i, @last) end end @@ -53,7 +53,7 @@ module Kaminari elsif page.left_outer? || page.right_outer? || page.inside_window? tags << page_link else - tags << truncated_span unless tags.last.is_a? TruncatedSpan + tags << truncated_span unless page.was_truncated? end end tags << (num_pages > current_page ? next_link : next_span) @@ -117,8 +117,8 @@ module Kaminari end class PageProxy - def initialize(renderer, page) - @renderer, @page = renderer, page + def initialize(renderer, page, last) + @renderer, @page, @last = renderer, page, last end def current? @@ -136,6 +136,10 @@ module Kaminari def inside_window? (@page - @renderer.options[:current_page]).abs <= @renderer.window end + + def was_truncated? + @last.is_a? TruncatedSpan + end end end From 5b76f94b51371b37bc306df00335601894d9ba85 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Wed, 16 Feb 2011 06:48:35 +0900 Subject: [PATCH 4/9] move the whole pagination logic to the paginator partial so that users can see and edit the logic this removes an internal hack but introduces one or two other kind of black magics... --- app/views/kaminari/_paginator.html.erb | 18 +++++++++++--- app/views/kaminari/_paginator.html.haml | 13 ++++++++-- lib/kaminari/helpers.rb | 32 +++++++------------------ lib/kaminari/tags.rb | 4 ++++ 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/app/views/kaminari/_paginator.html.erb b/app/views/kaminari/_paginator.html.erb index f8cafba..e356926 100644 --- a/app/views/kaminari/_paginator.html.erb +++ b/app/views/kaminari/_paginator.html.erb @@ -5,6 +5,18 @@ per_page: number of items to fetch per page remote: data-remote -%> - +<% paginator.compose_tags do -%> + +<% end -%> diff --git a/app/views/kaminari/_paginator.html.haml b/app/views/kaminari/_paginator.html.haml index ff93890..1c6e481 100644 --- a/app/views/kaminari/_paginator.html.haml +++ b/app/views/kaminari/_paginator.html.haml @@ -4,5 +4,14 @@ num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote -%nav.pagination - = content_for :kaminari_paginator_tags +- paginator.compose_tags do + %nav.pagination + = current_page > 1 ? prev_link_tag : prev_span_tag + - each_page do |page| + - if page.current? + = current_page_tag + - elsif page.left_outer? || page.right_outer? || page.inside_window? + = page_link_tag + - elsif !page.was_truncated? + = truncated_span_tag + = num_pages > current_page ? next_link_tag : next_span_tag diff --git a/lib/kaminari/helpers.rb b/lib/kaminari/helpers.rb index 72e8a58..7531c90 100644 --- a/lib/kaminari/helpers.rb +++ b/lib/kaminari/helpers.rb @@ -7,16 +7,18 @@ module Kaminari def initialize(template, options) #:nodoc: @template, @options = template, options + # so that this Renderer instance can actually "render". Black magic? + @output_buffer = @template.instance_variable_get('@output_buffer') @params = options[:params] ? template.params.merge(options.delete :params) : template.params @left, @window, @right = (options[:left] || options[:outer_window] || 1), (options[:window] || options[:inner_window] || 4), (options[:right] || options[:outer_window] || 1) end - def current_page + def current_page_tag @last = CurrentPage.new self, :page => @page end - def page_link - case @page + def page_link_tag + @last = case @page when 1 FirstPageLink when @options[:num_pages] @@ -28,7 +30,7 @@ module Kaminari %w[prev_link prev_span next_link next_span truncated_span].each do |tag| eval <<-DEF - def #{tag} + def #{tag}_tag @last = #{tag.classify}.new self end DEF @@ -41,22 +43,11 @@ module Kaminari end end - def tagify_links #:nodoc: + def compose_tags(&block) #:nodoc: num_pages, current_page = @options[:num_pages], @options[:current_page] return [] if num_pages <= 1 - tags = [] - tags << (current_page > 1 ? prev_link : prev_span) - each_page do |page| - if page.current? - tags << current_page() - elsif page.left_outer? || page.right_outer? || page.inside_window? - tags << page_link - else - tags << truncated_span unless page.was_truncated? - end - end - tags << (num_pages > current_page ? next_link : next_span) + instance_eval &block end def partial_exists?(name) #:nodoc: @@ -66,8 +57,6 @@ module Kaminari def to_s #:nodoc: suppress_logging_render_partial do - clear_content_for :kaminari_paginator_tags - @template.content_for :kaminari_paginator_tags, tagify_links.join.html_safe Paginator.new(self).to_s end end @@ -111,11 +100,6 @@ module Kaminari end end - # another dirty hack - def clear_content_for(name) - @template.instance_variable_get('@_content_for')[name] = ActiveSupport::SafeBuffer.new - end - class PageProxy def initialize(renderer, page, last) @renderer, @page, @last = renderer, page, last diff --git a/lib/kaminari/tags.rb b/lib/kaminari/tags.rb index 8461911..aad16c2 100644 --- a/lib/kaminari/tags.rb +++ b/lib/kaminari/tags.rb @@ -168,6 +168,10 @@ module Kaminari # The container tag class Paginator < Tag include Renderable + + def to_s(locals = {}) #:nodoc: + super locals.merge(:renderer => @renderer) + end end end end From a1a3d64a8da4bbce1df9dcc4fd465d4540689460 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Wed, 16 Feb 2011 07:21:56 +0900 Subject: [PATCH 5/9] bring link_tag generation logic back into the main logic --- app/views/kaminari/_paginator.html.erb | 8 +++++++- app/views/kaminari/_paginator.html.haml | 7 ++++++- lib/kaminari/helpers.rb | 27 +++++++++++++------------ 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/views/kaminari/_paginator.html.erb b/app/views/kaminari/_paginator.html.erb index e356926..4671500 100644 --- a/app/views/kaminari/_paginator.html.erb +++ b/app/views/kaminari/_paginator.html.erb @@ -12,7 +12,13 @@ <% if page.current? -%> <%= current_page_tag %> <% elsif page.left_outer? || page.right_outer? || page.inside_window? -%> - <%= page_link_tag %> + <% if page.first? -%> + <%= first_page_link_tag %> + <% elsif page.last? -%> + <%= last_page_link_tag %> + <% else -%> + <%= page_link_tag %> + <% end -%> <% elsif !page.was_truncated? -%> <%= truncated_span_tag %> <% end -%> diff --git a/app/views/kaminari/_paginator.html.haml b/app/views/kaminari/_paginator.html.haml index 1c6e481..cb62fca 100644 --- a/app/views/kaminari/_paginator.html.haml +++ b/app/views/kaminari/_paginator.html.haml @@ -11,7 +11,12 @@ - if page.current? = current_page_tag - elsif page.left_outer? || page.right_outer? || page.inside_window? - = page_link_tag + - if page.first? + = first_page_link_tag + - elsif page.last? + = last_page_link_tag + - else + = page_link_tag - elsif !page.was_truncated? = truncated_span_tag = num_pages > current_page ? next_link_tag : next_span_tag diff --git a/lib/kaminari/helpers.rb b/lib/kaminari/helpers.rb index 7531c90..4d7496b 100644 --- a/lib/kaminari/helpers.rb +++ b/lib/kaminari/helpers.rb @@ -13,19 +13,12 @@ module Kaminari @left, @window, @right = (options[:left] || options[:outer_window] || 1), (options[:window] || options[:inner_window] || 4), (options[:right] || options[:outer_window] || 1) end - def current_page_tag - @last = CurrentPage.new self, :page => @page - end - - def page_link_tag - @last = case @page - when 1 - FirstPageLink - when @options[:num_pages] - LastPageLink - else - PageLink - end.new self, :page => @page + %w[current_page first_page_link last_page_link page_link].each do |tag| + eval <<-DEF + def #{tag}_tag + @last = #{tag.classify}.new self, :page => @page + end + DEF end %w[prev_link prev_span next_link next_span truncated_span].each do |tag| @@ -109,6 +102,14 @@ module Kaminari @page == @renderer.options[:current_page] end + def first? + @page == 1 + end + + def last? + @page == @renderer.options[:num_pages] + end + def left_outer? @page <= @renderer.left + 1 end From 38ca8d4e6b5bc7e1da52047dd27dc052a736e22f Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Wed, 16 Feb 2011 07:33:58 +0900 Subject: [PATCH 6/9] what am I returning? --- lib/kaminari/helpers.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/kaminari/helpers.rb b/lib/kaminari/helpers.rb index 4d7496b..feca035 100644 --- a/lib/kaminari/helpers.rb +++ b/lib/kaminari/helpers.rb @@ -37,10 +37,7 @@ module Kaminari end def compose_tags(&block) #:nodoc: - num_pages, current_page = @options[:num_pages], @options[:current_page] - return [] if num_pages <= 1 - - instance_eval &block + instance_eval &block if @options[:num_pages] > 1 end def partial_exists?(name) #:nodoc: From 2353584af5444654e90cd7500c978572480dc77e Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Wed, 16 Feb 2011 22:52:55 +0900 Subject: [PATCH 7/9] refactor: split PaginationRenderer's responsibility --- lib/kaminari/helpers.rb | 75 +------------- lib/kaminari/tags.rb | 96 ++++++++++++++--- spec/helpers/helpers_spec.rb | 196 ++++++++++++++++++----------------- spec/helpers/tags_spec.rb | 111 ++++++++++++++++++++ 4 files changed, 298 insertions(+), 180 deletions(-) diff --git a/lib/kaminari/helpers.rb b/lib/kaminari/helpers.rb index feca035..82fca73 100644 --- a/lib/kaminari/helpers.rb +++ b/lib/kaminari/helpers.rb @@ -2,42 +2,13 @@ require File.join(File.dirname(__FILE__), 'tags') module Kaminari module Helpers - class PaginationRenderer - attr_reader :options, :params, :left, :window, :right + class TemplateWrapper + attr_reader :options, :params + delegate :render, :url_for, :to => :@template def initialize(template, options) #:nodoc: @template, @options = template, options - # so that this Renderer instance can actually "render". Black magic? - @output_buffer = @template.instance_variable_get('@output_buffer') @params = options[:params] ? template.params.merge(options.delete :params) : template.params - @left, @window, @right = (options[:left] || options[:outer_window] || 1), (options[:window] || options[:inner_window] || 4), (options[:right] || options[:outer_window] || 1) - end - - %w[current_page first_page_link last_page_link page_link].each do |tag| - eval <<-DEF - def #{tag}_tag - @last = #{tag.classify}.new self, :page => @page - end - DEF - end - - %w[prev_link prev_span next_link next_span truncated_span].each do |tag| - eval <<-DEF - def #{tag}_tag - @last = #{tag.classify}.new self - end - DEF - end - - def each_page - 1.upto(@options[:num_pages]) do |i| - @page = i - yield PageProxy.new(self, i, @last) - end - end - - def compose_tags(&block) #:nodoc: - instance_eval &block if @options[:num_pages] > 1 end def partial_exists?(name) #:nodoc: @@ -45,10 +16,8 @@ module Kaminari resolver.find_all(*args_for_lookup(name)).present? end - def to_s #:nodoc: - suppress_logging_render_partial do - Paginator.new(self).to_s - end + def output_buffer + @template.instance_variable_get('@output_buffer') end private @@ -89,40 +58,6 @@ module Kaminari blk.call end end - - class PageProxy - def initialize(renderer, page, last) - @renderer, @page, @last = renderer, page, last - end - - def current? - @page == @renderer.options[:current_page] - end - - def first? - @page == 1 - end - - def last? - @page == @renderer.options[:num_pages] - end - - def left_outer? - @page <= @renderer.left + 1 - end - - def right_outer? - @renderer.options[:num_pages] - @page <= @renderer.right - end - - def inside_window? - (@page - @renderer.options[:current_page]).abs <= @renderer.window - end - - def was_truncated? - @last.is_a? TruncatedSpan - end - end end # = Helpers diff --git a/lib/kaminari/tags.rb b/lib/kaminari/tags.rb index aad16c2..72d17d2 100644 --- a/lib/kaminari/tags.rb +++ b/lib/kaminari/tags.rb @@ -17,12 +17,12 @@ module Kaminari # installed template will be used. # e.g.) Paginator -> $GEM_HOME/kaminari-x.x.x/app/views/kaminari/_paginator.html.erb class Tag - def initialize(renderer, options = {}) #:nodoc: - @renderer, @options = renderer, renderer.options.merge(options) + def initialize(template, options = {}) #:nodoc: + @template, @options = template, template.options.merge(options) end def to_s(locals = {}) #:nodoc: - @renderer.render :partial => find_template, :locals => @options.merge(locals) + @template.render :partial => find_template, :locals => @options.merge(locals) end private @@ -41,13 +41,13 @@ module Kaminari # 3. the default one inside the engine def find_template self.class.ancestor_renderables.each do |klass| - return "kaminari/#{klass.template_filename}" if @renderer.partial_exists? klass.template_filename + return "kaminari/#{klass.template_filename}" if @template.partial_exists? klass.template_filename end "kaminari/#{self.class.template_filename}" end def page_url_for(page) - @renderer.url_for @renderer.params.merge(:page => (page <= 1 ? nil : page)) + @template.url_for @template.params.merge(:page => (page <= 1 ? nil : page)) end end @@ -65,6 +65,83 @@ module Kaminari end end + # The container tag + class Paginator < Tag + include Renderable + attr_reader :options + + def initialize(template, window_options) #:nodoc: + @template, @options = template, window_options.reverse_merge(template.options) + # so that this instance can actually "render". Black magic? + @output_buffer = @template.output_buffer + end + + def compose_tags(&block) #:nodoc: + instance_eval &block if @options[:num_pages] > 1 + end + + def each_page + 1.upto(@options[:num_pages]) do |i| + @page = i + yield PageProxy.new(options, i, @last) + end + end + + %w[current_page first_page_link last_page_link page_link].each do |tag| + eval <<-DEF + def #{tag}_tag + @last = #{tag.classify}.new @template, :page => @page + end + DEF + end + + %w[prev_link prev_span next_link next_span truncated_span].each do |tag| + eval <<-DEF + def #{tag}_tag + @last = #{tag.classify}.new @template + end + DEF + end + + def to_s(window_options = {}) #:nodoc: + super window_options.merge :paginator => self + end + + class PageProxy + def initialize(options, page, last) + @options, @page, @last = options, page, last + end + + def current? + @page == @options[:current_page] + end + + def first? + @page == 1 + end + + def last? + @page == @options[:num_pages] + end + + def left_outer? + @page <= @options[:left] + 1 + end + + def right_outer? + @options[:num_pages] - @page <= @options[:right] + end + + def inside_window? + (@page - @options[:current_page]).abs <= @options[:window] + end + + def was_truncated? + @last.is_a? TruncatedSpan + end + end + end + # A page module Page include Renderable @@ -164,14 +241,5 @@ module Kaminari class TruncatedSpan < Tag include NonLink end - - # The container tag - class Paginator < Tag - include Renderable - - def to_s(locals = {}) #:nodoc: - super locals.merge(:renderer => @renderer) - end - end end end diff --git a/spec/helpers/helpers_spec.rb b/spec/helpers/helpers_spec.rb index 0bad597..3601576 100644 --- a/spec/helpers/helpers_spec.rb +++ b/spec/helpers/helpers_spec.rb @@ -12,111 +12,115 @@ describe 'Kaminari::Helpers::PaginationRenderer' do end describe '#params' do - subject { PaginationRenderer.new(template, :params => {:controller => 'foo', :action => 'bar'}) } + before do + @renderer = PaginationRenderer.new(template, :params => {:controller => 'foo', :action => 'bar'}) + end + subject { @renderer.instance_variable_get '@template' } its(:params) { should == {:controller => 'foo', :action => 'bar'} } end - describe '#tagify_links' do - def tags_with(options) - PaginationRenderer.new(template, options).tagify_links - end + #TODO test somehow... +# describe '#tagify_links' do +# def tags_with(options) +# PaginationRenderer.new(template, options).tagify_links +# end - context '1 page in total' do - subject { tags_with :num_pages => 1, :current_page => 1 } - it { should have(0).tags } - end +# context '1 page in total' do +# subject { tags_with :num_pages => 1, :current_page => 1 } +# it { should have(0).tags } +# end - context '10 pages in total' do - context 'first page' do - subject { tags_with :num_pages => 10, :current_page => 1 } - it { should_not contain_tag PrevLink } - it { should contain_tag PrevSpan } - it { should contain_tag CurrentPage } - it { should_not contain_tag FirstPageLink } - it { should contain_tag LastPageLink } - it { should contain_tag PageLink } - it { should contain_tag NextLink } - it { should_not contain_tag NextSpan } - it { should contain_tag TruncatedSpan } - end +# context '10 pages in total' do +# context 'first page' do +# subject { tags_with :num_pages => 10, :current_page => 1 } +# it { should_not contain_tag PrevLink } +# it { should contain_tag PrevSpan } +# it { should contain_tag CurrentPage } +# it { should_not contain_tag FirstPageLink } +# it { should contain_tag LastPageLink } +# it { should contain_tag PageLink } +# it { should contain_tag NextLink } +# it { should_not contain_tag NextSpan } +# it { should contain_tag TruncatedSpan } +# end - context 'second page' do - subject { tags_with :num_pages => 10, :current_page => 2 } - it { should contain_tag PrevLink } - it { should_not contain_tag PrevSpan } - it { should contain_tag CurrentPage } - it { should contain_tag FirstPageLink } - it { should contain_tag LastPageLink } - it { should contain_tag PageLink } - it { should contain_tag NextLink } - it { should_not contain_tag NextSpan } - it { should contain_tag TruncatedSpan } - end +# context 'second page' do +# subject { tags_with :num_pages => 10, :current_page => 2 } +# it { should contain_tag PrevLink } +# it { should_not contain_tag PrevSpan } +# it { should contain_tag CurrentPage } +# it { should contain_tag FirstPageLink } +# it { should contain_tag LastPageLink } +# it { should contain_tag PageLink } +# it { should contain_tag NextLink } +# it { should_not contain_tag NextSpan } +# it { should contain_tag TruncatedSpan } +# end - context 'third page' do - subject { tags_with :num_pages => 10, :current_page => 3 } - it { should contain_tag PrevLink } - it { should_not contain_tag PrevSpan } - it { should contain_tag CurrentPage } - it { should contain_tag FirstPageLink } - it { should contain_tag LastPageLink } - it { should contain_tag PageLink } - it { should contain_tag NextLink } - it { should_not contain_tag NextSpan } - it { should contain_tag TruncatedSpan } - end +# context 'third page' do +# subject { tags_with :num_pages => 10, :current_page => 3 } +# it { should contain_tag PrevLink } +# it { should_not contain_tag PrevSpan } +# it { should contain_tag CurrentPage } +# it { should contain_tag FirstPageLink } +# it { should contain_tag LastPageLink } +# it { should contain_tag PageLink } +# it { should contain_tag NextLink } +# it { should_not contain_tag NextSpan } +# it { should contain_tag TruncatedSpan } +# end - context 'fourth page(no truncation)' do - subject { tags_with :num_pages => 10, :current_page => 4 } - it { should contain_tag PrevLink } - it { should_not contain_tag PrevSpan } - it { should contain_tag CurrentPage } - it { should contain_tag FirstPageLink } - it { should contain_tag LastPageLink } - it { should contain_tag PageLink } - it { should contain_tag NextLink } - it { should_not contain_tag NextSpan } - it { should_not contain_tag TruncatedSpan } - end +# context 'fourth page(no truncation)' do +# subject { tags_with :num_pages => 10, :current_page => 4 } +# it { should contain_tag PrevLink } +# it { should_not contain_tag PrevSpan } +# it { should contain_tag CurrentPage } +# it { should contain_tag FirstPageLink } +# it { should contain_tag LastPageLink } +# it { should contain_tag PageLink } +# it { should contain_tag NextLink } +# it { should_not contain_tag NextSpan } +# it { should_not contain_tag TruncatedSpan } +# end - context 'seventh page(no truncation)' do - subject { tags_with :num_pages => 10, :current_page => 7 } - it { should contain_tag PrevLink } - it { should_not contain_tag PrevSpan } - it { should contain_tag CurrentPage } - it { should contain_tag FirstPageLink } - it { should contain_tag LastPageLink } - it { should contain_tag PageLink } - it { should contain_tag NextLink } - it { should_not contain_tag NextSpan } - it { should_not contain_tag TruncatedSpan } - end +# context 'seventh page(no truncation)' do +# subject { tags_with :num_pages => 10, :current_page => 7 } +# it { should contain_tag PrevLink } +# it { should_not contain_tag PrevSpan } +# it { should contain_tag CurrentPage } +# it { should contain_tag FirstPageLink } +# it { should contain_tag LastPageLink } +# it { should contain_tag PageLink } +# it { should contain_tag NextLink } +# it { should_not contain_tag NextSpan } +# it { should_not contain_tag TruncatedSpan } +# end - context 'eighth page' do - subject { tags_with :num_pages => 10, :current_page => 8 } - it { should contain_tag PrevLink } - it { should_not contain_tag PrevSpan } - it { should contain_tag CurrentPage } - it { should contain_tag FirstPageLink } - it { should contain_tag LastPageLink } - it { should contain_tag PageLink } - it { should contain_tag NextLink } - it { should_not contain_tag NextSpan } - it { should contain_tag TruncatedSpan } - end +# context 'eighth page' do +# subject { tags_with :num_pages => 10, :current_page => 8 } +# it { should contain_tag PrevLink } +# it { should_not contain_tag PrevSpan } +# it { should contain_tag CurrentPage } +# it { should contain_tag FirstPageLink } +# it { should contain_tag LastPageLink } +# it { should contain_tag PageLink } +# it { should contain_tag NextLink } +# it { should_not contain_tag NextSpan } +# it { should contain_tag TruncatedSpan } +# end - context 'last page' do - subject { tags_with :num_pages => 10, :current_page => 10 } - it { should contain_tag PrevLink } - it { should_not contain_tag PrevSpan } - it { should contain_tag CurrentPage } - it { should contain_tag FirstPageLink } - it { should_not contain_tag LastPageLink } - it { should contain_tag PageLink } - it { should_not contain_tag NextLink } - it { should contain_tag NextSpan } - it { should contain_tag TruncatedSpan } - end - end - end +# context 'last page' do +# subject { tags_with :num_pages => 10, :current_page => 10 } +# it { should contain_tag PrevLink } +# it { should_not contain_tag PrevSpan } +# it { should contain_tag CurrentPage } +# it { should contain_tag FirstPageLink } +# it { should_not contain_tag LastPageLink } +# it { should contain_tag PageLink } +# it { should_not contain_tag NextLink } +# it { should contain_tag NextSpan } +# it { should contain_tag TruncatedSpan } +# end +# end +# end end diff --git a/spec/helpers/tags_spec.rb b/spec/helpers/tags_spec.rb index 7fe7265..3a60663 100644 --- a/spec/helpers/tags_spec.rb +++ b/spec/helpers/tags_spec.rb @@ -44,4 +44,115 @@ describe 'Kaminari::Helpers' do its(:ancestor_renderables) { should == [NextSpan, Next, NonLink] } end end + + describe 'Paginator' do + describe 'Paginator::PageProxy' do + describe '#current?' do + context 'current_page == page' do + subject { Paginator::PageProxy.new({:current_page => 26}, 26, nil) } + its(:current?) { should be_true } + end + context 'current_page != page' do + subject { Paginator::PageProxy.new({:current_page => 13}, 26, nil) } + its(:current?) { should_not be_true } + end + end + + describe '#first?' do + context 'page == 1' do + subject { Paginator::PageProxy.new({:current_page => 26}, 1, nil) } + its(:first?) { should be_true } + end + context 'page != 1' do + subject { Paginator::PageProxy.new({:current_page => 13}, 2, nil) } + its(:first?) { should_not be_true } + end + end + + describe '#last?' do + context 'current_page == page' do + subject { Paginator::PageProxy.new({:num_pages => 39}, 39, nil) } + its(:last?) { should be_true } + end + context 'current_page != page' do + subject { Paginator::PageProxy.new({:num_pages => 39}, 38, nil) } + its(:last?) { should_not be_true } + end + end + + describe '#left_outer?' do + context 'current_page == left' do + subject { Paginator::PageProxy.new({:left => 3}, 3, nil) } + its(:left_outer?) { should be_true } + end + context 'current_page == left + 1' do + subject { Paginator::PageProxy.new({:left => 3}, 4, nil) } + its(:left_outer?) { should be_true } + end + context 'current_page == left + 2' do + subject { Paginator::PageProxy.new({:left => 3}, 5, nil) } + its(:left_outer?) { should_not be_true } + end + end + + describe '#right_outer?' do + context 'num_pages - page > right' do + subject { Paginator::PageProxy.new({:num_pages => 10, :right => 3}, 6, nil) } + its(:right_outer?) { should_not be_true } + end + context 'num_pages - page == right' do + subject { Paginator::PageProxy.new({:num_pages => 10, :right => 3}, 7, nil) } + its(:right_outer?) { should be_true } + end + context 'num_pages - page < right' do + subject { Paginator::PageProxy.new({:num_pages => 10, :right => 3}, 8, nil) } + its(:right_outer?) { should be_true } + end + end + + describe '#inside_window?' do + context 'page > current_page' do + context 'page - current_page > window' do + subject { Paginator::PageProxy.new({:current_page => 4, :window => 5}, 10, nil) } + its(:inside_window?) { should_not be_true } + end + context 'page - current_page == window' do + subject { Paginator::PageProxy.new({:current_page => 4, :window => 6}, 10, nil) } + its(:inside_window?) { should be_true } + end + context 'page - current_page < window' do + subject { Paginator::PageProxy.new({:current_page => 4, :window => 7}, 10, nil) } + its(:inside_window?) { should be_true } + end + end + context 'current_page > page' do + context 'current_page - page > window' do + subject { Paginator::PageProxy.new({:current_page => 15, :window => 4}, 10, nil) } + its(:inside_window?) { should_not be_true } + end + context 'current_page - page == window' do + subject { Paginator::PageProxy.new({:current_page => 15, :window => 5}, 10, nil) } + its(:inside_window?) { should be_true } + end + context 'current_page - page < window' do + subject { Paginator::PageProxy.new({:current_page => 15, :window => 6}, 10, nil) } + its(:inside_window?) { should be_true } + end + end + end + describe '#was_truncated?' do + before do + stub(@template = Object.new).options { {} } + end + context 'last.is_a? TruncatedSpan' do + subject { Paginator::PageProxy.new({}, 10, TruncatedSpan.new(@template)) } + its(:was_truncated?) { should be_true } + end + context 'last.is not a TruncatedSpan' do + subject { Paginator::PageProxy.new({}, 10, PageLink.new(@template)) } + its(:was_truncated?) { should_not be_true } + end + end + end + end end From b4263a82649d2ad0d2b83b08634036b4e36b83c2 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Wed, 16 Feb 2011 23:24:31 +0900 Subject: [PATCH 8/9] cosmetics --- app/views/kaminari/_paginator.html.erb | 3 ++- app/views/kaminari/_paginator.html.haml | 3 ++- lib/kaminari/helpers.rb | 26 ++++++++++++++++++++----- lib/kaminari/tags.rb | 19 ++++++++++++++++-- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/app/views/kaminari/_paginator.html.erb b/app/views/kaminari/_paginator.html.erb index 4671500..da7bb70 100644 --- a/app/views/kaminari/_paginator.html.erb +++ b/app/views/kaminari/_paginator.html.erb @@ -4,8 +4,9 @@ num_pages: total number of pages per_page: number of items to fetch per page remote: data-remote + paginator: the paginator that renders the pagination tags inside -%> -<% paginator.compose_tags do -%> +<% paginator.render do -%>