Sha256: 65478eea7aaa14e0fa4ddc2e70fefb2dce68a4ca19b6dbcc8cbe16cdaec62f8e

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

module Paginate
  module Renderer
    class Base
      # Set the pagination options.
      attr_reader :options

      # Set the view context. You can use this object
      # to call view and url helpers.
      attr_reader :view_context

      # Set the object with defines all pagination methods
      # like `Paginate::Base#next_page?`.
      attr_reader :processor

      def initialize(view_context, options)
        @view_context = view_context
        @options = options.reverse_merge(Paginate::Config.to_hash)
        @processor = Paginate::Base.new(nil, options)
      end

      # Return the URL for previous page.
      def previous_url
        url_for(processor.page - 1)
      end

      # Return the URL for next page.
      def next_url
        url_for(processor.page + 1)
      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]

        if url.respond_to?(:call)
          url = url.call(page).to_s.dup
        else
          url = url.dup

          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])
        end

        url.gsub!(/&/, "&amp;")
        url
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
paginate-3.0.0 lib/paginate/renderer.rb