lib/paginate/renderer.rb in paginate-2.0.0 vs lib/paginate/renderer.rb in paginate-3.0.0
- old
+ new
@@ -1,73 +1,57 @@
module Paginate
- class Renderer
- attr_accessor :options
- attr_accessor :current_page
+ module Renderer
+ class Base
+ # Set the pagination options.
+ attr_reader :options
- def initialize(options)
- @current_page = [options[:page].to_i, 1].max
- options.reverse_merge!(Paginate::Config.to_hash)
- @options = options.merge(:page => current_page)
- end
+ # Set the view context. You can use this object
+ # to call view and url helpers.
+ attr_reader :view_context
- def processor
- @processor ||= Paginate::Base.new(options)
- end
+ # Set the object with defines all pagination methods
+ # like `Paginate::Base#next_page?`.
+ attr_reader :processor
- def url_for(page)
- url = options[:url] || options[:fullpath]
+ def initialize(view_context, options)
+ @view_context = view_context
+ @options = options.reverse_merge(Paginate::Config.to_hash)
+ @processor = Paginate::Base.new(nil, options)
+ end
- if url.respond_to?(:call)
- url = url.call(page).to_s.dup
- else
- url = url.dup
+ # Return the URL for previous page.
+ def previous_url
+ url_for(processor.page - 1)
+ end
- re = Regexp.new("([&?])#{Regexp.escape(options[:param_name].to_s)}=[^&]*")
- url.gsub!(re, "\\1")
- url.gsub!(/[\?&]$/, "")
- url.gsub!(/&+/, "&")
- url.gsub!(/\?&/, "?")
-
- url << (url =~ /\?/ ? "&" : "?")
- url << page.to_query(options[:param_name])
+ # Return the URL for next page.
+ def next_url
+ url_for(processor.page + 1)
end
- url.gsub!(/&/, "&")
- url
- end
+ # Compute the URL for a given page.
+ # It will keep track of all query string and replace the
+ # page parameter with the specified `page`.
+ def url_for(page)
+ url = options[:url] || options[:fullpath]
- def render
- html = String.new
- previous_label = I18n.t("paginate.previous")
- previous_url = url_for(options[:page] - 1)
- next_label = I18n.t("paginate.next")
- next_url = url_for(options[:page] + 1)
- page_label = I18n.t("paginate.page", options)
+ if url.respond_to?(:call)
+ url = url.call(page).to_s.dup
+ else
+ url = url.dup
- css = %w[ paginate ]
- css << "disabled" unless processor.previous_page? || processor.next_page?
+ re = Regexp.new("([&?])#{Regexp.escape(options[:param_name].to_s)}=[^&]*")
+ url.gsub!(re, "\\1")
+ url.gsub!(/[\?&]$/, "")
+ url.gsub!(/&+/, "&")
+ url.gsub!(/\?&/, "?")
- html << %[<ul class="#{css.join(" ")}">]
+ url << (url =~ /\?/ ? "&" : "?")
+ url << page.to_query(options[:param_name])
+ end
- # Previous page
- if processor.previous_page?
- html << %[<li class="previous-page"><a href="#{previous_url}" title="#{previous_label}">#{previous_label}</a></li>]
- else
- html << %[<li class="previous-page disabled"><span title="#{previous_label}">#{previous_label}</span></li>]
+ url.gsub!(/&/, "&")
+ url
end
-
- # Current page
- html << %[<li class="page"><span>#{page_label}</span></li>]
-
- # Next page
- if processor.next_page?
- html << %[<li class="next-page"><a href="#{next_url}" title="#{next_label}">#{next_label}</a></li>]
- else
- html << %[<li class="next-page disabled"><span title="#{next_label}">#{next_label}</span></li>]
- end
-
- html << %[</ul>]
-
- html.html_safe
end
end
end