mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
introduce modules to characterize each tag
This commit is contained in:
parent
0a80cda663
commit
dd77a7dcb3
2 changed files with 93 additions and 18 deletions
|
@ -17,10 +17,6 @@ module Kaminari
|
||||||
# template will be used.
|
# template will be used.
|
||||||
# e.g.) Paginator -> $GEM_HOME/kaminari-x.x.x/app/views/kaminari/_paginator.html.erb
|
# e.g.) Paginator -> $GEM_HOME/kaminari-x.x.x/app/views/kaminari/_paginator.html.erb
|
||||||
class Tag
|
class Tag
|
||||||
def self.template_filename #:nodoc:
|
|
||||||
name.demodulize.underscore
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize(renderer, options = {}) #:nodoc:
|
def initialize(renderer, options = {}) #:nodoc:
|
||||||
@renderer, @options = renderer, renderer.options.merge(options)
|
@renderer, @options = renderer, renderer.options.merge(options)
|
||||||
end
|
end
|
||||||
|
@ -38,7 +34,7 @@ module Kaminari
|
||||||
def find_template(klass = self.class)
|
def find_template(klass = self.class)
|
||||||
if @renderer.partial_exists? klass.template_filename
|
if @renderer.partial_exists? klass.template_filename
|
||||||
"kaminari/#{klass.template_filename}"
|
"kaminari/#{klass.template_filename}"
|
||||||
elsif (parent = klass.ancestors[1]) == Tag
|
elsif (parent = klass.ancestors[1]) == Renderable
|
||||||
"kaminari/#{self.class.template_filename}"
|
"kaminari/#{self.class.template_filename}"
|
||||||
else
|
else
|
||||||
find_template parent
|
find_template parent
|
||||||
|
@ -50,56 +46,109 @@ module Kaminari
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module Renderable #:nodoc:
|
||||||
|
def self.included(base) #:nodoc:
|
||||||
|
base.extend ClassMethods
|
||||||
|
end
|
||||||
|
module ClassMethods #:nodoc:
|
||||||
|
def template_filename #:nodoc:
|
||||||
|
name.demodulize.underscore
|
||||||
|
end
|
||||||
|
def included(base) #:nodoc:
|
||||||
|
base.extend Renderable::ClassMethods
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Tag that contains a link
|
||||||
|
module Link
|
||||||
|
include Renderable
|
||||||
|
def url
|
||||||
|
raise 'Override url with the actual url value to be a Link.'
|
||||||
|
end
|
||||||
|
def to_s(locals = {}) #:nodoc:
|
||||||
|
super locals.merge(:url => url)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Tag that doesn't contain a link
|
||||||
|
module NonLink
|
||||||
|
include Renderable
|
||||||
|
end
|
||||||
|
|
||||||
|
# Tag for a page
|
||||||
|
module Page
|
||||||
|
def page
|
||||||
|
raise 'Override page with the actual page value to be a Page.'
|
||||||
|
end
|
||||||
|
def to_s(locals = {}) #:nodoc:
|
||||||
|
super locals.merge(:page => page)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# "Previous" without link
|
# "Previous" without link
|
||||||
class PrevSpan < Tag
|
class PrevSpan < Tag
|
||||||
|
include NonLink
|
||||||
end
|
end
|
||||||
|
|
||||||
# "Previous" with link
|
# "Previous" with link
|
||||||
class PrevLink < Tag
|
class PrevLink < Tag
|
||||||
def to_s #:nodoc:
|
include Link
|
||||||
super :prev_url => page_url_for(@options[:current_page] - 1)
|
def url #:nodoc:
|
||||||
|
page_url_for @options[:current_page] - 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# "Next" without link
|
# "Next" without link
|
||||||
class NextSpan < Tag
|
class NextSpan < Tag
|
||||||
|
include NonLink
|
||||||
end
|
end
|
||||||
|
|
||||||
# "Next" with link
|
# "Next" with link
|
||||||
class NextLink < Tag
|
class NextLink < Tag
|
||||||
def to_s #:nodoc:
|
include Link
|
||||||
super :next_url => page_url_for(@options[:current_page] + 1)
|
def url #:nodoc:
|
||||||
|
page_url_for @options[:current_page] + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# A link showing page number
|
# Link showing page number
|
||||||
class PageLink < Tag
|
class PageLink < Tag
|
||||||
def to_s #:nodoc:
|
include Page
|
||||||
super :page_url => page_url_for(@options[:page])
|
include Link
|
||||||
|
def page #:nodoc:
|
||||||
|
@options[:page]
|
||||||
|
end
|
||||||
|
def url #:nodoc:
|
||||||
|
page_url_for page
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# A non-link tag showing the current page number
|
# Non-link tag showing the current page number
|
||||||
class CurrentPage < Tag
|
class CurrentPage < Tag
|
||||||
def to_s #:nodoc:
|
include Page
|
||||||
super :page_url => page_url_for(@options[:page])
|
include NonLink
|
||||||
|
def page #:nodoc:
|
||||||
|
@options[:page]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# A link with page number that appears at the leftmost
|
# Link with page number that appears at the leftmost
|
||||||
class FirstPageLink < PageLink
|
class FirstPageLink < PageLink
|
||||||
end
|
end
|
||||||
|
|
||||||
# A link with page number that appears at the rightmost
|
# Link with page number that appears at the rightmost
|
||||||
class LastPageLink < PageLink
|
class LastPageLink < PageLink
|
||||||
end
|
end
|
||||||
|
|
||||||
# A non-link tag that stands for skipped pages...
|
# Non-link tag that stands for skipped pages...
|
||||||
class TruncatedSpan < Tag
|
class TruncatedSpan < Tag
|
||||||
|
include NonLink
|
||||||
end
|
end
|
||||||
|
|
||||||
# The container tag
|
# The container tag
|
||||||
class Paginator < Tag
|
class Paginator < Tag
|
||||||
|
include Renderable
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
26
spec/helpers/tags_spec.rb
Normal file
26
spec/helpers/tags_spec.rb
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
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]}"}
|
||||||
|
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 }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue