From 9f3d3944cbcdfaee736db926c4df98ade9a6b7c3 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sun, 13 Feb 2011 22:55:31 +0900 Subject: [PATCH] refactor tags (Part II) * prefer more testable and clearer way * actually fixed a bug that Link was_not a Page. now everything conforms to the document, I believe --- lib/kaminari/tags.rb | 19 +++++++----- spec/helpers/tags_spec.rb | 63 +++++++++++++++++++++++++++------------ 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/lib/kaminari/tags.rb b/lib/kaminari/tags.rb index 72cd0978c..f4809f21f 100644 --- a/lib/kaminari/tags.rb +++ b/lib/kaminari/tags.rb @@ -26,19 +26,24 @@ def to_s(locals = {}) #:nodoc: end private + def self.ancestor_renderables + arr = [] + ancestors.each do |klass| + arr << klass if klass != Renderable + return arr if klass == Tag + end + end + # OMG yet another super dirty hack # this method finds # 1. a template for the given class from app/views # 2. a template for its parent class from app/views # 3. the default one inside the engine - def find_template(klass = self.class) - if @renderer.partial_exists? klass.template_filename - "kaminari/#{klass.template_filename}" - elsif (parent = klass.ancestors[1]) == Renderable - "kaminari/#{self.class.template_filename}" - else - find_template parent + def find_template + self.class.ancestor_renderables.each do |klass| + return "kaminari/#{klass.template_filename}" if @renderer.partial_exists? klass.template_filename end + "kaminari/#{self.class.template_filename}" end def page_url_for(page) diff --git a/spec/helpers/tags_spec.rb b/spec/helpers/tags_spec.rb index eec147ed7..add4cbfe5 100644 --- a/spec/helpers/tags_spec.rb +++ b/spec/helpers/tags_spec.rb @@ -2,25 +2,50 @@ include Kaminari::Helpers describe 'Kaminari::Helpers' do - let :renderer do - stub(r = Object.new) do - render.with_any_args - options { {} } - params { {} } - partial_exists?.with_any_args {|a| puts a; false } - url_for {|h| "/foo?page=#{h[:page]}"} - end - r - end - describe 'PageLink' do - subject { PageLink.new renderer, :page => 3 } - its('class.template_filename') { should == 'page_link' } - describe 'template lookup rule' do - before do - pending "spies doesn't work on RSpec 2 ATM: https://github.com/btakita/rr/issues#issue/45" - subject.to_s - end - specify { renderer.should have_received.partial_exists? PageLink } + describe 'template lookup rule' do + describe 'Tag' do + subject { Tag } + its(:ancestor_renderables) { should == [Tag] } + end + describe 'Paginator' do + subject { Paginator } + its(:ancestor_renderables) { should == [Paginator, Tag] } + end + describe 'PrevLink' do + subject { PrevLink } + its(:ancestor_renderables) { should == [PrevLink, Prev, Link, Page, Tag] } + end + describe 'PrevSpan' do + subject { PrevSpan } + its(:ancestor_renderables) { should == [PrevSpan, Prev, NonLink, Tag] } + end + describe 'FirstPageLink' do + subject { FirstPageLink } + its(:ancestor_renderables) { should == [FirstPageLink, PageLink, Link, Page, Tag] } + end + describe 'PageLink' do + subject { PageLink } + its(:ancestor_renderables) { should == [PageLink, Link, Page, Tag] } + end + describe 'CurrentPage' do + subject { CurrentPage } + its(:ancestor_renderables) { should == [CurrentPage, NonLink, Page, Tag] } + end + describe 'TruncatedSpan' do + subject { TruncatedSpan } + its(:ancestor_renderables) { should == [TruncatedSpan, NonLink, Tag] } + end + describe 'LastPageLink' do + subject { LastPageLink } + its(:ancestor_renderables) { should == [LastPageLink, PageLink, Link, Page, Tag] } + end + describe 'NextLink' do + subject { NextLink } + its(:ancestor_renderables) { should == [NextLink, Next, Link, Page, Tag] } + end + describe 'NextSpan' do + subject { NextSpan } + its(:ancestor_renderables) { should == [NextSpan, Next, NonLink, Tag] } end end end