folio-pagination 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/folio.rb CHANGED
@@ -45,10 +45,10 @@ module Folio
45
45
  end
46
46
 
47
47
  def configure_pagination(page, options)
48
- current_page = options.fetch(:page) { nil }
48
+ current_page = options[:page]
49
49
  current_page = page.first_page if current_page.nil?
50
50
  page.current_page = current_page
51
- page.per_page = options.fetch(:per_page) { self.per_page }
51
+ page.per_page = options[:per_page] || self.per_page
52
52
  page.total_entries = options.fetch(:total_entries) { self.respond_to?(:count) ? self.count : nil }
53
53
  page
54
54
  end
data/lib/folio/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Folio
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -113,6 +113,23 @@ module Folio
113
113
  end
114
114
 
115
115
  include ::Folio::Ordinal
116
+
117
+ def paginate(options={})
118
+ if !options.has_key?(:total_entries)
119
+ group_values = self.scoped.group_values
120
+ unless group_values.empty?
121
+ # total_entries left to an auto-count, but the relation being
122
+ # paginated has a grouping. we need to do a special count, lest
123
+ # self.count give us a hash instead of the integer we expect.
124
+ if self.scoped.having_values.empty?
125
+ options[:total_entries] = except(:group).select(group_values).uniq.count
126
+ else
127
+ options[:total_entries] = unscoped.from("(#{to_sql}) a").count
128
+ end
129
+ end
130
+ end
131
+ super(options).all
132
+ end
116
133
  end
117
134
 
118
135
  # mix into Active Record. these are the same ones that WillPaginate mixes
@@ -40,33 +40,27 @@ describe ActiveRecord do
40
40
  page.is_a?(Folio::Ordinal::Page).must_equal true
41
41
  end
42
42
 
43
- it "should still return a relation" do
43
+ it "should return an instantiated page" do
44
44
  page = Item.paginate
45
- page.is_a?(ActiveRecord::Relation).must_equal true
45
+ page.is_a?(Array).must_equal true
46
46
  end
47
47
 
48
- it "should set limit_value according to per_page" do
49
- page = Item.paginate(per_page: 10)
50
- page.limit_value.must_equal 10
51
- end
52
-
53
- it "should use that limit_value as per_page attribute" do
48
+ it "should respect per_page" do
54
49
  page = Item.paginate(per_page: 10)
55
50
  page.per_page.must_equal 10
56
51
  end
57
52
 
58
- it "should default limit_value/per_page to model's per_page" do
53
+ it "should default per_page to model's per_page" do
59
54
  was = Item.per_page
60
55
  Item.per_page = 10
61
56
  page = Item.paginate
62
- page.limit_value.must_equal 10
63
57
  page.per_page.must_equal 10
64
58
  Item.per_page = was
65
59
  end
66
60
 
67
61
  it "should set offset from page and per_page" do
68
62
  page = Item.paginate(page: 3, per_page: 10)
69
- page.offset_value.must_equal 20
63
+ page.offset.must_equal 20
70
64
  end
71
65
 
72
66
  it "should set current_page" do
@@ -74,19 +68,19 @@ describe ActiveRecord do
74
68
  page.current_page.must_equal 3
75
69
  end
76
70
 
77
- it "should have the right count on the unloaded page" do
71
+ it "should have the right count" do
78
72
  page = Item.paginate(page: 3, per_page: 10)
79
73
  page.count.must_equal 4
80
74
  end
81
75
 
82
- it "should have the right size on the unloaded page" do
76
+ it "should have the right size" do
83
77
  page = Item.paginate(page: 3, per_page: 10)
84
78
  page.size.must_equal 4
85
79
  end
86
80
 
87
81
  it "should return the expected items" do
88
82
  page = Item.paginate(page: 3, per_page: 10)
89
- page.to_a.must_equal Item.offset(20).all
83
+ page.must_equal Item.offset(20).all
90
84
  end
91
85
 
92
86
  it "should auto-count total_entries unless specified as nil" do
@@ -94,11 +88,26 @@ describe ActiveRecord do
94
88
  page.total_entries.must_equal 24
95
89
  end
96
90
 
91
+ it "should correctly auto-count total_entries with a grouping" do
92
+ page = Item.group(:filter).paginate
93
+ page.total_entries.must_equal 2
94
+ end
95
+
96
+ it "should correctly auto-count total_entries with a grouping and a having" do
97
+ page = Item.group(:filter).having("COUNT(*)>1").paginate
98
+ page.total_entries.must_equal 2
99
+ end
100
+
97
101
  it "should work with total_entries nil" do
98
102
  page = Item.paginate(page: 3, total_entries: nil)
99
103
  page.current_page.must_equal 3
100
104
  end
101
105
 
106
+ it "should work with per_page nil" do
107
+ page = Item.paginate(per_page: nil)
108
+ page.per_page.must_equal Folio.per_page
109
+ end
110
+
102
111
  it "should validate page number against auto-counted total_entries" do
103
112
  lambda{ Item.paginate(page: 4, per_page: 10) }.must_raise Folio::InvalidPage
104
113
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: folio-pagination
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-17 00:00:00.000000000 Z
12
+ date: 2014-02-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler