Skip to content

Commit

Permalink
move Array#paginate definition to will_paginate/array.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Apr 4, 2008
1 parent 9039500 commit 4c51c38
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
16 changes: 16 additions & 0 deletions lib/will_paginate/array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'will_paginate/collection'

# http://www.desimcadam.com/archives/8
Array.class_eval do
def paginate(options = {})
raise ArgumentError, "parameter hash expected (got #{options.inspect})" unless Hash === options

WillPaginate::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
19 changes: 1 addition & 18 deletions lib/will_paginate/core_ext.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
require 'set'
require 'will_paginate/collection'

unless Array.instance_methods.include? 'paginate'
# http://www.desimcadam.com/archives/8
Array.class_eval do
def paginate(options = {})
raise ArgumentError, "parameter hash expected (got #{options.inspect})" unless Hash === options

WillPaginate::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
end
require 'will_paginate/array'

unless Hash.instance_methods.include? 'except'
Hash.class_eval do
Expand Down
5 changes: 3 additions & 2 deletions test/array_pagination_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require File.dirname(__FILE__) + '/helper'
require 'will_paginate/core_ext'
require 'will_paginate/array'

class ArrayPaginationTest < Test::Unit::TestCase
def test_simple
Expand All @@ -11,7 +11,8 @@ def test_simple
{ :page => 3, :per_page => 5, :expected => [] },
].
each do |conditions|
assert_equal conditions[:expected], collection.paginate(conditions.slice(:page, :per_page))
expected = conditions.delete :expected
assert_equal expected, collection.paginate(conditions)
end
end

Expand Down

0 comments on commit 4c51c38

Please sign in to comment.