objectreload-pagination 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gemspec
2
+ *.log
3
+ *.gem
4
+ pkg/
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -5,48 +5,54 @@ module Pagination
5
5
  super "#{page} must be less than #{max}" if page >= 1
6
6
  end
7
7
  end
8
-
8
+
9
9
  class Collection < Array
10
10
  attr_reader :current_page, :per_page, :total_entries, :total_pages
11
-
11
+
12
12
  def initialize(options = {})
13
13
  @collection = options[:collection]
14
-
15
- @current_page = options[:current_page].to_i
14
+
15
+ if (already_paginated = ([:current_page, :per_page, :total_entries].all? { |m| @collection.respond_to?(m) }))
16
+ @current_page = @collection.current_page
17
+ @per_page = @collection.per_page
18
+ self.total_entries ||= @collection.total_entries
19
+ else
20
+ @current_page = options[:current_page].to_i
21
+ @per_page = options[:per_page].to_i
22
+ self.total_entries ||= @collection.count
23
+ end
24
+
16
25
  raise InvalidPage.new(@current_page) if @current_page < 1
17
-
18
- @per_page = options[:per_page].to_i
19
26
  raise ArgumentError, "`per_page` setting cannot be less than 1 (#{@per_page} given)" if @per_page < 1
20
-
21
- self.replace(@collection.paginate(:limit => limit, :offset => offset))
22
-
23
- self.total_entries ||= @collection.count
27
+
28
+ replace(already_paginated ? @collection : @collection.paginate(:limit => limit, :offset => offset))
29
+
24
30
  raise InvalidPage.new(current_page, total_pages) if current_page > total_pages
25
31
  end
26
-
32
+
27
33
  def previous_page
28
34
  current_page - 1 if current_page > 1
29
35
  end
30
-
36
+
31
37
  def next_page
32
38
  current_page + 1 if current_page < total_pages
33
39
  end
34
-
40
+
41
+ def offset
42
+ offset = (current_page-1)*limit
43
+ end
44
+
35
45
  protected
36
-
46
+
37
47
  def limit
38
48
  per_page
39
49
  end
40
-
41
- def offset
42
- offset = (current_page-1)*limit
43
- end
44
-
50
+
45
51
  def total_entries=(number)
46
52
  @total_entries = number.to_i
47
53
  @total_pages = @total_entries > 0 ? (@total_entries / per_page.to_f).ceil : 1
48
54
  end
49
-
55
+
50
56
  def replace(array)
51
57
  result = super
52
58
 
@@ -59,4 +65,4 @@ module Pagination
59
65
  result
60
66
  end
61
67
  end
62
- end
68
+ end
@@ -23,7 +23,7 @@ class CollectionTest < Test::Unit::TestCase
23
23
  assert_equal expected, @collection.paginate(conditions)
24
24
  end
25
25
  end
26
-
26
+
27
27
  context "init pagination collection" do
28
28
  setup do
29
29
  @collection = [1,2,3,4,5]
@@ -57,8 +57,28 @@ class CollectionTest < Test::Unit::TestCase
57
57
  assert_raise(ArgumentError) { paginate_collection(@collection, 2, -2) }
58
58
  end
59
59
  end
60
+
61
+ context "having already paginated collection" do
62
+ setup do
63
+ @collection = (11..20).to_a
64
+ class << @collection
65
+ def total_entries; 100; end
66
+ def current_page; 2; end
67
+ def per_page; 10; end
68
+ end
69
+
70
+ @paginated_collection = paginate_collection(@collection, 1, 1, 1)
71
+ end
72
+
73
+ should "use attribute values of the original collection" do
74
+ assert_equal @collection, @paginated_collection
75
+ assert_equal @collection.total_entries, @paginated_collection.total_entries
76
+ assert_equal @collection.current_page, @paginated_collection.current_page
77
+ assert_equal @collection.per_page, @paginated_collection.per_page
78
+ end
79
+ end
60
80
  end
61
-
81
+
62
82
  private
63
83
  def paginate_collection(collection, current_page=2, per_page=2, total_entries=nil)
64
84
  Pagination::Collection.new(
@@ -68,4 +88,4 @@ class CollectionTest < Test::Unit::TestCase
68
88
  :total_entries => total_entries
69
89
  )
70
90
  end
71
- end
91
+ end
@@ -1,3 +1,4 @@
1
+ gem 'actionpack', '2.3.5'
1
2
  require 'action_controller'
2
3
  require 'action_controller/test_process'
3
4
 
@@ -37,7 +38,7 @@ class Pagination::ViewTestCase < Test::Unit::TestCase
37
38
  end
38
39
 
39
40
  def default_test; end
40
-
41
+
41
42
  protected
42
43
 
43
44
  def paginate(collection = {}, options = {}, &block)
@@ -45,7 +46,7 @@ class Pagination::ViewTestCase < Test::Unit::TestCase
45
46
  if collection.instance_of? Hash
46
47
  @request.params :page => collection[:page] || 1
47
48
  collection = [1,2,3,4,5,6,7,8,9,10,11]
48
- end
49
+ end
49
50
  options = {:per_page => 4, :controls => :top}.merge(options)
50
51
 
51
52
  locals = { :collection => collection, :options => options }
@@ -177,4 +178,4 @@ module HTML
177
178
  childless?? '' : super
178
179
  end
179
180
  end
180
- end
181
+ end
data/test/test_helper.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  require 'rubygems'
2
- require 'activerecord'
2
+ gem 'test-unit', '1.2.3'
3
+ require 'test/unit'
4
+ gem 'activerecord', '2.3.5'
5
+ require 'active_record'
3
6
  require 'shoulda'
4
7
  require 'logger'
5
8
 
@@ -33,4 +36,4 @@ end
33
36
 
34
37
  class Article < ActiveRecord::Base
35
38
  belongs_to :user
36
- end
39
+ end
@@ -17,7 +17,7 @@ class ViewHelpersTest < Pagination::ViewTestCase
17
17
 
18
18
  assert_dom_equal expected, @html_result
19
19
  end
20
-
20
+
21
21
  should "show links to pages" do
22
22
  paginate do |pagination|
23
23
  assert_select 'a[href]', 3 do |elements|
@@ -221,4 +221,4 @@ class AdditionalLinkAttributesRenderer < Pagination::LinkRenderer
221
221
  def page_link(page, text, attributes = {})
222
222
  @template.link_to text, url_for(page), attributes.merge(@additional_link_attributes)
223
223
  end
224
- end
224
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: objectreload-pagination
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateusz Drozdzynski
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-13 00:00:00 +02:00
12
+ date: 2010-02-26 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,10 +23,12 @@ extra_rdoc_files:
23
23
  - LICENSE
24
24
  - README.rdoc
25
25
  files:
26
+ - .gitignore
26
27
  - CHANGELOG.rdoc
27
28
  - LICENSE
28
29
  - README.rdoc
29
30
  - Rakefile
31
+ - VERSION
30
32
  - init.rb
31
33
  - lib/active_record/base_extensions.rb
32
34
  - lib/active_record/scope_extensions.rb
@@ -70,8 +72,8 @@ signing_key:
70
72
  specification_version: 3
71
73
  summary: Simple pagination - without changes in controllers
72
74
  test_files:
73
- - test/test_helper.rb
74
75
  - test/lib/view_test_process.rb
75
76
  - test/active_record_extentions_test.rb
76
77
  - test/collection_test.rb
77
78
  - test/view_helpers_test.rb
79
+ - test/test_helper.rb