kaminari--kaminari/lib/kaminari/tags.rb

155 lines
4.1 KiB
Ruby
Raw Normal View History

2011-02-10 18:45:43 +00:00
module Kaminari
module Helpers
2011-02-10 23:22:59 +00:00
# A tag stands for an HTML tag inside the paginator.
# Basically, a tag has its own partial template file, so every tag can be
# rendered into String using its partial template.
#
# The template file should be placed in your app/views/kaminari/ directory
# with underscored class name (besides the "Tag" class. Tag is an abstract
# class, so _tag parital is not needed).
# e.g.) PrevLink -> app/views/kaminari/_prev_link.html.erb
#
# If the template file does not exist, it falls back to ancestor classes.
# e.g.) FirstPageLink -> app/views/kaminari/_first_page_link.html.erb
# -> app/views/kaminari/_page_link.html.erb
#
2011-02-11 10:13:02 +00:00
# When no matching template were found in your app, finally the engine's pre
# installed template will be used.
2011-02-10 23:22:59 +00:00
# e.g.) Paginator -> $GEM_HOME/kaminari-x.x.x/app/views/kaminari/_paginator.html.erb
2011-02-10 18:45:43 +00:00
class Tag
2011-02-10 23:22:59 +00:00
def initialize(renderer, options = {}) #:nodoc:
2011-02-10 18:45:43 +00:00
@renderer, @options = renderer, renderer.options.merge(options)
end
2011-02-10 23:22:59 +00:00
def to_s(locals = {}) #:nodoc:
2011-02-10 18:45:43 +00:00
@renderer.render :partial => find_template, :locals => @options.merge(locals)
end
private
# 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
2011-02-10 18:45:43 +00:00
"kaminari/#{klass.template_filename}"
elsif (parent = klass.ancestors[1]) == Renderable
2011-02-10 18:45:43 +00:00
"kaminari/#{self.class.template_filename}"
else
find_template parent
end
end
def page_url_for(page)
@renderer.url_for @renderer.params.merge(:page => (page <= 1 ? nil : page))
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
2011-02-10 23:22:59 +00:00
# "Previous" without link
2011-02-10 18:45:43 +00:00
class PrevSpan < Tag
include NonLink
2011-02-10 18:45:43 +00:00
end
2011-02-10 23:22:59 +00:00
# "Previous" with link
2011-02-10 18:45:43 +00:00
class PrevLink < Tag
include Link
def url #:nodoc:
page_url_for @options[:current_page] - 1
2011-02-10 18:45:43 +00:00
end
end
2011-02-10 23:22:59 +00:00
# "Next" without link
2011-02-10 18:45:43 +00:00
class NextSpan < Tag
include NonLink
2011-02-10 18:45:43 +00:00
end
2011-02-10 23:22:59 +00:00
# "Next" with link
2011-02-10 18:45:43 +00:00
class NextLink < Tag
include Link
def url #:nodoc:
page_url_for @options[:current_page] + 1
2011-02-10 18:45:43 +00:00
end
end
# Link showing page number
2011-02-10 18:45:43 +00:00
class PageLink < Tag
include Page
include Link
def page #:nodoc:
@options[:page]
end
def url #:nodoc:
page_url_for page
2011-02-10 18:45:43 +00:00
end
end
# Non-link tag showing the current page number
2011-02-10 18:45:43 +00:00
class CurrentPage < Tag
include Page
include NonLink
def page #:nodoc:
@options[:page]
2011-02-10 18:45:43 +00:00
end
end
# Link with page number that appears at the leftmost
2011-02-10 18:45:43 +00:00
class FirstPageLink < PageLink
end
# Link with page number that appears at the rightmost
2011-02-10 18:45:43 +00:00
class LastPageLink < PageLink
end
# Non-link tag that stands for skipped pages...
2011-02-10 18:45:43 +00:00
class TruncatedSpan < Tag
include NonLink
2011-02-10 18:45:43 +00:00
end
2011-02-10 23:22:59 +00:00
# The container tag
2011-02-10 18:45:43 +00:00
class Paginator < Tag
include Renderable
2011-02-10 18:45:43 +00:00
end
end
end