Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

..and the old Pagination module has been refactored out of existence

  • Loading branch information...
commit 4426fbcb080bb296e4d302aecb13dbd02971a04d 1 parent e030adf
Inge Jørgensen authored
2  app/models/exchange.rb
View
@@ -1,7 +1,5 @@
# encoding: utf-8
-require 'pagination'
-
# = Exchange
#
# Exchange is the base class for all threads, which both Discussion and Conversation inherit from.
74 lib/pagination.rb
View
@@ -1,74 +0,0 @@
-# encoding: utf-8
-
-require File.join(File.dirname(__FILE__), 'pagination/paginater')
-require File.join(File.dirname(__FILE__), 'pagination/instance_methods')
-
-# = Pagination
-#
-# The Pagination module provides simple pagination for any set of records.
-#
-# === Example
-#
-# class Discussion
-# has_many :posts
-#
-# # Finds paginated posts
-# def paginated_posts(options={})
-# Pagination.paginate(
-# :total_count => Post.count(:all, :conditions => ['discussion_id = ?', this.id]),
-# :per_page => options[:per_page] || 50,
-# :page => options[:page] || 1
-# ) do |pagination|
-# Post.find(
-# :all,
-# :conditions => ['discussion_id = ?', this.id]
-# :limit => pagination.limit,
-# :offset => pagination.offset,
-# :order => 'created_at ASC',
-# )
-# end
-# end
-# end
-#
-# You can now load paginated posts by doing:
-# @posts = @discussion.paginated_posts(:page => params[:page])
-#
-# The posts array will have Pagination::InstanceMethods mixed in, which
-# means you can do:
-#
-# @posts.total_count # => 156
-# @posts.pages # => 4
-# @posts.page # => 1
-# @posts.next_page # => 2
-# @posts.previous_page? # => false
-# @posts.nearest_pages(3) # => [1,2,3]
-#
-# === Wrapping incompatible pagination
-#
-# Some plugins provide their own pagination.
-# You can create a Paginater object and manually apply this to the collection
-# in order to reuse your pagination view code:
-#
-# posts = Post.search('music', :per_page => 20, :page => 1)
-# paginater = Pagination::Paginater.new(:total_count => posts.total_entries, :page => 1, :per_page => 20)
-# Pagination.apply(posts, paginater)
-
-
-module Pagination
- class << self
- # Applies pagination to the result set returned from the given block.
- # See the <tt>Pagination</tt> module for usage examples.
- def paginate(options, &block)
- paginater = Pagination::Paginater.new(options)
- collection = yield(paginater)
- self.apply(collection, paginater)
- end
-
- # Applies a paginater to a collection.
- def apply(collection, paginater)
- class << collection; include Pagination::InstanceMethods; end
- collection.paginater = paginater
- collection
- end
- end
-end
106 lib/pagination/instance_methods.rb
View
@@ -1,106 +0,0 @@
-# encoding: utf-8
-
-module Pagination
-
- # The InstanceMethods are mixed into the collection by
- # Pagination.apply, which means they can be used in your views.
- #
- # See the Pagination module documentation for more info and examples.
-
- module InstanceMethods
- # The paginater info object
- attr_accessor :paginater
-
- # Total number of pages.
- def pages
- paginater.pages
- end
- alias :total_pages :pages
-
- # Current page.
- def current_page
- paginater.page
- end
- alias :page :current_page
-
- # Total number of items.
- def total_count
- paginater.total_count
- end
-
- # Number of items per page.
- def per_page
- paginater.per_page
- end
- alias :limit :per_page
-
- # The start offset (number of items skipped).
- def offset_value
- paginater.offset
- end
- alias :offset :offset_value
-
- # Number of the first page (which for obvious reasons is always 1).
- def first_page
- 1
- end
-
- # Number of the last page.
- def last_page
- pages
- end
-
- # Number of the previous page, or nil if there isn't one.
- def previous_page
- (page > 1) ? (page - 1) : nil
- end
-
- # Number of the next page, or nil if there isn't one.
- def next_page
- (pages > page) ? (page + 1) : nil
- end
-
- # Returns true or false, depending if there's a previous page.
- def previous_page?
- (previous_page) ? true : false
- end
-
- # Returns true or false, depending if there's a next page.
- def next_page?
- (next_page) ? true : false
- end
-
- # Returns true if the collection is on the first page.
- def first_page?
- (page == first_page) ? true : false
- end
-
- # Returns true if the collection is on the last page.
- def last_page?
- (page == last_page) ? true : false
- end
-
- # Number of items presented in context
- def context
- paginater.context
- end
-
- # Returns true if the collection has items in context.
- def context?
- paginater.context?
- end
-
- # Get an array of nearby pages.
- def nearest_pages(number=5)
- first = page - (number/2)
- first = 1 if first < 1
- last = first + (number - 1)
- last = pages if last > pages
- if (last - first) < number
- first = last - (number - 1)
- first = 1 if first < 1
- end
- (first..last).to_a
- end
- end
-end
68 lib/pagination/paginater.rb
View
@@ -1,68 +0,0 @@
-# encoding: utf-8
-
-module Pagination
- # The Paginater stores pagination info and handles the math.
- # See the Pagination module documentation for usage examples.
- class Paginater
-
- # Total number of items
- attr_reader :total_count
- # Items per page
- attr_reader :per_page
- # Total number of pages
- attr_reader :pages
- # Number of items to present in context
- attr_reader :context
- ## Current page
- #attr_accessor :page
-
- # The following parameters are required:
- # * <tt>:total_count</tt> - Total count of items.
- # * <tt>:per_page</tt> - Items per page.
- # * <tt>:page</tt> - Current page.
- def initialize(options)
- @total_count = options[:total_count]
- @per_page = options[:per_page]
- @page = options[:page]
- @context = options[:context] || 0
- end
-
- # Returns total number of pages.
- def pages
- (total_count.to_f/@per_page).ceil
- end
-
- # Returns the current page.
- def page
- if @page.to_s == "last"
- @page = pages
- else
- @page = @page.to_i
- @page = 1 if @page < 1
- @page = pages if @page > pages
- end
- @page
- end
-
- # Sets the current page
- def page=(new_page)
- @page = new_page
- self.page
- end
-
- def limit
- (context?) ? (@per_page + context) : @per_page
- end
-
- def context?
- (@context > 0 && page > 1) ? true : false
- end
-
- # Returns the start offset.
- def offset
- o = (per_page * (page - 1)) - context
- o = 0 if o < 0
- o
- end
- end
-end
50 spec/lib/pagination/paginater_spec.rb
View
@@ -1,50 +0,0 @@
-require 'spec_helper'
-
-describe Pagination::Paginater do
-
- context 'on the 3rd page with 35 items, 10 per page' do
- before { @paginater = Pagination::Paginater.new(:total_count => 35, :per_page => 10, :page => 3) }
-
- it 'has 4 pages' do
- @paginater.pages.should eq(4)
- end
-
- it 'has a limit of 10' do
- @paginater.limit.should eq(10)
- end
-
- it 'has an offset of 20' do
- @paginater.offset.should eq(20)
- end
-
- it 'does not go beyond 4 pages' do
- @paginater.page = 5
- @paginater.page.should eq(4)
- end
-
- it 'does not go below page 1' do
- @paginater.page = 0
- @paginater.page.should eq(1)
- end
-
- it 'page = :last goes to the last page' do
- @paginater.page = :last
- @paginater.page.should eq(@paginater.pages)
- end
-
- it 'applies to a collection' do
- collection = [1,2,3,4,5,6,7,8,9,10]
- Pagination.apply(collection, @paginater)
- collection.paginater.should eq(@paginater)
- end
- end
-
- context 'with zero items' do
- before { @paginater = Pagination::Paginater.new(:total_count => 0, :per_page => 10, :page => 1) }
-
- it 'reports an offset of 0' do
- @paginater.offset.should eq(0)
- end
- end
-
-end
101 spec/lib/pagination_spec.rb
View
@@ -1,101 +0,0 @@
-require 'spec_helper'
-
-describe 'A collection run through Pagination.paginate' do
-
- context 'on page 3' do
- before do
- @collection = Pagination.paginate(:total_count => 95, :per_page => 10, :page => 3){[1,2,3,4,5,6,7,8,9,10]}
- end
-
- it 'retains it original object type' do
- @collection.should be_kind_of(Enumerable)
- end
-
- it 'has Pagination::InstanceMethods mixed in' do
- @collection.should be_kind_of(Pagination::InstanceMethods)
- end
-
- it 'delegates to paginater' do
- [:total_count, :pages, :page, :per_page, :offset].each do |delegated_method|
- @collection.send(delegated_method).should eq(@collection.paginater.send(delegated_method))
- end
- end
-
- it 'has a first page' do
- @collection.first_page.should eq(1)
- end
-
- it 'has a last page' do
- @collection.last_page.should eq(10)
- end
-
- it 'is not on the first page' do
- @collection.first_page?.should be_false
- end
-
- it 'is not on the last page' do
- @collection.last_page?.should be_false
- end
-
- it 'has a next page' do
- @collection.next_page?.should be_true
- end
-
- it 'has a previous page' do
- @collection.previous_page?.should be_true
- end
-
- it 'responds to next page with 4' do
- @collection.next_page.should eq(4)
- end
-
- it 'responds to previous page with 2' do
- @collection.previous_page.should eq(2)
- end
-
- it 'reports its nearest pages' do
- @collection.nearest_pages(3).should eq([2,3,4])
- @collection.nearest_pages(7).should eq([1,2,3,4,5,6,7])
- end
-
- end
-
- context 'on the first page' do
- before do
- @collection = Pagination.paginate(:total_count => 95, :per_page => 10, :page => 1){[1,2,3,4,5,6,7,8,9,10]}
- end
-
- it 'is on the first page' do
- @collection.first_page?.should be_true
- end
-
- it 'does not have a previous page' do
- @collection.previous_page?.should be_false
- @collection.previous_page.should be_nil
- end
-
- it 'reports its nearest pages' do
- @collection.nearest_pages(3).should eq([1,2,3])
- end
- end
-
- context 'on the last page' do
- before do
- @collection = Pagination.paginate(:total_count => 95, :per_page => 10, :page => :last){[1,2,3,4,5,6,7,8,9,10]}
- end
-
- it 'is on the last page' do
- @collection.last_page?.should be_true
- end
-
- it 'does not have a next page' do
- @collection.next_page?.should be_false
- @collection.next_page.should be_nil
- end
-
- it 'reports its nearest pages' do
- @collection.nearest_pages(3).should eq([8,9,10])
- end
- end
-
-end
Please sign in to comment.
Something went wrong with that request. Please try again.