Skip to content

Commit

Permalink
refactor tags (Part II)
Browse files Browse the repository at this point in the history
* prefer more testable and clearer way
* actually fixed a bug that Link was_not a Page. now everything conforms to the document, I believe
  • Loading branch information
amatsuda committed Feb 13, 2011
1 parent 2e88f6b commit 9f3d394
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 26 deletions.
19 changes: 12 additions & 7 deletions lib/kaminari/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
63 changes: 44 additions & 19 deletions spec/helpers/tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9f3d394

Please sign in to comment.