dimkiriyenko-kaminari 0.12.4 → 0.12.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +2 -1
- data/lib/kaminari/version.rb +1 -1
- data/spec/fake_app.rb +10 -3
- data/spec/models/active_record_relation_methods_spec.rb +5 -3
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -62,10 +62,11 @@ Then bundle:
|
|
62
62
|
Note that the +per+ scope is not directly defined on the models but is just a method defined on the page scope. This is absolutely reasonable because you will never actually use +per_page+ without specifying the +page+ number.
|
63
63
|
|
64
64
|
* the +smart_count+ method
|
65
|
+
|
65
66
|
+ActiveRecord#count+ method may fail or give wrong results on complex queries. Say, for example, you use +having+ constraint, based on +select+ generated columns. As long as generated columns are thrown away, +having+ constraint will fail and you will get invalid query. If this is your case, you should define a +smart_count+ method on your model or scope. A possible implementation may be (works with any complex query):
|
66
67
|
|
67
68
|
def smart_count
|
68
|
-
count_by_sql("SELECT COUNT(*) FROM (#{to_sql}) AS count_table")
|
69
|
+
count_by_sql("SELECT COUNT(*) FROM (#{scoped.to_sql}) AS count_table")
|
69
70
|
end
|
70
71
|
|
71
72
|
Kaminari will call this method instead of +count+ if it is defined.
|
data/lib/kaminari/version.rb
CHANGED
data/spec/fake_app.rb
CHANGED
@@ -29,15 +29,22 @@ class User < ActiveRecord::Base
|
|
29
29
|
User.joins(:books_read => :authors).where(:authors_books => {:id => self})
|
30
30
|
end
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
cols = if connection.adapter_name == "PostgreSQL"
|
32
|
+
def self.cols
|
33
|
+
if connection.adapter_name == "PostgreSQL"
|
35
34
|
column_names.map { |column| %{"users"."#{column}"} }.join(", ")
|
36
35
|
else
|
37
36
|
'"users"."id"'
|
38
37
|
end
|
38
|
+
end
|
39
|
+
|
40
|
+
scope :by_name, order(:name)
|
41
|
+
scope :by_read_count, lambda {
|
39
42
|
group(cols).select("count(readerships.id) AS read_count, #{cols}").order('read_count DESC')
|
40
43
|
}
|
44
|
+
|
45
|
+
scope :read_count_at_least, lambda { |cnt|
|
46
|
+
group(cols).select("count(readerships.id) AS read_count, #{cols}").having("read_count >= #{cnt}")
|
47
|
+
}
|
41
48
|
end
|
42
49
|
class Authorship < ActiveRecord::Base
|
43
50
|
belongs_to :user
|
@@ -10,7 +10,8 @@ describe Kaminari::ActiveRecordRelationMethods do
|
|
10
10
|
@books2 = 3.times.map {|i| @author2.books_authored.create!(:title => "title%03d" % i) }
|
11
11
|
@books3 = 4.times.map {|i| @author3.books_authored.create!(:title => "subject%03d" % i) }
|
12
12
|
@readers = 4.times.map { User.create! :name => 'reader' }
|
13
|
-
@books.each {|book| book.readers << @readers }
|
13
|
+
@books.each { |book| book.readers << @readers }
|
14
|
+
@books2.each { |book| book.readers << @readers.take(2) }
|
14
15
|
end
|
15
16
|
|
16
17
|
context "when the scope includes an order which references a generated column" do
|
@@ -28,12 +29,13 @@ describe Kaminari::ActiveRecordRelationMethods do
|
|
28
29
|
context "when the scope responds to smart_count" do
|
29
30
|
module SmartCount
|
30
31
|
def smart_count
|
31
|
-
|
32
|
+
count_by_sql("SELECT COUNT(*) FROM (#{scoped.to_sql}) AS count_table")
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
35
36
|
it "should call smart_count on scope and return the given value" do
|
36
|
-
|
37
|
+
# only 2 users have read more than two books
|
38
|
+
User.joins(:readerships).read_count_at_least(3).extending(SmartCount).page(1).total_count.should == 2
|
37
39
|
end
|
38
40
|
end
|
39
41
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dimkiriyenko-kaminari
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 37
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 12
|
9
|
-
-
|
10
|
-
version: 0.12.
|
9
|
+
- 5
|
10
|
+
version: 0.12.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Akira Matsuda
|