kaminari-activerecord 1.0.0.beta2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +21 -0
- data/README.md +32 -0
- data/kaminari-activerecord.gemspec +26 -0
- data/lib/kaminari/activerecord.rb +8 -0
- data/lib/kaminari/activerecord/active_record_extension.rb +23 -0
- data/lib/kaminari/activerecord/active_record_model_extension.rb +24 -0
- data/lib/kaminari/activerecord/active_record_relation_methods.rb +88 -0
- data/lib/kaminari/activerecord/version.rb +6 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d6e55d83b0bccd30eea50b63a401eedab96bd110
|
4
|
+
data.tar.gz: 66b0af1385a854e91e2ed9ff0b4efbfc1b8d5477
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '08ac3a7c80b4f4be3e8618ddacffe49b52ca141943781ca166f699803203bfeaa71feda2cd50834637bb49f8cc98ef0678cb83fead3a7c31c9a63b976521d5a4'
|
7
|
+
data.tar.gz: 459cc20d8083391fb1b4ef4e6d185be221e7e993a5cb684119e2a694c0bef05699f5ddead4c83fecd8cd5e02a4bb8208d0e69c6138901cc9b598efe70b0f150e
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Akira Matsuda
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Kaminari::Activerecord
|
2
|
+
|
3
|
+
Kaminari Active Record adapter.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'kaminari-activerecord'
|
12
|
+
```
|
13
|
+
|
14
|
+
And bundle.
|
15
|
+
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
This gem is basically an internal gem that will be automatically bundled from kaminari gem.
|
20
|
+
|
21
|
+
Or if you're using ORMs other than Active Record, you might need to explicitly bundle this gem.
|
22
|
+
|
23
|
+
See [Kaminari README (Other Framework/Library Support)](https://github.com/amatsuda/kaminari/blob/master/README.md#other-frameworklibrary-support) for details.
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
Pull requests are welcome on GitHub at https://github.com/amatsuda/kaminari.
|
28
|
+
|
29
|
+
|
30
|
+
## License
|
31
|
+
|
32
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'kaminari/activerecord/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "kaminari-activerecord"
|
9
|
+
spec.version = Kaminari::Activerecord::VERSION
|
10
|
+
spec.authors = ["Akira Matsuda"]
|
11
|
+
spec.email = ["ronnie@dio.jp"]
|
12
|
+
|
13
|
+
spec.summary = 'Kaminari Active Record adapter'
|
14
|
+
spec.description = 'kaminari-activerecord lets your Active Record models be paginatable'
|
15
|
+
spec.homepage = 'https://github.com/amatsuda/kaminari'
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'kaminari-core', Kaminari::Activerecord::VERSION
|
22
|
+
spec.add_dependency 'activerecord'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", ">= 1.12"
|
25
|
+
spec.add_development_dependency "rake", ">= 10.0"
|
26
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "kaminari/activerecord/version"
|
3
|
+
require 'active_support/lazy_load_hooks'
|
4
|
+
|
5
|
+
ActiveSupport.on_load :active_record do
|
6
|
+
require 'kaminari/activerecord/active_record_extension'
|
7
|
+
::ActiveRecord::Base.send :include, Kaminari::ActiveRecordExtension
|
8
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'kaminari/activerecord/active_record_model_extension'
|
3
|
+
|
4
|
+
module Kaminari
|
5
|
+
module ActiveRecordExtension
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods #:nodoc:
|
9
|
+
# Future subclasses will pick up the model extension
|
10
|
+
def inherited(kls) #:nodoc:
|
11
|
+
super
|
12
|
+
kls.send(:include, Kaminari::ActiveRecordModelExtension) if kls.superclass == ::ActiveRecord::Base
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
included do
|
17
|
+
# Existing subclasses pick up the model extension as well
|
18
|
+
descendants.each do |kls|
|
19
|
+
kls.send(:include, Kaminari::ActiveRecordModelExtension) if kls.superclass == ::ActiveRecord::Base
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'kaminari/activerecord/active_record_relation_methods'
|
3
|
+
|
4
|
+
module Kaminari
|
5
|
+
module ActiveRecordModelExtension
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
include Kaminari::ConfigurationMethods
|
10
|
+
|
11
|
+
# Fetch the values at the specified page number
|
12
|
+
# Model.page(5)
|
13
|
+
eval <<-RUBY, nil, __FILE__, __LINE__ + 1
|
14
|
+
def self.#{Kaminari.config.page_method_name}(num = nil)
|
15
|
+
per_page = max_per_page && (default_per_page > max_per_page) ? max_per_page : default_per_page
|
16
|
+
limit(per_page).offset(per_page * ((num = num.to_i - 1) < 0 ? 0 : num)).extending do
|
17
|
+
include Kaminari::ActiveRecordRelationMethods
|
18
|
+
include Kaminari::PageScopeMethods
|
19
|
+
end
|
20
|
+
end
|
21
|
+
RUBY
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Kaminari
|
3
|
+
# Active Record specific page scope methods implementations
|
4
|
+
module ActiveRecordRelationMethods
|
5
|
+
# Used for page_entry_info
|
6
|
+
def entry_name(options = {})
|
7
|
+
default = options[:count] == 1 ? model_name.human : model_name.human.pluralize
|
8
|
+
model_name.human(options.reverse_merge(default: default))
|
9
|
+
end
|
10
|
+
|
11
|
+
def reset #:nodoc:
|
12
|
+
@total_count = nil
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
def total_count(column_name = :all, _options = nil) #:nodoc:
|
17
|
+
return @total_count if defined?(@total_count) && @total_count
|
18
|
+
|
19
|
+
# There are some cases that total count can be deduced from loaded records
|
20
|
+
if loaded?
|
21
|
+
# Total count has to be 0 if loaded records are 0
|
22
|
+
return @total_count = 0 if (current_page == 1) && @records.empty?
|
23
|
+
# Total count is calculatable at the last page
|
24
|
+
return @total_count = (current_page - 1) * @_per + @records.length if defined?(@_per) && (@records.length < @_per)
|
25
|
+
end
|
26
|
+
|
27
|
+
# #count overrides the #select which could include generated columns referenced in #order, so skip #order here, where it's irrelevant to the result anyway
|
28
|
+
c = except(:offset, :limit, :order)
|
29
|
+
# Remove includes only if they are irrelevant
|
30
|
+
c = c.except(:includes) unless references_eager_loaded_tables?
|
31
|
+
# .group returns an OrderedHash that responds to #count
|
32
|
+
c = c.count(column_name)
|
33
|
+
@total_count = if c.is_a?(Hash) || c.is_a?(ActiveSupport::OrderedHash)
|
34
|
+
c.count
|
35
|
+
else
|
36
|
+
c.respond_to?(:count) ? c.count(column_name) : c
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Turn this Relation to a "without count mode" Relation.
|
41
|
+
# Note that the "without count mode" is supposed to be performant but has a feature limitation.
|
42
|
+
# Pro: paginates without casting an extra SELECT COUNT query
|
43
|
+
# Con: unable to know the total number of records/pages
|
44
|
+
def without_count
|
45
|
+
extend ::Kaminari::PaginatableWithoutCount
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# A module that makes AR::Relation paginatable without having to cast another SELECT COUNT query
|
50
|
+
module PaginatableWithoutCount
|
51
|
+
# Overwrite AR::Relation#load to actually load one more record to judge if the page has next page
|
52
|
+
# then store the result in @_has_next ivar
|
53
|
+
def load
|
54
|
+
if loaded? || limit_value.nil?
|
55
|
+
super
|
56
|
+
else
|
57
|
+
@values[:limit] = limit_value + 1
|
58
|
+
super
|
59
|
+
@values[:limit] = limit_value - 1
|
60
|
+
|
61
|
+
if @records.any?
|
62
|
+
@records = @records.dup if (frozen = @records.frozen?)
|
63
|
+
@_has_next = !!@records.delete_at(limit_value)
|
64
|
+
@records.freeze if frozen
|
65
|
+
end
|
66
|
+
|
67
|
+
self
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# The page wouldn't be the last page if there's "limit + 1" record
|
72
|
+
def last_page?
|
73
|
+
!out_of_range? && !@_has_next
|
74
|
+
end
|
75
|
+
|
76
|
+
# Empty relation needs no pagination
|
77
|
+
def out_of_range?
|
78
|
+
load unless loaded?
|
79
|
+
@records.empty?
|
80
|
+
end
|
81
|
+
|
82
|
+
# Force to raise an exception if #total_count is called explicitly.
|
83
|
+
def total_count
|
84
|
+
raise "This scope is marked as a non-count paginable scope and can't be used in combination " \
|
85
|
+
"with `#paginate' or `#page_entries_info'. Use #link_to_next_page or #link_to_previous_page instead."
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kaminari-activerecord
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.beta2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Akira Matsuda
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: kaminari-core
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.0.beta2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.0.beta2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.12'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.12'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: kaminari-activerecord lets your Active Record models be paginatable
|
70
|
+
email:
|
71
|
+
- ronnie@dio.jp
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- MIT-LICENSE
|
77
|
+
- README.md
|
78
|
+
- kaminari-activerecord.gemspec
|
79
|
+
- lib/kaminari/activerecord.rb
|
80
|
+
- lib/kaminari/activerecord/active_record_extension.rb
|
81
|
+
- lib/kaminari/activerecord/active_record_model_extension.rb
|
82
|
+
- lib/kaminari/activerecord/active_record_relation_methods.rb
|
83
|
+
- lib/kaminari/activerecord/version.rb
|
84
|
+
homepage: https://github.com/amatsuda/kaminari
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">"
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.3.1
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.6.8
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Kaminari Active Record adapter
|
108
|
+
test_files: []
|