-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of [email protected]:pjg/simply_paginate
- Loading branch information
Showing
13 changed files
with
253 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
test/log/* | ||
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Array.class_eval do | ||
def paginate(options = {}) | ||
SimplyPaginate::Collection.create(options[:page] || 1, options[:per_page] || 30, options[:total_entries] || self.length) {|pager| | ||
pager.replace self[pager.offset, pager.per_page].to_a | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
require 'array' | ||
require 'collection' | ||
require 'view_helpers' | ||
require 'finder' | ||
require 'view_helpers' | ||
|
||
ActiveRecord::Base.send :include, SimplyPaginate::Finder | ||
ActionView::Base.send :include, SimplyPaginate::ViewHelpers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
sqlite3: | ||
:adapter: sqlite3 | ||
:database: ":memory:" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ActiveRecord::Schema.define(:version => 1) do | ||
create_table "rectangles", :force => true do |t| | ||
t.string "name", :limit => 30 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Rectangle < ActiveRecord::Base | ||
named_scope :ordered, :order => 'name' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
rectangle_one: | ||
name: 'my name is 1' | ||
|
||
rectangle_two: | ||
name: 'my name is 2' | ||
|
||
rectangle_three: | ||
name: 'my name is 3' | ||
|
||
rectangle_four: | ||
name: 'my name is 4' | ||
|
||
rectangle_five: | ||
name: 'my name is 5' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class RectanglesController < ApplicationController | ||
# clear all filters defined in application.rb | ||
skip_filter filter_chain | ||
|
||
# second page of paginated results | ||
def index | ||
@rectangles = Rectangle.paginate(:per_page => 2, :page => 2) | ||
render :inline => "<%= pagination_for @rectangles %>", :layout => false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require File.dirname(__FILE__) + '/../test_helper' | ||
require 'action_controller' | ||
require 'action_controller/test_process' | ||
|
||
require File.dirname(__FILE__) + '/../fixtures/rectangle_model.rb' | ||
require File.dirname(__FILE__) + '/../fixtures/rectangles_controller.rb' | ||
|
||
class UsersControllerTest < ActionController::TestCase | ||
|
||
include SimplyAuthenticate::Helpers | ||
|
||
def setup | ||
@controller = RectanglesController.new | ||
@request = ActionController::TestRequest.new | ||
@response = ActionController::TestResponse.new | ||
|
||
ActionController::Routing::Routes.draw do |map| | ||
map.root :controller => 'rectangles' | ||
end | ||
end | ||
|
||
# test pagination links on second page of paginated results | ||
def test_pagination_for | ||
get :index | ||
assert_response :success | ||
assert_select 'p.pagination' | ||
assert_select 'p.pagination a', :text => /«/ | ||
assert_select 'p.pagination a', :text => /»/ | ||
assert_select 'p.pagination a', :text => /1/ | ||
assert_select 'p.pagination a', :text => /3/ | ||
assert_select 'p.pagination strong', :text => /2/ | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
ENV['RAILS_ENV'] = 'test' | ||
ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..' | ||
|
||
require 'test/unit' | ||
require 'rubygems' | ||
|
||
# Optional gems | ||
begin | ||
require 'redgreen' | ||
rescue LoadError | ||
end | ||
|
||
# Load Rails | ||
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb')) | ||
|
||
# Setup the database | ||
config = YAML::load(IO.read(File.dirname(__FILE__) + '/config/database.yml')) | ||
|
||
Dir.mkdir(File.dirname(__FILE__) + '/log') if !File.exists?(File.dirname(__FILE__) + '/log') | ||
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/log/test.log') | ||
|
||
db_adapter = | ||
begin | ||
require 'sqlite3' | ||
'sqlite3' | ||
end | ||
|
||
if db_adapter.nil? | ||
raise "Could not select the database adapter. Please install Sqlite3 (gem install sqlite3-ruby)." | ||
end | ||
|
||
|
||
ActiveRecord::Base.establish_connection(config[db_adapter]) | ||
|
||
# Load the test database schema | ||
load(File.dirname(__FILE__) + '/db/schema.rb') | ||
|
||
# Load the plugin | ||
require File.dirname(__FILE__) + '/../init.rb' | ||
|
||
# Setup fixtures | ||
require 'active_support/test_case' | ||
require 'active_record/fixtures' | ||
|
||
# Load the plugin's fixtures | ||
Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures" | ||
|
||
class Test::Unit::TestCase | ||
self.use_transactional_fixtures = true | ||
self.use_instantiated_fixtures = true | ||
fixtures :all | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require File.dirname(__FILE__) + '/../test_helper' | ||
|
||
require File.dirname(__FILE__) + '/../fixtures/rectangle_model.rb' | ||
|
||
class PaginateTest < Test::Unit::TestCase | ||
|
||
def test_array_pagination | ||
numbers = [1, 2, 3, 4, 5] | ||
|
||
assert_equal SimplyPaginate::Collection, numbers.paginate.class | ||
assert_equal numbers.size, numbers.paginate.total_entries | ||
|
||
paginated = numbers.paginate(:page => 2, :per_page => 2) | ||
assert_equal numbers[2..3], paginated | ||
assert_equal 2, paginated.offset | ||
assert_equal 1, paginated.previous_page | ||
assert_equal 3, paginated.next_page | ||
assert_equal 3, paginated.current_results_first | ||
assert_equal 4, paginated.current_results_last | ||
|
||
assert_equal numbers[0..1], numbers.paginate(:page => 1, :per_page => 2) | ||
assert_equal numbers[0..3], numbers.paginate(:page => 1, :per_page => 4) | ||
assert_equal numbers[3..4], numbers.paginate(:page => 2, :per_page => 3) | ||
end | ||
|
||
def test_active_record_pagination | ||
rectangles = Rectangle.ordered | ||
|
||
assert_equal rectangles[0..0], Rectangle.paginate(:page => 1, :per_page => 1, :order => 'name') | ||
assert_equal rectangles[0..2], Rectangle.paginate(:page => 1, :per_page => 3, :order => 'name') | ||
assert_equal rectangles[2..3], Rectangle.paginate(:page => 2, :per_page => 2, :order => 'name') | ||
assert_equal rectangles[0..3], Rectangle.paginate(:page => 1, :per_page => 4, :order => 'name') | ||
assert_equal rectangles[4..4], Rectangle.paginate(:page => 2, :per_page => 4, :order => 'name') | ||
|
||
assert_equal rectangles, Rectangle.paginate(:per_page => 100, :order => 'name') | ||
assert_equal rectangles, Rectangle.ordered.paginate(:per_page => 100) | ||
end | ||
end |