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
This commit is contained in:
Akira Matsuda 2011-02-13 22:55:31 +09:00
parent 2e88f6b456
commit 9f3d3944cb
2 changed files with 55 additions and 25 deletions

View File

@ -26,19 +26,24 @@ module Kaminari
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)

View File

@ -2,25 +2,50 @@ require File.expand_path('../spec_helper', File.dirname(__FILE__))
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]}"}
describe 'template lookup rule' do
describe 'Tag' do
subject { Tag }
its(:ancestor_renderables) { should == [Tag] }
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 '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