Sha256: d77ffaac3d22001597a0a8341f6407a9c7ae99dd1e49e90231d93b6c14298a5a

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 KB

Contents

module Pagers
  module ActionView
    module Base
      extend ActiveSupport::Concern

      def page_path(page, options={})
        parameter = options[:parameter] || Rails.application.config.pagers.parameter
        request.path + '?' + request.query_parameters.merge(parameter => page).to_query
      end

      def paginate(collection, options={})
        pages = options[:pages] || Rails.application.config.pagers.pages
        if collection.total_pages >= pages
          middle = (pages / 2).ceil
          if collection.current_page > (collection.total_pages - middle)
            starts_at = collection.total_pages - pages + 1
            ends_at = collection.total_pages
          elsif collection.current_page <= middle
            starts_at = 1
            ends_at = pages
          else
            starts_at = collection.current_page - middle
            ends_at = collection.current_page + middle
          end
        else
          starts_at = 1
          ends_at = collection.total_pages
        end
        render partial: 'pagers/main', locals: {
          collection: collection,
          options: options,
          pages: Range.new(starts_at, ends_at),
          less_pages: starts_at > 1,
          more_pages: ends_at < collection.total_pages
        }
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pagers-3.0.3 lib/pagers/action_view/base.rb
pagers-3.0.2 lib/pagers/action_view/base.rb
pagers-3.0.1 lib/pagers/action_view/base.rb
pagers-3.0.0 lib/pagers/action_view/base.rb