Skip to content

Commit

Permalink
refactor: split PaginationRenderer's responsibility
Browse files Browse the repository at this point in the history
  • Loading branch information
amatsuda committed Feb 16, 2011
1 parent 38ca8d4 commit 2353584
Show file tree
Hide file tree
Showing 4 changed files with 298 additions and 180 deletions.
75 changes: 5 additions & 70 deletions lib/kaminari/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,22 @@

module Kaminari
module Helpers
class PaginationRenderer
attr_reader :options, :params, :left, :window, :right
class TemplateWrapper
attr_reader :options, :params
delegate :render, :url_for, :to => :@template

def initialize(template, options) #:nodoc:
@template, @options = template, options
# so that this Renderer instance can actually "render". Black magic?
@output_buffer = @template.instance_variable_get('@output_buffer')
@params = options[:params] ? template.params.merge(options.delete :params) : template.params
@left, @window, @right = (options[:left] || options[:outer_window] || 1), (options[:window] || options[:inner_window] || 4), (options[:right] || options[:outer_window] || 1)
end

%w[current_page first_page_link last_page_link page_link].each do |tag|
eval <<-DEF
def #{tag}_tag
@last = #{tag.classify}.new self, :page => @page
end
DEF
end

%w[prev_link prev_span next_link next_span truncated_span].each do |tag|
eval <<-DEF
def #{tag}_tag
@last = #{tag.classify}.new self
end
DEF
end

def each_page
1.upto(@options[:num_pages]) do |i|
@page = i
yield PageProxy.new(self, i, @last)
end
end

def compose_tags(&block) #:nodoc:
instance_eval &block if @options[:num_pages] > 1
end

def partial_exists?(name) #:nodoc:
resolver = context.instance_variable_get('@view_paths').first
resolver.find_all(*args_for_lookup(name)).present?
end

def to_s #:nodoc:
suppress_logging_render_partial do
Paginator.new(self).to_s
end
def output_buffer
@template.instance_variable_get('@output_buffer')
end

private
Expand Down Expand Up @@ -89,40 +58,6 @@ class << subscriber
blk.call
end
end

class PageProxy
def initialize(renderer, page, last)
@renderer, @page, @last = renderer, page, last
end

def current?
@page == @renderer.options[:current_page]
end

def first?
@page == 1
end

def last?
@page == @renderer.options[:num_pages]
end

def left_outer?
@page <= @renderer.left + 1
end

def right_outer?
@renderer.options[:num_pages] - @page <= @renderer.right
end

def inside_window?
(@page - @renderer.options[:current_page]).abs <= @renderer.window
end

def was_truncated?
@last.is_a? TruncatedSpan
end
end
end

# = Helpers
Expand Down
96 changes: 82 additions & 14 deletions lib/kaminari/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ module Helpers
# installed template will be used.
# e.g.) Paginator -> $GEM_HOME/kaminari-x.x.x/app/views/kaminari/_paginator.html.erb
class Tag
def initialize(renderer, options = {}) #:nodoc:
@renderer, @options = renderer, renderer.options.merge(options)
def initialize(template, options = {}) #:nodoc:
@template, @options = template, template.options.merge(options)
end

def to_s(locals = {}) #:nodoc:
@renderer.render :partial => find_template, :locals => @options.merge(locals)
@template.render :partial => find_template, :locals => @options.merge(locals)
end

private
Expand All @@ -41,13 +41,13 @@ def self.ancestor_renderables
# 3. the default one inside the engine
def find_template
self.class.ancestor_renderables.each do |klass|
return "kaminari/#{klass.template_filename}" if @renderer.partial_exists? klass.template_filename
return "kaminari/#{klass.template_filename}" if @template.partial_exists? klass.template_filename
end
"kaminari/#{self.class.template_filename}"
end

def page_url_for(page)
@renderer.url_for @renderer.params.merge(:page => (page <= 1 ? nil : page))
@template.url_for @template.params.merge(:page => (page <= 1 ? nil : page))
end
end

Expand All @@ -65,6 +65,83 @@ def included(base) #:nodoc:
end
end

# The container tag
class Paginator < Tag
include Renderable
attr_reader :options

def initialize(template, window_options) #:nodoc:
@template, @options = template, window_options.reverse_merge(template.options)
# so that this instance can actually "render". Black magic?
@output_buffer = @template.output_buffer
end

def compose_tags(&block) #:nodoc:
instance_eval &block if @options[:num_pages] > 1
end

def each_page
1.upto(@options[:num_pages]) do |i|
@page = i
yield PageProxy.new(options, i, @last)
end
end

%w[current_page first_page_link last_page_link page_link].each do |tag|
eval <<-DEF
def #{tag}_tag
@last = #{tag.classify}.new @template, :page => @page
end
DEF
end

%w[prev_link prev_span next_link next_span truncated_span].each do |tag|
eval <<-DEF
def #{tag}_tag
@last = #{tag.classify}.new @template
end
DEF
end

def to_s(window_options = {}) #:nodoc:
super window_options.merge :paginator => self
end

class PageProxy
def initialize(options, page, last)
@options, @page, @last = options, page, last
end

def current?
@page == @options[:current_page]
end

def first?
@page == 1
end

def last?
@page == @options[:num_pages]
end

def left_outer?
@page <= @options[:left] + 1
end

def right_outer?
@options[:num_pages] - @page <= @options[:right]
end

def inside_window?
(@page - @options[:current_page]).abs <= @options[:window]
end

def was_truncated?
@last.is_a? TruncatedSpan
end
end
end

# A page
module Page
include Renderable
Expand Down Expand Up @@ -164,14 +241,5 @@ class LastPageLink < PageLink
class TruncatedSpan < Tag
include NonLink
end

# The container tag
class Paginator < Tag
include Renderable

def to_s(locals = {}) #:nodoc:
super locals.merge(:renderer => @renderer)
end
end
end
end
Loading

0 comments on commit 2353584

Please sign in to comment.