kaminari-activerecord 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a9a6578e9af2cd73cd987f013310cc7db6f338af3996cc9f8033279bb80fe37f
4
- data.tar.gz: 26f8663b6b30ef6a4cae8529e86a9e394d4dec9936e7518ea0dcc971a29c15cd
3
+ metadata.gz: df68cbe8dbf5e813cf9695d7b7f89c432005701347017ae25e8711646ea9d21e
4
+ data.tar.gz: 6f7d3965cc8849695ed7e8b495b0040f3405a7669ff1333dc570af4d10620942
5
5
  SHA512:
6
- metadata.gz: 6d72db40c4816e6a57074b39a0118a5586bb3a5c39c40dfecef53a61b9ad83cdf380cd2a249f1305d73ff1c3c3f77d744d4f6fd5cc71858793a9f5b34c923d8a
7
- data.tar.gz: 3e8de5ead4b7627e8d6a803f0cff8c3608f4c37422cc33db36aebed5dc23d30d340ef09c1f3d19f9a2f60b30981194fe1b1ee1600c91f2267ea419371a20c726
6
+ metadata.gz: 277cb5b789e37e8889f66e03f5aaec7ee06f04c3230630a0d9c06ca242c2a29545eee91f0bf8262d3d7fa8229dcf77714bc095a6925f886716af3fef2ce497ec
7
+ data.tar.gz: b240655b581ed051c18831dfad97d5207cb02d41eaed7db3bcf95932fff8a7acc64df18bb21776eb9d63f4792f05912b8e7ad4566e8edba6531655cbbda62f32
@@ -1,5 +1,5 @@
1
- # coding: utf-8
2
1
  # frozen_string_literal: true
2
+
3
3
  lib = File.expand_path('../lib', __FILE__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require 'kaminari/activerecord/version'
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "kaminari/activerecord/version"
3
4
  require 'active_support/lazy_load_hooks'
4
5
 
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'kaminari/activerecord/active_record_model_extension'
3
4
 
4
5
  module Kaminari
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'kaminari/activerecord/active_record_relation_methods'
3
4
 
4
5
  module Kaminari
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Kaminari
3
4
  # Active Record specific page scope methods implementations
4
5
  module ActiveRecordRelationMethods
@@ -21,22 +22,21 @@ module Kaminari
21
22
  # Total count has to be 0 if loaded records are 0
22
23
  return @total_count = 0 if (current_page == 1) && @records.empty?
23
24
  # Total count is calculable at the last page
24
- per_page = (defined?(@_per) && @_per) || default_per_page
25
- return @total_count = (current_page - 1) * per_page + @records.length if @records.any? && (@records.length < per_page)
25
+ return @total_count = (current_page - 1) * limit_value + @records.length if @records.any? && (@records.length < limit_value)
26
26
  end
27
27
 
28
28
  # #count overrides the #select which could include generated columns referenced in #order, so skip #order here, where it's irrelevant to the result anyway
29
29
  c = except(:offset, :limit, :order)
30
30
  # Remove includes only if they are irrelevant
31
31
  c = c.except(:includes) unless references_eager_loaded_tables?
32
- # .group returns an OrderedHash that responds to #count
33
- c = c.count(column_name)
34
- @total_count = if c.is_a?(Hash) || c.is_a?(ActiveSupport::OrderedHash)
35
- c.count
36
- elsif c.respond_to? :count
37
- c.count(column_name)
38
- else
39
- c
32
+
33
+ c = c.limit(max_pages * limit_value) if max_pages && max_pages.respond_to?(:*)
34
+
35
+ # Handle grouping with a subquery
36
+ @total_count = if c.group_values.any?
37
+ c.model.from(c.except(:select).select("1")).count
38
+ else
39
+ c.count(column_name)
40
40
  end
41
41
  end
42
42
 
@@ -50,6 +50,35 @@ module Kaminari
50
50
  end
51
51
 
52
52
  # A module that makes AR::Relation paginatable without having to cast another SELECT COUNT query
53
+ module PaginatableWithoutCount
54
+ module LimitValueSetter
55
+ # refine PaginatableWithoutCount do # NOTE: this doesn't work in Ruby < 2.4
56
+ refine ::ActiveRecord::Relation do
57
+ private
58
+
59
+ # Update multiple instance variables that holds `limit` to a given value
60
+ def set_limit_value(new_limit)
61
+ @values[:limit] = new_limit
62
+
63
+ if @arel
64
+ case @arel.limit
65
+ when Integer
66
+ @arel.limit = new_limit
67
+ when Arel::Nodes::BindParam
68
+ if @arel.limit.respond_to?(:value)
69
+ @arel.limit = Arel::Nodes::BindParam.new(@arel.limit.value.with_cast_value(new_limit))
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ # NOTE: ending all modules and reopening again because using has to be called from the toplevel in Ruby 2.0
79
+ using Kaminari::PaginatableWithoutCount::LimitValueSetter
80
+
81
+ module Kaminari
53
82
  module PaginatableWithoutCount
54
83
  # Overwrite AR::Relation#load to actually load one more record to judge if the page has next page
55
84
  # then store the result in @_has_next ivar
@@ -57,13 +86,9 @@ module Kaminari
57
86
  if loaded? || limit_value.nil?
58
87
  super
59
88
  else
60
- @values[:limit] = limit_value + 1
61
- # FIXME: this could be removed when we're dropping AR 4 support
62
- @arel.limit = @values[:limit] if @arel && (Integer === @arel.limit)
89
+ set_limit_value limit_value + 1
63
90
  super
64
- @values[:limit] = limit_value - 1
65
- # FIXME: this could be removed when we're dropping AR 4 support
66
- @arel.limit = @values[:limit] if @arel && (Integer === @arel.limit)
91
+ set_limit_value limit_value - 1
67
92
 
68
93
  if @records.any?
69
94
  @records = @records.dup if (frozen = @records.frozen?)
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Kaminari
3
4
  module Activerecord
4
- VERSION = '1.1.1'
5
+ VERSION = '1.2.0'
5
6
  end
6
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kaminari-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Matsuda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-21 00:00:00.000000000 Z
11
+ date: 2020-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kaminari-core
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 1.1.1
19
+ version: 1.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 1.1.1
26
+ version: 1.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -101,8 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  - !ruby/object:Gem::Version
102
102
  version: '0'
103
103
  requirements: []
104
- rubyforge_project:
105
- rubygems_version: 2.6.14
104
+ rubygems_version: 3.1.2
106
105
  signing_key:
107
106
  specification_version: 4
108
107
  summary: Kaminari Active Record adapter